diff --git a/src/lib.rs b/src/lib.rs index 8f787ba..b5eda2c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -14,14 +14,14 @@ pub static MONGO_CLIENT: OnceCell = OnceCell::const_new(); #[macro_export] macro_rules! get_mongo { () => { - if let Some(client) = crate::MONGO_CLIENT.get() { + if let Some(client) = $crate::MONGO_CLIENT.get() { client } else { let client = mongodb::Client::with_uri_str(&std::env::var("DB_URI").unwrap()) .await .unwrap(); - crate::MONGO_CLIENT.set(client).unwrap(); - crate::MONGO_CLIENT.get().unwrap() + $crate::MONGO_CLIENT.set(client).unwrap(); + $crate::MONGO_CLIENT.get().unwrap() } }; } @@ -31,7 +31,7 @@ macro_rules! get_mongo { macro_rules! col { ($db:expr, $col:expr) => { $db.database(&std::env::var("DB").unwrap()) - .collection::($col) + .collection::<$crate::mongodb::bson::Document>($col) }; } @@ -54,6 +54,6 @@ macro_rules! collect_results { #[macro_export] macro_rules! id_of { ($id:expr) => { - mongodb::bson::doc! { "_id": $id} + $crate::mongodb::bson::doc! { "_id": $id} }; } diff --git a/src/model/historic.rs b/src/model/historic.rs index 9b8c2bf..85d8286 100644 --- a/src/model/historic.rs +++ b/src/model/historic.rs @@ -11,7 +11,7 @@ pub struct Historic { pub changes: HashMap, } -impl Historic { +impl Historic { /// Create a new value with tracked history pub fn new(value: T) -> Historic { let mut changes = HashMap::new(); @@ -22,16 +22,27 @@ impl Historic { changes, } } +} +impl Historic { /// Update the value. The change will be recorded. + /// Will record a change even if the value is the same as the current one. pub fn update(&mut self, value: T) { + self.changes + .insert(chrono::Utc::now().to_rfc3339(), value.clone()); + self.current = value; + } +} + +impl Historic { + /// Update the value. The change will be recorded. + /// If the value is equal to the current one, no update will take place. + pub fn update_unique(&mut self, value: T) { if self.current == value { return; } - self.changes - .insert(chrono::Utc::now().to_rfc3339(), value.clone()); - self.current = value; + self.update(value); } }