From f3d428dcbedeecbec367f667057c2ef4a85abba7 Mon Sep 17 00:00:00 2001 From: Marcel Enguehard Date: Thu, 30 Jul 2026 15:09:34 +0200 Subject: [PATCH] Add Display for Euros MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Renders as "123.45 €" — dot separator and a plain space, i.e. American style. French formatting (narrow no-break space, comma separator) will need a separate path since Display takes no parameters; deferred until there is a page to render. Co-Authored-By: Claude Opus 5 --- crates/immo-core/src/euros.rs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/crates/immo-core/src/euros.rs b/crates/immo-core/src/euros.rs index 87070cb..33b2109 100644 --- a/crates/immo-core/src/euros.rs +++ b/crates/immo-core/src/euros.rs @@ -1,3 +1,5 @@ +use std::fmt; + use rust_decimal::Decimal; #[derive(PartialOrd, PartialEq, Ord, Eq, Clone, Copy, Debug)] @@ -19,6 +21,12 @@ impl Euros { } } +impl fmt::Display for Euros { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{} €", self.as_decimal()) + } +} + #[cfg(test)] mod tests { use super::*; @@ -77,6 +85,25 @@ mod tests { #[test] fn from_0_cents_to_decimal_stays_zero() { let euros = Euros::from_cents(0); + assert_eq!(euros.as_decimal().scale(), 2); assert_eq!(euros.as_decimal(), Decimal::ZERO); } + + #[test] + fn standard_euros_prints_correctly() { + let euros = Euros::from_cents(123_45); + assert_eq!(format!("{}", euros), "123.45 €"); + } + + #[test] + fn euros_without_cents_prints_with_trailing_zeros() { + let euros = Euros::from_cents(123_00); + assert_eq!(format!("{}", euros), "123.00 €"); + } + + #[test] + fn euros_with_negative_amount_print_with_minus() { + let euros = Euros::from_cents(-123_00); + assert_eq!(format!("{}", euros), "-123.00 €"); + } }