use std::fmt;
use num_bigint::Sign;
use crate::{
ext::{
business::{Balance, Money, MoneyRef, Value},
shop::Gtin,
},
runtime::model::{Concept, Entity, Object},
};
impl fmt::Display for Gtin {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "gtin {} ({} digits)", self.get(), self.digits())
}
}
impl fmt::Display for Money {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
MoneyRef::from(self).fmt(f)
}
}
impl fmt::Display for MoneyRef<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let whole = self.0 / 100u8;
let frac = self.0 % 100u8;
write!(f, "{whole}.{frac:02} €")
}
}
impl fmt::Display for Value {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let sign = match self.0.sign() {
Sign::Minus => "-",
_ => "",
};
let mag = Money(self.0.magnitude().clone());
write!(f, "{sign}{mag}")
}
}
impl fmt::Display for Balance {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let magnitude = self.amount().abs();
let standing = match self.amount().sign() {
Sign::Minus => format!("owes {magnitude} to"),
Sign::NoSign => "is even with".to_string(),
Sign::Plus => format!("is owed {magnitude} by"),
};
write!(
f,
"{} {} {}",
self.between().source(),
standing,
self.between().target(),
)
}
}
impl fmt::Display for Entity {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "entity {}", self.name())
}
}
impl fmt::Display for Concept {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "concept {}", self.name())
}
}
impl fmt::Display for Object {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "object ")?;
if let Some(name) = self.name() {
write!(f, "{name}")?;
} else {
write!(f, "<anonymous>")?;
}
if let Some(parent) = self.parent() {
write!(f, " instance of {parent}")?;
}
Ok(())
}
}