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

@ -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);
}
}