newnit/
area.rs

1//! Units of area.
2//!
3//! This module contains predefined newtypes for units of area as defined in
4//! the following systems:
5//! - [`imperial`] - British Imperial units
6//! - [`metric`] - International System of Units (SI)
7
8use crate::Unit;
9use crate::length::Length;
10use crate::length::metric::Meter;
11use crate::volume::metric::CubicMeter;
12
13pub mod imperial;
14pub mod metric;
15
16pub trait Area: Unit {
17    /// Multiply a unit of area with a unit of length
18    fn multiply_length(&self, rhs: &dyn Length) -> CubicMeter {
19        CubicMeter(self.to_base() * rhs.to_base())
20    }
21
22    /// Divide a unit of area by a unit of length.
23    fn divide_length(&self, rhs: &dyn Length) -> Meter {
24        Meter(self.to_base() / rhs.to_base())
25    }
26}
27
28#[cfg(test)]
29mod test {
30    use super::*;
31
32    #[test]
33    fn multiply_with_length() {
34        let area = metric::SquareMeter(2.0);
35        let length = crate::length::imperial::Foot(3.0);
36
37        let volume = area.multiply_length(&length);
38        assert!((volume.to_value() - 1.8288).abs() < 1e-5);
39    }
40
41    #[test]
42    fn divide_by_length() {
43        let area = metric::SquareMeter(2.0);
44        let length = crate::length::metric::Meter(4.0);
45
46        let result_length = area.divide_length(&length);
47        assert!((result_length.to_value() - 0.5).abs() < 1e-5);
48    }
49}