temp-converter 1.1.3

Simple terminal temperature unit converter between Celsius, Fahrenheit and Kelvin.
Documentation
//! # Temperature Converter
//! Functions to convert a float of type `f32` representing a temperature unit into another `f32` representing a different temperature unit.

/// Given a float of type `f32` representing a Fahrenheit value, returns it's equvalent in Celsius.
///
/// # Arguments
/// * `x` - an `f32` that represents a Fahrenheit value.
///
/// # Examples
///
/// ```
/// use temp_converter::fahrenheit_to_celsius;
///
/// let fahrenheit = 100.0;
/// let result = fahrenheit_to_celsius(fahrenheit);
/// assert_eq!(result, 37.77778);
/// ```
///
/// You may want to round the result to a specific number of decimal values.
///
/// ```
/// use temp_converter::fahrenheit_to_celsius;
///
/// let fahrenheit = 100.0;
/// let result = fahrenheit_to_celsius(fahrenheit);
///
/// assert_eq!(format!("{:.2}", result), "37.78".to_string());
/// ```
pub fn fahrenheit_to_celsius(x: f32) -> f32 {
    (x - 32.0) * (5.0 / 9.0)
}

/// Given a float of type `f32` representing a Celsius value, returns it's equvalent in Fahrenheit.
///
/// # Arguments
/// * `x` - an `f32` that represents a Celsius value.
///
/// # Examples
///
/// ```
/// use temp_converter::celsius_to_fahrenheit;
///
/// let celsius = 100.0;
/// let result = celsius_to_fahrenheit(celsius);
/// assert_eq!(result, 212.0);
/// ```
///
/// You may want to round the result to a specific number of decimal values.
///
/// ```
/// use temp_converter::celsius_to_fahrenheit;
///
/// let celsius = 100.0;
/// let result = celsius_to_fahrenheit(celsius);
///
/// assert_eq!(format!("{:.2}", result), "212.00".to_string());
/// ```
pub fn celsius_to_fahrenheit(x: f32) -> f32 {
    (x * (9.0 / 5.0)) + 32.0
}

/// Given a float of type `f32` representing a Celsius value, returns it's equvalent in Kelvin.
///
/// # Arguments
/// * `x` - an `f32` that represents a Celsius value.
///
/// # Examples
///
/// ```
/// use temp_converter::celsius_to_kelvin;
///
/// let celsius = 100.0;
/// let result = celsius_to_kelvin(celsius);
/// assert_eq!(result, 373.15);
/// ```
///
/// You may want to round the result to a specific number of decimal values.
///
/// ```
/// use temp_converter::celsius_to_kelvin;
///
/// let celsius = 100.0;
/// let result = celsius_to_kelvin(celsius);
///
/// assert_eq!(format!("{:.1}", result), "373.1".to_string());
/// ```
pub fn celsius_to_kelvin(x: f32) -> f32 {
    x + 273.15
}

/// Given a float of type `f32` representing a Kelvin value, returns it's equvalent in Celsius.
///
/// # Arguments
/// * `x` - an `f32` that represents a Kelvin value.
///
/// # Examples
///
/// ```
/// use temp_converter::kelvin_to_celsius;
///
/// let kelvin = 100.0;
/// let result = kelvin_to_celsius(kelvin);
/// assert_eq!(result, -173.15);
/// ```
///
/// You may want to round the result to a specific number of decimal values.
///
/// ```
/// use temp_converter::kelvin_to_celsius;
///
/// let kelvin = 100.0;
/// let result = kelvin_to_celsius(kelvin);
///
/// assert_eq!(format!("{:.1}", result), "-173.1".to_string());
/// ```
pub fn kelvin_to_celsius(x: f32) -> f32 {
    x - 273.15
}

/// Given a float of type `f32` representing a Fahrenheit value, returns it's equvalent in Kelvin.
///
/// # Arguments
/// * `x` - an `f32` that represents a Fahrenheit value.
///
/// # Examples
///
/// ```
/// use temp_converter::fahrenheit_to_kelvin;
///
/// let fahrenheit = 100.0;
/// let result = fahrenheit_to_kelvin(fahrenheit);
/// assert_eq!(result, 310.92776);
/// ```
///
/// You may want to round the result to a specific number of decimal values.
///
/// ```
/// use temp_converter::fahrenheit_to_kelvin;
///
/// let fahrenheit = 100.0;
/// let result = fahrenheit_to_kelvin(fahrenheit);
///
/// assert_eq!(format!("{:.2}", result), "310.93".to_string());
/// ```
pub fn fahrenheit_to_kelvin(x: f32) -> f32 {
    (x - 32.0) / 1.8 + 273.15
}

/// Given a float of type `f32` representing a Kelvin value, returns it's equvalent in Fahrenheit.
///
/// # Arguments
/// * `x` - an `f32` that represents a Kelvin value.
///
/// # Examples
///
/// ```
/// use temp_converter::kelvin_to_fahrenheit;
///
/// let kelvin = 100.0;
/// let result = kelvin_to_fahrenheit(kelvin);
/// assert_eq!(result, -279.66998);
/// ```
///
/// You may want to round the result to a specific number of decimal values.
///
/// ```
/// use temp_converter::kelvin_to_fahrenheit;
///
/// let kelvin = 100.0;
/// let result = kelvin_to_fahrenheit(kelvin);
///
/// assert_eq!(format!("{:.2}", result), "-279.67".to_string());
/// ```
pub fn kelvin_to_fahrenheit(x: f32) -> f32 {
    (x - 273.15) * 1.8 + 32.0
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn f_to_c_works() {
        assert_eq!(fahrenheit_to_celsius(0.0), -17.777_779);
        assert_eq!(fahrenheit_to_celsius(100.0), 37.777_78);
        assert_eq!(fahrenheit_to_celsius(1000.0), 537.777_83);
        assert_eq!(fahrenheit_to_celsius(5000.0), 2_760.000_2);
        assert_eq!(fahrenheit_to_celsius(69420.0), 38_548.89);
    }

    #[test]
    fn c_to_f_works() {
        assert_eq!(celsius_to_fahrenheit(0.0), 32.0);
        assert_eq!(celsius_to_fahrenheit(100.0), 212.0);
        assert_eq!(celsius_to_fahrenheit(1000.0), 1832.0);
        assert_eq!(celsius_to_fahrenheit(5000.0), 9032.0);
        assert_eq!(celsius_to_fahrenheit(69420.0), 124_988.0);
    }

    #[test]
    fn c_to_k_works() {
        assert_eq!(celsius_to_kelvin(0.0), 273.15);
        assert_eq!(celsius_to_kelvin(100.0), 373.15);
        assert_eq!(celsius_to_kelvin(1000.0), 1273.15);
        assert_eq!(celsius_to_kelvin(5000.0), 5273.15);
        assert_eq!(celsius_to_kelvin(69420.0), 69_693.15);
    }

    #[test]
    fn k_to_c_works() {
        assert_eq!(kelvin_to_celsius(0.0), -273.15);
        assert_eq!(kelvin_to_celsius(100.0), -173.15);
        assert_eq!(kelvin_to_celsius(1000.0), 726.85);
        assert_eq!(kelvin_to_celsius(5000.0), 4726.85);
        assert_eq!(kelvin_to_celsius(69420.0), 69_146.85);
    }

    #[test]
    fn f_to_k_works() {
        assert_eq!(fahrenheit_to_kelvin(0.0), 255.372_22);
        assert_eq!(fahrenheit_to_kelvin(100.0), 310.927_76);
        assert_eq!(fahrenheit_to_kelvin(1000.0), 810.927_73);
        assert_eq!(fahrenheit_to_kelvin(5000.0), 3033.15);
        assert_eq!(fahrenheit_to_kelvin(69420.0), 38_822.04);
    }

    #[test]
    fn k_to_f_works() {
        assert_eq!(kelvin_to_fahrenheit(0.0), -459.669_98);
        assert_eq!(kelvin_to_fahrenheit(100.0), -279.669_98);
        assert_eq!(kelvin_to_fahrenheit(1000.0), 1340.33);
        assert_eq!(kelvin_to_fahrenheit(5000.0), 8540.33);
        assert_eq!(kelvin_to_fahrenheit(69420.0), 124_496.33);
    }
}