update
This commit is contained in:
parent
f78cf419fc
commit
ebefb97d9c
2 changed files with 20 additions and 9 deletions
10
src/lib.rs
10
src/lib.rs
|
@ -14,14 +14,14 @@ pub static MONGO_CLIENT: OnceCell<mongodb::Client> = 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::<mongodb::bson::Document>($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}
|
||||
};
|
||||
}
|
||||
|
|
|
@ -11,7 +11,7 @@ pub struct Historic<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
|
||||
pub fn new(value: T) -> Historic<T> {
|
||||
let mut changes = HashMap::new();
|
||||
|
@ -22,16 +22,27 @@ impl<T: Clone + std::cmp::PartialEq> Historic<T> {
|
|||
changes,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Clone> Historic<T> {
|
||||
/// 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<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 {
|
||||
return;
|
||||
}
|
||||
|
||||
self.changes
|
||||
.insert(chrono::Utc::now().to_rfc3339(), value.clone());
|
||||
self.current = value;
|
||||
self.update(value);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue