dinero 0.0.11

Dinero lets you express monetary values. You can perform mutations, conversions, comparisons, format them extensively, and overall make money manipulation in your application easier and safer.
Documentation
use crate::Dinero;

use super::transform_scale::transform_scale;

/// normalize scale
///
/// Normalize two Dinero objects to the highest scale.
///
/// Normalizing to a higher scale means that the internal amount value increases by orders of magnitude. **Be careful not to exceed the minimum and maximum safe integers.**
pub fn normalize_scale_tuple(a: Dinero, b: Dinero) -> (Dinero, Dinero) {
    if a.scale == b.scale {
        return (a.to_owned(), b.to_owned());
    }

    if a.scale > b.scale {
        return (a.to_owned(), transform_scale(&b, a.scale));
    }

    (transform_scale(&a, b.scale), b.to_owned())
}

#[cfg(test)]
#[cfg(not(tarpaulin_include))]
mod tests {

    use super::*;
    use crate::currencies::{EUR, USD};
    use pretty_assertions::assert_eq;

    #[test]
    fn test_normalize_scale_tuple() {
        assert_eq!(
            normalize_scale_tuple(
                Dinero::new(10, EUR, Some(2)), //
                Dinero::new(20, EUR, Some(2))
            ),
            (
                Dinero::new(10, EUR, Some(2)), //
                Dinero::new(20, EUR, Some(2))
            )
        );

        assert_eq!(
            normalize_scale_tuple(
                Dinero::new(20, USD, Some(2)), //
                Dinero::new(30, EUR, Some(3))
            ),
            (
                Dinero::new(200, USD, Some(3)), //
                Dinero::new(30, EUR, Some(3))
            )
        );

        assert_eq!(
            normalize_scale_tuple(
                Dinero::new(30, EUR, Some(3)), //
                Dinero::new(20, USD, Some(2)),
            ),
            (
                Dinero::new(30, EUR, Some(3)), //
                Dinero::new(200, USD, Some(3)),
            )
        );
    }
}