keepass/db/types/
entry.rs1use std::{
2 collections::HashMap,
3 ops::{Deref, DerefMut},
4};
5
6use uuid::Uuid;
7
8use crate::{
9 Database,
10 db::{GroupId, Times, Value, fields},
11};
12
13#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
15pub struct EntryId(Uuid);
16
17impl Default for EntryId {
18 fn default() -> Self {
19 Self::new()
20 }
21}
22
23impl EntryId {
24 pub fn new() -> Self {
26 Self(Uuid::new_v4())
27 }
28
29 pub const fn from_uuid(uuid: Uuid) -> Self {
35 Self(uuid)
36 }
37
38 pub fn uuid(&self) -> Uuid {
40 self.0
41 }
42}
43
44impl From<Uuid> for EntryId {
45 fn from(uuid: Uuid) -> Self {
46 Self::from_uuid(uuid)
47 }
48}
49
50#[derive(Debug, Eq, PartialEq, Clone)]
52pub struct Entry {
53 pub(crate) id: EntryId,
55
56 pub(crate) parent: GroupId,
58
59 pub fields: HashMap<String, Value<String>>,
63
64 pub times: Times,
66}
67
68impl Entry {
69 pub(crate) fn with_id(id: EntryId, parent: GroupId) -> Self {
70 Self { id, parent, fields: HashMap::new(), times: Times::new() }
71 }
72
73 pub fn id(&self) -> EntryId {
75 self.id
76 }
77
78 pub fn get(&self, key: &str) -> Option<&str> {
80 self.fields.get(key).map(|v| v.as_str())
81 }
82
83 pub fn set(&mut self, key: impl Into<String>, value: Value<String>) {
85 self.fields.insert(key.into(), value);
86 }
87
88 pub fn set_unprotected(&mut self, key: impl Into<String>, value: impl Into<String>) {
90 self.set(key, Value::unprotected(value));
91 }
92
93 pub fn update_unprotected(&mut self, key: &str, value: &str) -> bool {
96 if self.fields.get(key).is_some_and(|current| !current.is_protected() && current.get() == value) {
97 return false;
98 }
99
100 self.set(key, Value::unprotected(value.to_owned()));
101 true
102 }
103
104 pub fn update_protected(&mut self, key: &str, value: &str) -> bool {
107 if self.fields.get(key).is_some_and(|current| current.is_protected() && current.get() == value) {
108 return false;
109 }
110
111 self.set(key, Value::protected(value.to_owned()));
112 true
113 }
114
115 pub fn set_protected(&mut self, key: impl Into<String>, value: impl Into<String>) {
117 self.set(key, Value::protected(value));
118 }
119
120 pub fn get_raw_otp_value(&self) -> Option<&str> {
122 self.get(fields::OTP)
123 }
124
125 pub fn get_title(&self) -> Option<&str> {
127 self.get(fields::TITLE)
128 }
129
130 pub fn get_username(&self) -> Option<&str> {
132 self.get(fields::USERNAME)
133 }
134
135 pub fn get_password(&self) -> Option<&str> {
137 self.get(fields::PASSWORD)
138 }
139
140 pub fn get_url(&self) -> Option<&str> {
142 self.get(fields::URL)
143 }
144}
145
146impl std::fmt::Display for EntryId {
147 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
148 write!(f, "{}", self.0)
149 }
150}
151
152pub struct EntryRef<'a> {
154 database: &'a Database,
155 id: EntryId,
156}
157
158impl EntryRef<'_> {
159 pub(crate) fn new(database: &Database, id: EntryId) -> EntryRef<'_> {
160 EntryRef { database, id }
161 }
162}
163
164impl Deref for EntryRef<'_> {
165 type Target = Entry;
166
167 #[allow(clippy::expect_used, clippy::missing_panics_doc)] fn deref(&self) -> &Self::Target {
169 self.database.entries.get(&self.id).expect("Entry not found")
171 }
172}
173
174pub struct EntryMut<'a> {
176 database: &'a mut Database,
177 id: EntryId,
178}
179
180impl EntryMut<'_> {
181 pub(crate) fn new(database: &mut Database, id: EntryId) -> EntryMut<'_> {
182 EntryMut { database, id }
183 }
184
185 #[allow(clippy::expect_used, clippy::missing_panics_doc)] pub fn remove(self) {
188 let entry = self.database.entries.remove(&self.id).expect("Entry not found");
189 self.database.group_mut(entry.parent).expect("Parent group not found").entries.shift_remove(&self.id);
190 }
191}
192
193impl Deref for EntryMut<'_> {
194 type Target = Entry;
195
196 #[allow(clippy::expect_used, clippy::missing_panics_doc)] fn deref(&self) -> &Self::Target {
198 self.database.entries.get(&self.id).expect("Entry not found")
200 }
201}
202
203impl DerefMut for EntryMut<'_> {
204 #[allow(clippy::expect_used, clippy::missing_panics_doc)] fn deref_mut(&mut self) -> &mut Self::Target {
206 self.database.entries.get_mut(&self.id).expect("Entry not found")
208 }
209}
210
211#[cfg(test)]
212#[allow(clippy::unwrap_used)]
213mod tests {
214 use super::EntryId;
215
216 #[test]
217 fn entry_id_new_generates_distinct_ids() {
218 assert_ne!(EntryId::new(), EntryId::new());
219 }
220
221 #[test]
222 fn update_fields_only_when_changed() {
223 use super::Entry;
224 use crate::db::GroupId;
225
226 let mut entry = Entry::with_id(EntryId::new(), GroupId::new());
227
228 assert!(entry.update_protected("Password", "secret"));
229 assert!(!entry.update_protected("Password", "secret"));
230 assert!(entry.fields["Password"].is_protected());
231
232 assert!(entry.update_protected("Password", "new-secret"));
233 assert_eq!(entry.get("Password"), Some("new-secret"));
234
235 assert!(entry.update_unprotected("Password", "new-secret"));
236 assert!(!entry.fields["Password"].is_protected());
237
238 assert!(!entry.update_unprotected("Password", "new-secret"));
239
240 assert!(entry.update_protected("Password", "new-secret"));
241 assert!(entry.fields["Password"].is_protected());
242 }
243}