Skip to content

Commit

Permalink
NAUM-4 impl PartialOrd for Term
Browse files Browse the repository at this point in the history
  • Loading branch information
turboladen committed May 10, 2024
1 parent e469d7e commit c6c7f3d
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 4 deletions.
1 change: 1 addition & 0 deletions crates/api/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
- NAUM-4: Derive `PartialOrd` for `Composition`
- NAUM-4: `impl From<Dimension> for Composition`
- NAUM-4: Derive `Hash` for `Property`
- NAUM-4: `impl PartialOrd for Term`
- Added `Unit::into_terms()` for cases where you only need the `Term`s of the `Unit`.
- Added `unit` constant: `UNITY`
- Added `term` constants: `UNITY`, `UNITY_ARRAY`, and `UNITY_ARRAY_REF`.
Expand Down
1 change: 1 addition & 0 deletions crates/api/src/term.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mod invert;
mod is_compatible_with;
pub(crate) mod num_traits;
mod partial_eq;
mod partial_ord;
mod reducible;
mod ucum_unit;

Expand Down
6 changes: 2 additions & 4 deletions crates/api/src/term/partial_eq.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use crate::{is_compatible_with::IsCompatibleWith, ucum_unit::UcumUnit, Term};
use approx::ulps_eq;

//-----------------------------------------------------------------------------
// impl PartialEq
//-----------------------------------------------------------------------------
use crate::{is_compatible_with::IsCompatibleWith, ucum_unit::UcumUnit, Term};

/// `Term`s are `PartialEq` if
///
/// a) they are compatible
Expand Down
69 changes: 69 additions & 0 deletions crates/api/src/term/partial_ord.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
use crate::{is_compatible_with::IsCompatibleWith, ucum_unit::UcumUnit, Term};

/// `Term`s are `PartialOrd` if
///
/// a) they are compatible
/// b) their `scalar()` values are comparable
///
/// ```rust
/// use std::cmp::Ordering;
/// use wise_units::{Atom, Prefix, Term};
///
/// let lhs = Term {
/// factor: Some(1000),
/// prefix: None,
/// atom: Some(Atom::Meter),
/// exponent: None,
/// annotation: None
/// };
/// let rhs = Term {
/// factor: None,
/// prefix: Some(Prefix::Kilo),
/// atom: Some(Atom::Meter),
/// exponent: None,
/// annotation: None
/// };
/// assert_eq!(lhs.partial_cmp(&rhs), Some(Ordering::Equal));
///
/// let lhs = Term {
/// factor: None,
/// prefix: None,
/// atom: Some(Atom::Meter),
/// exponent: None,
/// annotation: None
/// };
/// let rhs = Term {
/// factor: None,
/// prefix: Some(Prefix::Kilo),
/// atom: Some(Atom::Meter),
/// exponent: None,
/// annotation: None
/// };
/// assert_eq!(lhs.partial_cmp(&rhs), Some(Ordering::Less));
///
/// let lhs = Term {
/// factor: None,
/// prefix: None,
/// atom: Some(Atom::Meter),
/// exponent: None,
/// annotation: None
/// };
/// let rhs = Term {
/// factor: None,
/// prefix: None,
/// atom: Some(Atom::Gram),
/// exponent: None,
/// annotation: None
/// };
/// assert_eq!(lhs.partial_cmp(&rhs), None);
/// ```
///
impl PartialOrd for Term {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
if !self.is_compatible_with(other) {
return None;
}

self.scalar().partial_cmp(&other.scalar())
}
}

0 comments on commit c6c7f3d

Please sign in to comment.