Skip to content

Commit

Permalink
disallow duplicate history
Browse files Browse the repository at this point in the history
  • Loading branch information
zahash committed Oct 15, 2023
1 parent 2a6e0dd commit d74bf51
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
7 changes: 4 additions & 3 deletions help.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ cargo publish


TODO:
history
import
reveal history
is =

sensitize del output
find a way to copy history attrs to clipboard. Maybe by providing index.

is =
1 change: 1 addition & 0 deletions src/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,7 @@ mod tests {
"set sus user = 'benito sussolini' sensitive pass = amogus"
);
eval!(&mut store, "set sus user = 'pablo susscobar'");
eval!(&mut store, "set sus user = 'pablo susscobar'");
eval!(&mut store, "del sus user");
eval!(&mut store, "set sus pass = potatus");
eval!(&mut store, "set sus note = 'this is the latest'");
Expand Down
26 changes: 19 additions & 7 deletions src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,7 @@ impl<'text> Store {
});
}

record
.history
.push(HistoryEntry::new(record.fields.clone()));
record.update_history();
}

pub fn history(&self, name: &str) -> Vec<HistoryEntry> {
Expand All @@ -85,9 +83,7 @@ impl<'text> Store {
pub fn remove_attrs(&mut self, name: &str, attrs: &[&str]) -> Option<Record> {
if let Some(record) = self.records.iter_mut().find(|r| r.name == name) {
record.fields.retain(|f| !attrs.contains(&f.attr.as_str()));
record
.history
.push(HistoryEntry::new(record.fields.clone()));
record.update_history();
return Some(record.clone());
}
None
Expand All @@ -104,7 +100,23 @@ pub struct Record {
pub history: Vec<HistoryEntry>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
impl Record {
pub fn update_history(&mut self) {
self.history.sort_by(|h1, h2| h1.datetime.cmp(&h2.datetime));
match self.history.last_mut() {
Some(history) => {
history.fields.sort_by(|f1, f2| f1.attr.cmp(&f2.attr));
self.fields.sort_by(|f1, f2| f1.attr.cmp(&f2.attr));
if history.fields != self.fields {
self.history.push(HistoryEntry::new(self.fields.clone()))
}
}
None => self.history.push(HistoryEntry::new(self.fields.clone())),
}
}
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct Field {
pub attr: String,
pub value: String,
Expand Down

0 comments on commit d74bf51

Please sign in to comment.