Skip to main content

keepass/db/types/
mod.rs

1pub(crate) mod entry;
2pub(crate) mod group;
3pub(crate) mod times;
4pub(crate) mod value;
5
6use std::collections::HashMap;
7
8pub use entry::{Entry, EntryId, EntryMut, EntryRef};
9pub use group::{DuplicateEntryIdError, DuplicateGroupIdError, Group, GroupId, GroupMut, GroupRef};
10pub use times::Times;
11pub use value::Value;
12
13use crate::config::DatabaseConfig;
14
15/// A decrypted KeePass database
16#[derive(Debug, Clone, PartialEq, Eq)]
17pub struct Database {
18	/// Encryption, compression, and KDF configuration.
19	pub config: DatabaseConfig,
20
21	/// Root group identifier.
22	pub(crate) root: GroupId,
23
24	/// All entries.
25	pub(crate) entries: HashMap<EntryId, Entry>,
26
27	/// All groups.
28	pub(crate) groups: HashMap<GroupId, Group>,
29}
30
31impl Database {
32	/// Create a new database with a single root group.
33	#[allow(clippy::new_without_default)]
34	pub fn new() -> Self {
35		Self::with_data(DatabaseConfig::default(), GroupId::new())
36	}
37
38	/// Create a new database with the given configuration.
39	pub fn with_config(config: DatabaseConfig) -> Self {
40		Self::with_data(config, GroupId::new())
41	}
42
43	/// Create a new database with the given root group ID.
44	pub fn new_with_root_id(root_id: GroupId) -> Self {
45		Self::with_data(DatabaseConfig::default(), root_id)
46	}
47
48	pub(crate) fn with_data(config: DatabaseConfig, root_id: GroupId) -> Self {
49		let root = Group::with_id(root_id, None);
50
51		let mut groups = HashMap::new();
52		groups.insert(root_id, root);
53
54		Self { config, root: root_id, entries: HashMap::new(), groups }
55	}
56
57	/// Get an immutable reference to the root group of the database.
58	pub fn root(&self) -> GroupRef<'_> {
59		GroupRef::new(self, self.root)
60	}
61
62	/// Get a mutable reference to the root group of the database.
63	pub fn root_mut(&mut self) -> GroupMut<'_> {
64		GroupMut::new(self, self.root)
65	}
66
67	/// Iterate over all entries with immutable access.
68	pub fn iter_all_entries(&self) -> impl Iterator<Item = EntryRef<'_>> + '_ {
69		self.entries.keys().map(move |id| EntryRef::new(self, *id))
70	}
71
72	/// Get an immutable reference to the entry with the given ID, if it exists
73	pub fn entry(&self, id: EntryId) -> Option<EntryRef<'_>> {
74		self.entries.contains_key(&id).then(move || EntryRef::new(self, id))
75	}
76
77	/// Get a mutable reference to the entry with the given ID, if it exists
78	pub fn entry_mut(&mut self, id: EntryId) -> Option<EntryMut<'_>> {
79		self.entries.contains_key(&id).then(move || EntryMut::new(self, id))
80	}
81
82	/// Get an immutable reference to the group with the given ID, if it exists
83	pub fn group(&self, id: GroupId) -> Option<GroupRef<'_>> {
84		self.groups.contains_key(&id).then(move || GroupRef::new(self, id))
85	}
86
87	/// Get a mutable reference to the group with the given ID, if it exists
88	pub fn group_mut(&mut self, id: GroupId) -> Option<GroupMut<'_>> {
89		self.groups.contains_key(&id).then(move || GroupMut::new(self, id))
90	}
91}