kpdb/types/
group.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 super::custom_icon_uuid::CustomIconUuid;
11use super::entry_uuid::EntryUuid;
12use super::group_uuid::GroupUuid;
13use super::icon::Icon;
14use super::times::Times;
15
16/// A group in the database.
17#[derive(Clone, Debug, PartialEq)]
18pub struct Group {
19    /// The date and time this group was created.
20    pub creation_time: DateTime<Utc>,
21
22    /// The identifier of this group's custom icon if any.
23    pub custom_icon_uuid: Option<CustomIconUuid>,
24
25    /// Default auto-type sequence.
26    pub def_auto_type_sequence: String,
27
28    /// Whether auto-type is enabled.
29    pub enable_auto_type: Option<bool>,
30
31    /// Whether searching is enabled.
32    pub enable_searching: Option<bool>,
33
34    /// Vector with entry identifiers that belong to this group.
35    pub entries: Vec<EntryUuid>,
36
37    /// Whether this group expires.
38    pub expires: bool,
39
40    /// The date and time this group will expire if expires is true.
41    pub expiry_time: DateTime<Utc>,
42
43    /// Vector with group identifiers that are subgroups of this group.
44    pub groups: Vec<GroupUuid>,
45
46    /// This group's icon.
47    pub icon: Icon,
48
49    /// Whether this group is expanded.
50    pub is_expanded: bool,
51
52    /// The date and time this group was last accessed.
53    pub last_accessed: DateTime<Utc>,
54
55    /// The date and time this group was last modified.
56    pub last_modified: DateTime<Utc>,
57
58    /// The identifier of the last top visible entry.
59    pub last_top_visible_entry: EntryUuid,
60
61    /// The date and time the location of this group was changed.
62    pub location_changed: DateTime<Utc>,
63
64    /// The name of this group.
65    pub name: String,
66
67    /// The notes of this group.
68    pub notes: String,
69
70    /// The usage count of this group.
71    pub usage_count: i32,
72
73    /// The identifier of this group.
74    pub uuid: GroupUuid,
75}
76
77impl Group {
78    /// Create a new group.
79    ///
80    /// # Examples
81    ///
82    /// ```rust
83    /// use kpdb::Group;
84    ///
85    /// let group = Group::new("Websites");
86    /// ```
87    pub fn new<S: Into<String>>(name: S) -> Group {
88        let mut group = Group::default();
89        group.name = name.into();
90        group.uuid = GroupUuid::new_random();
91        group
92    }
93}
94
95impl Default for Group {
96    fn default() -> Group {
97        let now = Utc::now();
98        Group {
99            creation_time: now,
100            custom_icon_uuid: Default::default(),
101            def_auto_type_sequence: Default::default(),
102            enable_auto_type: Default::default(),
103            enable_searching: Default::default(),
104            entries: Default::default(),
105            expires: false,
106            expiry_time: now,
107            groups: Default::default(),
108            icon: Icon::Folder,
109            is_expanded: true,
110            last_accessed: now,
111            last_modified: now,
112            last_top_visible_entry: Default::default(),
113            location_changed: now,
114            name: Default::default(),
115            notes: Default::default(),
116            usage_count: Default::default(),
117            uuid: Default::default(),
118        }
119    }
120}
121
122impl Times for Group {
123    fn creation_time(&self) -> DateTime<Utc> {
124        self.creation_time
125    }
126
127    fn expires(&self) -> bool {
128        self.expires
129    }
130
131    fn expiry_time(&self) -> DateTime<Utc> {
132        self.expiry_time
133    }
134
135    fn last_accessed(&self) -> DateTime<Utc> {
136        self.last_accessed
137    }
138
139    fn last_modified(&self) -> DateTime<Utc> {
140        self.last_modified
141    }
142
143    fn location_changed(&self) -> DateTime<Utc> {
144        self.location_changed
145    }
146
147    fn usage_count(&self) -> i32 {
148        self.usage_count
149    }
150
151    fn set_creation_time(&mut self, val: DateTime<Utc>) {
152        self.creation_time = val;
153    }
154
155    fn set_expires(&mut self, val: bool) {
156        self.expires = val;
157    }
158
159    fn set_expiry_time(&mut self, val: DateTime<Utc>) {
160        self.expiry_time = val;
161    }
162
163    fn set_last_accessed(&mut self, val: DateTime<Utc>) {
164        self.last_accessed = val;
165    }
166
167    fn set_last_modified(&mut self, val: DateTime<Utc>) {
168        self.last_modified = val;
169    }
170
171    fn set_location_changed(&mut self, val: DateTime<Utc>) {
172        self.location_changed = val;
173    }
174
175    fn set_usage_count(&mut self, val: i32) {
176        self.usage_count = val;
177    }
178}
179
180#[cfg(test)]
181mod tests {
182
183    use chrono::Utc;
184    use super::*;
185    use types::EntryUuid;
186    use types::GroupUuid;
187    use types::Icon;
188    use utils::test::approx_equal_datetime;
189
190    #[test]
191    fn test_new_returns_correct_instance() {
192        let now = Utc::now();
193        let name = "Root";
194        let group = Group::new(name.clone());
195        assert!(approx_equal_datetime(group.creation_time, now));
196        assert_eq!(group.custom_icon_uuid, None);
197        assert_eq!(group.def_auto_type_sequence, "");
198        assert_eq!(group.enable_auto_type, None);
199        assert_eq!(group.enable_searching, None);
200        assert_eq!(group.entries, Vec::new());
201        assert_eq!(group.expires, false);
202        assert!(approx_equal_datetime(group.expiry_time, now));
203        assert_eq!(group.groups, Vec::new());
204        assert_eq!(group.icon, Icon::Folder);
205        assert_eq!(group.is_expanded, true);
206        assert!(approx_equal_datetime(group.last_accessed, now));
207        assert!(approx_equal_datetime(group.last_modified, now));
208        assert_eq!(group.last_top_visible_entry, EntryUuid::nil());
209        assert!(approx_equal_datetime(group.location_changed, now));
210        assert_eq!(group.name, name);
211        assert_eq!(group.notes, "");
212        assert_eq!(group.usage_count, 0);
213        assert!(group.uuid != GroupUuid::nil());
214    }
215
216    #[test]
217    fn test_default_returns_correct_instance() {
218        let now = Utc::now();
219        let group = Group::default();
220        assert!(approx_equal_datetime(group.creation_time, now));
221        assert_eq!(group.custom_icon_uuid, None);
222        assert_eq!(group.def_auto_type_sequence, "");
223        assert_eq!(group.enable_auto_type, None);
224        assert_eq!(group.enable_searching, None);
225        assert_eq!(group.entries, Vec::new());
226        assert_eq!(group.expires, false);
227        assert!(approx_equal_datetime(group.expiry_time, now));
228        assert_eq!(group.groups, Vec::new());
229        assert_eq!(group.icon, Icon::Folder);
230        assert_eq!(group.is_expanded, true);
231        assert!(approx_equal_datetime(group.last_accessed, now));
232        assert!(approx_equal_datetime(group.last_modified, now));
233        assert_eq!(group.last_top_visible_entry, EntryUuid::nil());
234        assert!(approx_equal_datetime(group.location_changed, now));
235        assert_eq!(group.name, "");
236        assert_eq!(group.notes, "");
237        assert_eq!(group.usage_count, 0);
238        assert_eq!(group.uuid, GroupUuid::nil());
239    }
240}