Skip to main content

rex_db/models/
others.rs

1use chrono::NaiveDateTime;
2use rex_shared::models::Cent;
3use std::fmt::{self, Display};
4use strum_macros::{Display, EnumIter};
5
6#[derive(Clone, Debug, Copy, Display, EnumIter)]
7pub enum TxType {
8    #[strum(to_string = "Income")]
9    Income,
10    #[strum(to_string = "Expense")]
11    Expense,
12    #[strum(to_string = "Transfer")]
13    Transfer,
14    #[strum(to_string = "Borrow")]
15    Borrow,
16    #[strum(to_string = "Lend")]
17    Lend,
18    #[strum(to_string = "Borrow Repay")]
19    BorrowRepay,
20    #[strum(to_string = "Lend Repay")]
21    LendRepay,
22}
23
24#[derive(Clone, Debug, Copy, Eq, PartialEq)]
25pub enum FetchNature {
26    Monthly,
27    Yearly,
28    All,
29}
30
31#[derive(Clone, Copy)]
32pub enum DateNature {
33    Exact(NaiveDateTime),
34    ByMonth {
35        start_date: NaiveDateTime,
36        end_date: NaiveDateTime,
37    },
38    ByYear {
39        start_date: NaiveDateTime,
40        end_date: NaiveDateTime,
41    },
42}
43
44#[derive(Clone, Debug, Copy)]
45pub enum AmountNature {
46    Exact(Cent),
47    MoreThan(Cent),
48    MoreThanEqual(Cent),
49    LessThan(Cent),
50    LessThanEqual(Cent),
51}
52
53impl AmountNature {
54    #[must_use]
55    pub fn extract(&self) -> Cent {
56        let i = match self {
57            AmountNature::Exact(i)
58            | AmountNature::MoreThan(i)
59            | AmountNature::MoreThanEqual(i)
60            | AmountNature::LessThan(i)
61            | AmountNature::LessThanEqual(i) => i,
62        };
63
64        *i
65    }
66
67    #[must_use]
68    pub fn to_type(&self) -> AmountType {
69        match self {
70            AmountNature::Exact(_) => AmountType::Exact,
71            AmountNature::MoreThan(_) => AmountType::MoreThan,
72            AmountNature::MoreThanEqual(_) => AmountType::MoreThanEqual,
73            AmountNature::LessThan(_) => AmountType::LessThan,
74            AmountNature::LessThanEqual(_) => AmountType::LessThanEqual,
75        }
76    }
77
78    #[must_use]
79    pub fn from_type(t: AmountType, amount: Cent) -> Self {
80        match t {
81            AmountType::Exact => AmountNature::Exact(amount),
82            AmountType::MoreThan => AmountNature::MoreThan(amount),
83            AmountType::MoreThanEqual => AmountNature::MoreThanEqual(amount),
84            AmountType::LessThan => AmountNature::LessThan(amount),
85            AmountType::LessThanEqual => AmountNature::LessThanEqual(amount),
86        }
87    }
88}
89
90impl Display for AmountNature {
91    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
92        let a = match self {
93            AmountNature::Exact(v) => format!("{}", v.dollar()),
94            AmountNature::MoreThan(v) => format!(">{}", v.dollar()),
95            AmountNature::MoreThanEqual(v) => format!(">={}", v.dollar()),
96            AmountNature::LessThan(v) => format!("<{}", v.dollar()),
97            AmountNature::LessThanEqual(v) => format!("<={}", v.dollar()),
98        };
99        write!(f, "{a}")
100    }
101}
102
103#[derive(Copy, Clone)]
104pub enum AmountType {
105    Exact,
106    MoreThan,
107    MoreThanEqual,
108    LessThan,
109    LessThanEqual,
110}
111
112impl From<AmountType> for String {
113    fn from(value: AmountType) -> Self {
114        match value {
115            AmountType::Exact => "exact".to_string(),
116            AmountType::MoreThan => "more_than".to_string(),
117            AmountType::MoreThanEqual => "more_than_equal".to_string(),
118            AmountType::LessThan => "less_than".to_string(),
119            AmountType::LessThanEqual => "less_than_equal".to_string(),
120        }
121    }
122}
123
124impl From<&str> for AmountType {
125    fn from(value: &str) -> Self {
126        match value {
127            "exact" => AmountType::Exact,
128            "more_than" => AmountType::MoreThan,
129            "more_than_equal" => AmountType::MoreThanEqual,
130            "less_than" => AmountType::LessThan,
131            "less_than_equal" => AmountType::LessThanEqual,
132            other => panic!("Invalid AmountType string: {other}"),
133        }
134    }
135}
136
137#[derive(Clone, Debug, Copy)]
138pub enum ActivityNature {
139    AddTx,
140    EditTx,
141    DeleteTx,
142    SearchTx,
143    PositionSwap,
144}
145
146impl From<&str> for ActivityNature {
147    fn from(s: &str) -> Self {
148        match s {
149            "add_tx" => ActivityNature::AddTx,
150            "edit_tx" => ActivityNature::EditTx,
151            "delete_tx" => ActivityNature::DeleteTx,
152            "search_tx" => ActivityNature::SearchTx,
153            "position_swap" => ActivityNature::PositionSwap,
154            other => panic!("Invalid TxType string: {other}"),
155        }
156    }
157}
158
159impl From<ActivityNature> for String {
160    fn from(a: ActivityNature) -> Self {
161        match a {
162            ActivityNature::AddTx => "add_tx".to_string(),
163            ActivityNature::EditTx => "edit_tx".to_string(),
164            ActivityNature::DeleteTx => "delete_tx".to_string(),
165            ActivityNature::SearchTx => "search_tx".to_string(),
166            ActivityNature::PositionSwap => "position_swap".to_string(),
167        }
168    }
169}
170
171impl Display for ActivityNature {
172    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> std::fmt::Result {
173        match self {
174            ActivityNature::AddTx => write!(f, "Add Transaction"),
175            ActivityNature::EditTx => write!(f, "Edit Transaction"),
176            ActivityNature::DeleteTx => write!(f, "Delete Transaction"),
177            ActivityNature::SearchTx => write!(f, "Search Transaction"),
178            ActivityNature::PositionSwap => write!(f, "Position Swap"),
179        }
180    }
181}
182
183impl From<&str> for TxType {
184    fn from(s: &str) -> Self {
185        match s {
186            "Income" => TxType::Income,
187            "Expense" => TxType::Expense,
188            "Transfer" => TxType::Transfer,
189            "Borrow" => TxType::Borrow,
190            "Lend" => TxType::Lend,
191            "Borrow Repay" => TxType::BorrowRepay,
192            "Lend Repay" => TxType::LendRepay,
193            other => panic!("Invalid TxType string: {other}"),
194        }
195    }
196}