-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
NAUM-6 Add v2::ToFraction
- Loading branch information
Showing
17 changed files
with
427 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
mod convert; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
use crate::{unit, v2::convert::ToFraction, Measurement, Unit}; | ||
|
||
impl ToFraction<Self, Option<Unit>> for Measurement { | ||
fn to_fraction(&self) -> (Self, Option<Unit>) { | ||
let unit_parts = self.unit.to_fraction(); | ||
|
||
( | ||
Self { | ||
value: self.value, | ||
unit: unit_parts.0.unwrap_or(unit::UNITY), | ||
}, | ||
unit_parts.1, | ||
) | ||
} | ||
|
||
fn to_numerator(&self) -> Self { | ||
Self { | ||
value: self.value, | ||
unit: self.unit.to_numerator().unwrap_or(unit::UNITY), | ||
} | ||
} | ||
|
||
fn to_denominator(&self) -> Option<Unit> { | ||
self.unit.to_denominator() | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::{ | ||
testing::const_units::{GRAM_METER, METER, METER_PER_SECOND, PER_SECOND, SECOND}, | ||
unit::UNITY, | ||
Measurement, | ||
}; | ||
|
||
use super::*; | ||
|
||
macro_rules! validate_fraction_parts { | ||
( | ||
$measurement:expr, | ||
expected_numerator => $expected_numerator:expr, | ||
expected_denominator => $expected_denominator:expr | ||
) => { | ||
let fraction = $measurement.to_fraction(); | ||
assert_eq!(&fraction.0, &$expected_numerator); | ||
assert_eq!(fraction.1, $expected_denominator); | ||
|
||
let numerator = $measurement.to_numerator(); | ||
assert_eq!(numerator, $expected_numerator); | ||
|
||
let denominator = $measurement.to_denominator(); | ||
assert_eq!(denominator, $expected_denominator); | ||
}; | ||
} | ||
|
||
#[test] | ||
fn validate_one_numerator_term() { | ||
let measurement = Measurement::new(42.0, METER); | ||
|
||
validate_fraction_parts!( | ||
measurement, | ||
expected_numerator => measurement, | ||
expected_denominator => None | ||
); | ||
} | ||
|
||
#[test] | ||
fn validate_two_numerator_terms() { | ||
let measurement = Measurement::new(42.0, GRAM_METER); | ||
|
||
validate_fraction_parts!( | ||
measurement, | ||
expected_numerator => measurement, | ||
expected_denominator => None | ||
); | ||
} | ||
|
||
#[test] | ||
fn validate_one_numerator_term_one_denominator_term() { | ||
let measurement = Measurement::new(42.0, METER_PER_SECOND); | ||
|
||
validate_fraction_parts!( | ||
measurement, | ||
expected_numerator => Measurement::new(42.0, METER), | ||
expected_denominator => Some(SECOND) | ||
); | ||
} | ||
|
||
#[test] | ||
fn validate_one_denominator_term() { | ||
let measurement = Measurement::new(42.0, PER_SECOND); | ||
|
||
validate_fraction_parts!( | ||
measurement, | ||
expected_numerator => Measurement::new(42.0, UNITY), | ||
expected_denominator => Some(SECOND) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
pub(crate) mod const_units { | ||
use std::borrow::Cow; | ||
|
||
use crate::{Atom, Prefix, Term, Unit}; | ||
|
||
pub(crate) const METER: Unit = Unit { | ||
terms: Cow::Borrowed(&[Term { | ||
factor: None, | ||
prefix: None, | ||
atom: Some(Atom::Meter), | ||
exponent: None, | ||
annotation: None, | ||
}]), | ||
}; | ||
|
||
pub(crate) const KILOMETER: Unit = Unit { | ||
terms: Cow::Borrowed(&[Term { | ||
factor: None, | ||
prefix: Some(Prefix::Kilo), | ||
atom: Some(Atom::Meter), | ||
exponent: None, | ||
annotation: None, | ||
}]), | ||
}; | ||
|
||
pub(crate) const GRAM_METER: Unit = Unit { | ||
terms: Cow::Borrowed(&[ | ||
Term { | ||
factor: None, | ||
prefix: None, | ||
atom: Some(Atom::Gram), | ||
exponent: None, | ||
annotation: None, | ||
}, | ||
Term { | ||
factor: None, | ||
prefix: None, | ||
atom: Some(Atom::Meter), | ||
exponent: None, | ||
annotation: None, | ||
}, | ||
]), | ||
}; | ||
|
||
pub(crate) const PER_GRAM_METER: Unit = Unit { | ||
terms: Cow::Borrowed(&[ | ||
Term { | ||
factor: None, | ||
prefix: None, | ||
atom: Some(Atom::Gram), | ||
exponent: Some(-1), | ||
annotation: None, | ||
}, | ||
Term { | ||
factor: None, | ||
prefix: None, | ||
atom: Some(Atom::Meter), | ||
exponent: Some(-1), | ||
annotation: None, | ||
}, | ||
]), | ||
}; | ||
|
||
pub(crate) const SECOND: Unit = Unit { | ||
terms: Cow::Borrowed(&[Term { | ||
factor: None, | ||
prefix: None, | ||
atom: Some(Atom::Second), | ||
exponent: None, | ||
annotation: None, | ||
}]), | ||
}; | ||
|
||
pub(crate) const PER_SECOND: Unit = Unit { | ||
terms: Cow::Borrowed(&[Term { | ||
factor: None, | ||
prefix: None, | ||
atom: Some(Atom::Second), | ||
exponent: Some(-1), | ||
annotation: None, | ||
}]), | ||
}; | ||
|
||
pub(crate) const METER_PER_SECOND: Unit = Unit { | ||
terms: Cow::Borrowed(&[ | ||
Term { | ||
factor: None, | ||
prefix: None, | ||
atom: Some(Atom::Meter), | ||
exponent: None, | ||
annotation: None, | ||
}, | ||
Term { | ||
factor: None, | ||
prefix: None, | ||
atom: Some(Atom::Second), | ||
exponent: Some(-1), | ||
annotation: None, | ||
}, | ||
]), | ||
}; | ||
|
||
pub(crate) const ACRE: Unit = Unit { | ||
terms: Cow::Borrowed(&[Term { | ||
factor: None, | ||
prefix: None, | ||
atom: Some(Atom::AcreUS), | ||
exponent: None, | ||
annotation: None, | ||
}]), | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.