78 lines
1.9 KiB
Rust
78 lines
1.9 KiB
Rust
use crate::db::base::{SledModel, Storable};
|
|
use chrono::{DateTime, Duration, Utc};
|
|
use rhai::{CustomType, EvalAltResult, TypeBuilder};
|
|
use serde::{Deserialize, Serialize}; // Import Sled traits from db module
|
|
|
|
/// Currency represents a monetary value with amount and currency code
|
|
#[derive(Debug, Clone, Serialize, Deserialize, CustomType)]
|
|
pub struct Currency {
|
|
pub amount: f64,
|
|
pub currency_code: String,
|
|
}
|
|
|
|
impl Currency {
|
|
/// Create a new currency with amount and code
|
|
pub fn new(amount: f64, currency_code: String) -> Self {
|
|
Self {
|
|
amount,
|
|
currency_code,
|
|
}
|
|
}
|
|
|
|
pub fn amount(&mut self) -> f64 {
|
|
self.amount
|
|
}
|
|
}
|
|
|
|
/// Builder for Currency
|
|
#[derive(Clone, CustomType)]
|
|
pub struct CurrencyBuilder {
|
|
amount: Option<f64>,
|
|
currency_code: Option<String>,
|
|
}
|
|
|
|
impl CurrencyBuilder {
|
|
/// Create a new CurrencyBuilder with all fields set to None
|
|
pub fn new() -> Self {
|
|
Self {
|
|
amount: None,
|
|
currency_code: None,
|
|
}
|
|
}
|
|
|
|
/// Set the amount
|
|
pub fn amount(mut self, amount: f64) -> Self {
|
|
self.amount = Some(amount);
|
|
self
|
|
}
|
|
|
|
/// Set the currency code
|
|
pub fn currency_code<S: Into<String>>(mut self, currency_code: S) -> Self {
|
|
self.currency_code = Some(currency_code.into());
|
|
self
|
|
}
|
|
|
|
/// Build the Currency object
|
|
pub fn build(self) -> Result<Currency, Box<EvalAltResult>> {
|
|
Ok(Currency {
|
|
amount: self.amount.ok_or("amount is required")?,
|
|
currency_code: self.currency_code.ok_or("currency_code is required")?,
|
|
})
|
|
}
|
|
}
|
|
|
|
// Implement Storable trait (provides default dump/load)
|
|
impl Storable for Currency {}
|
|
|
|
// Implement SledModel trait
|
|
impl SledModel for Currency {
|
|
fn get_id(&self) -> String {
|
|
// Use the currency code as the ID
|
|
self.currency_code.clone()
|
|
}
|
|
|
|
fn db_prefix() -> &'static str {
|
|
"currency"
|
|
}
|
|
}
|