Skip to content

Commit

Permalink
start implementing PartialOrd for Directive
Browse files Browse the repository at this point in the history
  • Loading branch information
jcornaz committed Jul 9, 2023
1 parent 6d1fb42 commit d220cec
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
* Support booking method in open account directive
* implement `std::error::Error` for `ConversionError`
* implement `FromStr` for `Directive<D>` where `D: Decimal`
* implement `PartialOrd` for `Directive<D>`


## [2.0.0-beta.2] - 2023-07-08
Expand Down
10 changes: 10 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
//! # Ok(()) }
//! ```

use std::cmp::Ordering;
use std::{collections::HashSet, fs::File, io::Read, path::PathBuf, str::FromStr};

use nom::{
Expand Down Expand Up @@ -292,6 +293,15 @@ impl<D: Decimal> FromStr for Directive<D> {
}
}

impl<D> PartialOrd for Directive<D>
where
Directive<D>: PartialEq,
{
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.date.cmp(&other.date))
}
}

/// Directive specific content
#[allow(missing_docs)]
#[derive(Debug, Clone, PartialEq)]
Expand Down
13 changes: 13 additions & 0 deletions tests/util_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,16 @@ fn directive_from_str(
let from_str: Directive<f64> = input.parse().unwrap();
assert_eq!(from_file, from_str);
}

#[rstest]
#[case("2023-07-09 open Assets:Cash", "2023-07-10 open Assets:Cash")]
#[case("2023-07-09 open Assets:Cash", "2023-08-08 open Assets:Cash")]
#[case("2023-07-09 open Assets:Cash", "2024-06-08 open Assets:Cash")]
fn directive_ord(#[case] smaller: Directive<f64>, #[case] bigger: Directive<f64>) {
assert!(smaller < bigger);
assert!(smaller <= bigger);
assert_eq!(smaller, smaller.clone());
assert_eq!(bigger, bigger.clone());
assert!(bigger > smaller);
assert!(bigger >= smaller);
}

0 comments on commit d220cec

Please sign in to comment.