aboutsummaryrefslogtreecommitdiff
path: root/src/app/store/records.rs
diff options
context:
space:
mode:
authorKevin Hoerr <kjhoerr@submelon.dev>2026-04-23 17:19:51 -0400
committerKevin Hoerr <kjhoerr@submelon.dev>2026-04-23 17:19:51 -0400
commit1b6ca363d3b3479e04aff3e9fb2a141404dbb282 (patch)
tree5f0dabe27831ec3a7f587037dc8866f62a9816ee /src/app/store/records.rs
parentad5e1f1c89c4e3f42aa09a0e8d7018a78bc10474 (diff)
downloadequity-tracker-1b6ca363d3b3479e04aff3e9fb2a141404dbb282.tar.gz
equity-tracker-1b6ca363d3b3479e04aff3e9fb2a141404dbb282.tar.bz2
equity-tracker-1b6ca363d3b3479e04aff3e9fb2a141404dbb282.zip
records.rs: Add try_from for enums
Diffstat (limited to 'src/app/store/records.rs')
-rw-r--r--src/app/store/records.rs54
1 files changed, 54 insertions, 0 deletions
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<chrono::Utc>;
/// 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<Self, Self::Error> {
+ 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<Self, Self::Error> {
+ 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 {
@@ -128,6 +157,19 @@ mod tests {
}
#[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");
assert_eq!(EquityType::ETF.to_string(), "ETF");
@@ -135,6 +177,18 @@ mod tests {
}
#[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");
assert_eq!(ChangeEventType::SELL.to_string(), "SELL");