This commit is contained in:
JMARyA 2024-07-23 15:31:04 +02:00
parent f78cf419fc
commit ebefb97d9c
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263
2 changed files with 20 additions and 9 deletions

View file

@ -14,14 +14,14 @@ pub static MONGO_CLIENT: OnceCell<mongodb::Client> = OnceCell::const_new();
#[macro_export] #[macro_export]
macro_rules! get_mongo { macro_rules! get_mongo {
() => { () => {
if let Some(client) = crate::MONGO_CLIENT.get() { if let Some(client) = $crate::MONGO_CLIENT.get() {
client client
} else { } else {
let client = mongodb::Client::with_uri_str(&std::env::var("DB_URI").unwrap()) let client = mongodb::Client::with_uri_str(&std::env::var("DB_URI").unwrap())
.await .await
.unwrap(); .unwrap();
crate::MONGO_CLIENT.set(client).unwrap(); $crate::MONGO_CLIENT.set(client).unwrap();
crate::MONGO_CLIENT.get().unwrap() $crate::MONGO_CLIENT.get().unwrap()
} }
}; };
} }
@ -31,7 +31,7 @@ macro_rules! get_mongo {
macro_rules! col { macro_rules! col {
($db:expr, $col:expr) => { ($db:expr, $col:expr) => {
$db.database(&std::env::var("DB").unwrap()) $db.database(&std::env::var("DB").unwrap())
.collection::<mongodb::bson::Document>($col) .collection::<$crate::mongodb::bson::Document>($col)
}; };
} }
@ -54,6 +54,6 @@ macro_rules! collect_results {
#[macro_export] #[macro_export]
macro_rules! id_of { macro_rules! id_of {
($id:expr) => { ($id:expr) => {
mongodb::bson::doc! { "_id": $id} $crate::mongodb::bson::doc! { "_id": $id}
}; };
} }

View file

@ -11,7 +11,7 @@ pub struct Historic<T> {
pub changes: HashMap<String, T>, pub changes: HashMap<String, T>,
} }
impl<T: Clone + std::cmp::PartialEq> Historic<T> { impl<T: Clone> Historic<T> {
/// Create a new value with tracked history /// Create a new value with tracked history
pub fn new(value: T) -> Historic<T> { pub fn new(value: T) -> Historic<T> {
let mut changes = HashMap::new(); let mut changes = HashMap::new();
@ -22,16 +22,27 @@ impl<T: Clone + std::cmp::PartialEq> Historic<T> {
changes, changes,
} }
} }
}
impl<T: Clone> Historic<T> {
/// Update the value. The change will be recorded. /// 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) { pub fn update(&mut self, value: T) {
self.changes
.insert(chrono::Utc::now().to_rfc3339(), value.clone());
self.current = value;
}
}
impl<T: Clone + std::cmp::PartialEq> Historic<T> {
/// 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 { if self.current == value {
return; return;
} }
self.changes self.update(value);
.insert(chrono::Utc::now().to_rfc3339(), value.clone());
self.current = value;
} }
} }