kpdb/types/
entry.rs

1// Copyright (c) 2016-2017 Martijn Rijkeboer <[email protected]>
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8
9use chrono::{DateTime, Utc};
10use common;
11use std::collections::HashMap;
12use super::association::Association;
13use super::binary_key::BinaryKey;
14use super::binary_value::BinaryValue;
15use super::color::Color;
16use super::custom_icon_uuid::CustomIconUuid;
17use super::entry_uuid::EntryUuid;
18use super::icon::Icon;
19use super::obfuscation::Obfuscation;
20use super::string_key::StringKey;
21use super::string_value::StringValue;
22use super::strings_map::StringsMap;
23use super::times::Times;
24
25/// An entry in the database.
26#[derive(Clone, Debug, PartialEq)]
27pub struct Entry {
28    /// Auto-type associations.
29    pub associations: Vec<Association>,
30
31    /// Default auto-type sequence.
32    pub auto_type_def_sequence: String,
33
34    /// Whether auto-type is enabled.
35    pub auto_type_enabled: bool,
36
37    /// The type of obfuscation to use for auto-typing.
38    pub auto_type_obfuscation: Obfuscation,
39
40    /// The background color.
41    pub background_color: Option<Color>,
42
43    /// Map with binaries.
44    pub binaries: HashMap<BinaryKey, BinaryValue>,
45
46    /// The date and time this entry was created.
47    pub creation_time: DateTime<Utc>,
48
49    /// The identifier of this entry's custom icon if any.
50    pub custom_icon_uuid: Option<CustomIconUuid>,
51
52    /// Whether this entry expires.
53    pub expires: bool,
54
55    /// The date and time this entry will expire if expires is true.
56    pub expiry_time: DateTime<Utc>,
57
58    /// The foreground color.
59    pub foreground_color: Option<Color>,
60
61    /// This entry's icon.
62    pub icon: Icon,
63
64    /// The date and time this entry was last accessed.
65    pub last_accessed: DateTime<Utc>,
66
67    /// The date and time this entry was last modified.
68    pub last_modified: DateTime<Utc>,
69
70    /// The date and time the location of this entry was changed.
71    pub location_changed: DateTime<Utc>,
72
73    /// Override URL.
74    pub override_url: String,
75
76    /// Map with strings.
77    pub strings: StringsMap,
78
79    /// The tags associated with this entry.
80    pub tags: String,
81
82    /// The usage count of this entry.
83    pub usage_count: i32,
84
85    /// The identifier of this entry.
86    pub uuid: EntryUuid,
87}
88
89impl Entry {
90    /// Create a new entry.
91    pub fn new() -> Entry {
92        let mut entry = Entry::default();
93        let notes = StringValue::new("", common::PROTECT_NOTES_DEFAULT);
94        let password = StringValue::new("", common::PROTECT_PASSWORD_DEFAULT);
95        let title = StringValue::new("", common::PROTECT_TITLE_DEFAULT);
96        let url = StringValue::new("", common::PROTECT_URL_DEFAULT);
97        let username = StringValue::new("", common::PROTECT_USERNAME_DEFAULT);
98        entry.strings.insert(StringKey::Notes, notes);
99        entry.strings.insert(StringKey::Password, password);
100        entry.strings.insert(StringKey::Title, title);
101        entry.strings.insert(StringKey::Url, url);
102        entry.strings.insert(StringKey::Username, username);
103        entry.uuid = EntryUuid::new_random();
104        entry
105    }
106}
107
108impl Default for Entry {
109    fn default() -> Entry {
110        let now = Utc::now();
111        Entry {
112            associations: Default::default(),
113            auto_type_def_sequence: Default::default(),
114            auto_type_enabled: true,
115            auto_type_obfuscation: Default::default(),
116            background_color: Default::default(),
117            binaries: Default::default(),
118            creation_time: now,
119            custom_icon_uuid: Default::default(),
120            expires: false,
121            expiry_time: now,
122            foreground_color: Default::default(),
123            icon: Icon::Key,
124            last_accessed: now,
125            last_modified: now,
126            location_changed: now,
127            override_url: Default::default(),
128            strings: Default::default(),
129            tags: Default::default(),
130            usage_count: Default::default(),
131            uuid: Default::default(),
132        }
133    }
134}
135
136impl Times for Entry {
137    fn creation_time(&self) -> DateTime<Utc> {
138        self.creation_time
139    }
140
141    fn expires(&self) -> bool {
142        self.expires
143    }
144
145    fn expiry_time(&self) -> DateTime<Utc> {
146        self.expiry_time
147    }
148
149    fn last_accessed(&self) -> DateTime<Utc> {
150        self.last_accessed
151    }
152
153    fn last_modified(&self) -> DateTime<Utc> {
154        self.last_modified
155    }
156
157    fn location_changed(&self) -> DateTime<Utc> {
158        self.location_changed
159    }
160
161    fn usage_count(&self) -> i32 {
162        self.usage_count
163    }
164
165    fn set_creation_time(&mut self, val: DateTime<Utc>) {
166        self.creation_time = val;
167    }
168
169    fn set_expires(&mut self, val: bool) {
170        self.expires = val;
171    }
172
173    fn set_expiry_time(&mut self, val: DateTime<Utc>) {
174        self.expiry_time = val;
175    }
176
177    fn set_last_accessed(&mut self, val: DateTime<Utc>) {
178        self.last_accessed = val;
179    }
180
181    fn set_last_modified(&mut self, val: DateTime<Utc>) {
182        self.last_modified = val;
183    }
184
185    fn set_location_changed(&mut self, val: DateTime<Utc>) {
186        self.location_changed = val;
187    }
188
189    fn set_usage_count(&mut self, val: i32) {
190        self.usage_count = val;
191    }
192}
193
194#[cfg(test)]
195mod tests {
196
197    use chrono::Utc;
198    use std::collections::HashMap;
199    use super::*;
200    use types::EntryUuid;
201    use types::Icon;
202    use types::Obfuscation;
203    use types::StringKey;
204    use types::StringsMap;
205    use utils::test::approx_equal_datetime;
206
207    #[test]
208    fn test_new_returns_correct_instance() {
209        let now = Utc::now();
210        let entry = Entry::new();
211        assert_eq!(entry.associations, Vec::new());
212        assert_eq!(entry.auto_type_def_sequence, "");
213        assert_eq!(entry.auto_type_enabled, true);
214        assert_eq!(entry.auto_type_obfuscation, Obfuscation::None);
215        assert_eq!(entry.background_color, None);
216        assert_eq!(entry.binaries, HashMap::new());
217        assert!(approx_equal_datetime(entry.creation_time, now));
218        assert_eq!(entry.custom_icon_uuid, None);
219        assert_eq!(entry.expires, false);
220        assert!(approx_equal_datetime(entry.expiry_time, now));
221        assert_eq!(entry.foreground_color, None);
222        assert_eq!(entry.icon, Icon::Key);
223        assert!(approx_equal_datetime(entry.last_accessed, now));
224        assert!(approx_equal_datetime(entry.last_modified, now));
225        assert!(approx_equal_datetime(entry.location_changed, now));
226        assert_eq!(entry.override_url, "");
227        assert_eq!(entry.strings.len(), 5);
228        assert!(entry.strings.contains_key(&StringKey::Notes));
229        assert!(entry.strings.contains_key(&StringKey::Password));
230        assert!(entry.strings.contains_key(&StringKey::Title));
231        assert!(entry.strings.contains_key(&StringKey::Url));
232        assert!(entry.strings.contains_key(&StringKey::Username));
233        assert_eq!(entry.tags, "");
234        assert_eq!(entry.usage_count, 0);
235        assert!(entry.uuid != EntryUuid::nil());
236    }
237
238    #[test]
239    fn test_default_returns_correct_instance() {
240        let now = Utc::now();
241        let entry = Entry::default();
242        assert_eq!(entry.associations, Vec::new());
243        assert_eq!(entry.auto_type_def_sequence, "");
244        assert_eq!(entry.auto_type_enabled, true);
245        assert_eq!(entry.auto_type_obfuscation, Obfuscation::None);
246        assert_eq!(entry.background_color, None);
247        assert_eq!(entry.binaries, HashMap::new());
248        assert!(approx_equal_datetime(entry.creation_time, now));
249        assert_eq!(entry.custom_icon_uuid, None);
250        assert_eq!(entry.expires, false);
251        assert!(approx_equal_datetime(entry.expiry_time, now));
252        assert_eq!(entry.foreground_color, None);
253        assert_eq!(entry.icon, Icon::Key);
254        assert!(approx_equal_datetime(entry.last_accessed, now));
255        assert!(approx_equal_datetime(entry.last_modified, now));
256        assert!(approx_equal_datetime(entry.location_changed, now));
257        assert_eq!(entry.override_url, "");
258        assert_eq!(entry.strings, StringsMap::new());
259        assert_eq!(entry.tags, "");
260        assert_eq!(entry.usage_count, 0);
261        assert_eq!(entry.uuid, EntryUuid::nil());
262    }
263}