From 1b6ca363d3b3479e04aff3e9fb2a141404dbb282 Mon Sep 17 00:00:00 2001 From: Kevin Hoerr Date: Thu, 23 Apr 2026 17:19:51 -0400 Subject: records.rs: Add try_from for enums --- src/app/store/records.rs | 54 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) (limited to 'src/app/store/records.rs') diff --git a/src/app/store/records.rs b/src/app/store/records.rs index 4e85fc1..6971b23 100644 --- a/src/app/store/records.rs +++ b/src/app/store/records.rs @@ -1,5 +1,7 @@ use bigdecimal::BigDecimal; +use super::ContextError; + type DateTime = chrono::DateTime; /// Represents an account that holds equity. An account may hold equity with @@ -19,12 +21,26 @@ pub struct FiduciaryAccount { /// An atomic enum to describe for what element of equity a symbol tracks /// (stock, fund, etc.) +#[derive(Debug, PartialEq)] pub enum EquityType { STOCK, ETF, CRYPTO, } +impl TryFrom<&str> for EquityType { + type Error = ContextError; + + fn try_from(value: &str) -> Result { + match value { + "STOCK" => Ok(EquityType::STOCK), + "ETF" => Ok(EquityType::ETF), + "CRYPTO" => Ok(EquityType::CRYPTO), + _ => Err(ContextError::EnumTranslationError(value.into())), + } + } +} + impl std::fmt::Display for EquityType { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!( @@ -54,11 +70,24 @@ pub struct EquitySymbol { } /// An atomic enum to describe the basic action of an equity change event +#[derive(Debug, PartialEq)] pub enum ChangeEventType { BUY, SELL, } +impl TryFrom<&str> for ChangeEventType { + type Error = ContextError; + + fn try_from(value: &str) -> Result { + match value { + "BUY" => Ok(ChangeEventType::BUY), + "SELL" => Ok(ChangeEventType::SELL), + _ => Err(ContextError::EnumTranslationError(value.into())), + } + } +} + impl ChangeEventType { /// Relativizes the effect of a change event for numeration pub fn score(&self) -> i8 { @@ -127,6 +156,19 @@ mod tests { num.with_scale_round(2_i64, bigdecimal::RoundingMode::HalfEven) } + #[test] + fn equity_type_try_from() { + assert_eq!(EquityType::try_from("STOCK").unwrap(), EquityType::STOCK); + assert_eq!(EquityType::try_from("ETF").unwrap(), EquityType::ETF); + assert_eq!(EquityType::try_from("CRYPTO").unwrap(), EquityType::CRYPTO); + + assert!(EquityType::try_from("unknown").is_err()); + match EquityType::try_from("unknown") { + Err(ContextError::EnumTranslationError(e)) => assert_eq!(e, "unknown"), + _ => assert!(false), + }; + } + #[test] fn equity_type_display() { assert_eq!(EquityType::STOCK.to_string(), "STOCK"); @@ -134,6 +176,18 @@ mod tests { assert_eq!(EquityType::CRYPTO.to_string(), "CRYPTO"); } + #[test] + fn event_type_try_from() { + assert_eq!(ChangeEventType::try_from("BUY").unwrap(), ChangeEventType::BUY); + assert_eq!(ChangeEventType::try_from("SELL").unwrap(), ChangeEventType::SELL); + + assert!(ChangeEventType::try_from("unknown").is_err()); + match ChangeEventType::try_from("unknown") { + Err(ContextError::EnumTranslationError(e)) => assert_eq!(e, "unknown"), + _ => assert!(false), + }; + } + #[test] fn event_type_display() { assert_eq!(ChangeEventType::BUY.to_string(), "BUY"); -- cgit v1.3