rex_db/models/
others.rs

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