-
Notifications
You must be signed in to change notification settings - Fork 26
/
entry.rs
78 lines (69 loc) · 2.21 KB
/
entry.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
use keepass::Value;
use std::collections::HashMap;
use crate::diff::field::Field;
use crate::diff::{Diff, DiffResult, DiffResultFormat};
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Entry {
pub fields: HashMap<String, Field>,
use_verbose: bool,
}
impl Entry {
pub fn from_keepass(e: &keepass::Entry, use_verbose: bool) -> Self {
// username, password, etc. are just fields
let fields = e
.fields
.iter()
.map(|(k, v)| {
(
k.to_owned(),
Field {
name: k.to_owned(),
value: match v {
Value::Bytes(b) => String::from_utf8_lossy(b).to_string(),
Value::Unprotected(v) => v.to_owned(),
Value::Protected(p) => String::from_utf8(p.unsecure().to_owned())
.unwrap()
.to_owned(),
},
use_verbose,
},
)
})
.collect();
Entry {
fields,
use_verbose,
}
}
}
impl Diff for Entry {
fn diff<'a>(&'a self, other: &'a Self) -> DiffResult<'a, Self> {
let (has_differences, field_differences) =
crate::diff::diff_hashmap(&self.fields, &other.fields);
if has_differences {
let mut inner_differences: Vec<Box<dyn DiffResultFormat>> = Vec::new();
for dr in field_differences {
inner_differences.push(Box::new(dr))
}
DiffResult::InnerDifferences {
left: self,
right: other,
inner_differences,
}
} else {
DiffResult::Identical {
left: self,
right: other,
}
}
}
}
impl std::fmt::Display for Entry {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
if self.use_verbose {
write!(f, "Entry '{}'", self.fields.get("Title").unwrap().value)
} else {
write!(f, "{}", self.fields.get("Title").unwrap().value)
}
}
}