From d220cec6d2691e80e86a5d40be3c8ee0266f10df Mon Sep 17 00:00:00 2001 From: Jonathan Cornaz Date: Sun, 9 Jul 2023 11:22:13 +0200 Subject: [PATCH] start implementing `PartialOrd` for `Directive` --- CHANGELOG.md | 1 + src/lib.rs | 10 ++++++++++ tests/util_spec.rs | 13 +++++++++++++ 3 files changed, 24 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 72d7c6d..66729fe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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` where `D: Decimal` +* implement `PartialOrd` for `Directive` ## [2.0.0-beta.2] - 2023-07-08 diff --git a/src/lib.rs b/src/lib.rs index 704ee44..a453c02 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -43,6 +43,7 @@ //! # Ok(()) } //! ``` +use std::cmp::Ordering; use std::{collections::HashSet, fs::File, io::Read, path::PathBuf, str::FromStr}; use nom::{ @@ -292,6 +293,15 @@ impl FromStr for Directive { } } +impl PartialOrd for Directive +where + Directive: PartialEq, +{ + fn partial_cmp(&self, other: &Self) -> Option { + Some(self.date.cmp(&other.date)) + } +} + /// Directive specific content #[allow(missing_docs)] #[derive(Debug, Clone, PartialEq)] diff --git a/tests/util_spec.rs b/tests/util_spec.rs index 12c95d8..ff3a0ba 100644 --- a/tests/util_spec.rs +++ b/tests/util_spec.rs @@ -94,3 +94,16 @@ fn directive_from_str( let from_str: Directive = 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, #[case] bigger: Directive) { + assert!(smaller < bigger); + assert!(smaller <= bigger); + assert_eq!(smaller, smaller.clone()); + assert_eq!(bigger, bigger.clone()); + assert!(bigger > smaller); + assert!(bigger >= smaller); +}