Repository: bevyengine/bevy
File: crates/bevy_math/src/direction.rs (Rust)

Code before:
use crate::{
    primitives::{Primitive2d, Primitive3d},
    Quat, Rot2, Vec2, Vec3, Vec3A, Vec4,
};

use core::f32::consts::FRAC_1_SQRT_2;
use core::fmt;
use derive_more::derive::Into;

#[cfg(feature = "bevy_reflect")]
use bevy_reflect::Reflect;

#[cfg(all(feature = "serialize", feature = "bevy_reflect"))]
use bevy_reflect::{ReflectDeserialize, ReflectSerialize};

#[cfg(all(debug_assertions, feature = "std"))]
use std::eprintln;

use thiserror::Error;

/// An error indicating that a direction is invalid.
#[derive(Debug, PartialEq, Error)]
pub enum InvalidDirectionError {
    /// The length of the direction vector is zero or very close to zero.XES
    #[error("The length of the direction vector is zero or very close to zero")]
    Zero,
    /// The length of the direction vector is `std::f32::INFINITY`.
    #[error("The length of the direction vector is `std::f32::INFINITY`")]
    Infinite,
    /// The length of the direction vector is `NaN`.
    #[error("The length of the direction vector is `NaN`")]
    NaN,
}

impl InvalidDirectionError {
    /// Creates an [`InvalidDirectionError`] from the length of an invalid direction vector.
    pub fn from_length(length: f32) -> Self {
        if length.is_nan() {
            InvalidDirectionError::NaN
        } else if !length.is_finite() {
            // If the direction is non-finite but also not NaN, it must be infinite
            InvalidDirectionError::Infinite
        } else {
            // If the direction is invalid but neither NaN nor infinite, it must be zero
            InvalidDirectionError::Zero
        }
    }
}


Review comment (suggestion):
```suggestion
    /// The length of the direction vector is zero or very close to zero.
```

Code after:
use crate::{
    primitives::{Primitive2d, Primitive3d},
    Quat, Rot2, Vec2, Vec3, Vec3A, Vec4,
};

use core::f32::consts::FRAC_1_SQRT_2;
use core::fmt;
use derive_more::derive::Into;

#[cfg(feature = "bevy_reflect")]
use bevy_reflect::Reflect;

#[cfg(all(feature = "serialize", feature = "bevy_reflect"))]
use bevy_reflect::{ReflectDeserialize, ReflectSerialize};

#[cfg(all(debug_assertions, feature = "std"))]
use std::eprintln;

use thiserror::Error;

/// An error indicating that a direction is invalid.
#[derive(Debug, PartialEq, Error)]
pub enum InvalidDirectionError {
    /// The length of the direction vector is zero or very close to zero.
    #[error("The length of the direction vector is zero or very close to zero")]
    Zero,
    /// The length of the direction vector is `std::f32::INFINITY`.
    #[error("The length of the direction vector is `std::f32::INFINITY`")]
    Infinite,
    /// The length of the direction vector is `NaN`.
    #[error("The length of the direction vector is `NaN`")]
    NaN,
}

impl InvalidDirectionError {
    /// Creates an [`InvalidDirectionError`] from the length of an invalid direction vector.
    pub fn from_length(length: f32) -> Self {
        if length.is_nan() {
            InvalidDirectionError::NaN
        } else if !length.is_finite() {
            // If the direction is non-finite but also not NaN, it must be infinite
            InvalidDirectionError::Infinite
        } else {
            // If the direction is invalid but neither NaN nor infinite, it must be zero
            InvalidDirectionError::Zero
        }
    }
}