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#[derive(Debug, Clone, PartialEq, Eq)]
17pub struct Database {
18 pub config: DatabaseConfig,
20
21 pub(crate) root: GroupId,
23
24 pub(crate) entries: HashMap<EntryId, Entry>,
26
27 pub(crate) groups: HashMap<GroupId, Group>,
29}
30
31impl Database {
32 #[allow(clippy::new_without_default)]
34 pub fn new() -> Self {
35 Self::with_data(DatabaseConfig::default(), GroupId::new())
36 }
37
38 pub fn with_config(config: DatabaseConfig) -> Self {
40 Self::with_data(config, GroupId::new())
41 }
42
43 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 pub fn root(&self) -> GroupRef<'_> {
59 GroupRef::new(self, self.root)
60 }
61
62 pub fn root_mut(&mut self) -> GroupMut<'_> {
64 GroupMut::new(self, self.root)
65 }
66
67 pub fn iter_all_entries(&self) -> impl Iterator<Item = EntryRef<'_>> + '_ {
69 self.entries.keys().map(move |id| EntryRef::new(self, *id))
70 }
71
72 pub fn entry(&self, id: EntryId) -> Option<EntryRef<'_>> {
74 self.entries.contains_key(&id).then(move || EntryRef::new(self, id))
75 }
76
77 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 pub fn group(&self, id: GroupId) -> Option<GroupRef<'_>> {
84 self.groups.contains_key(&id).then(move || GroupRef::new(self, id))
85 }
86
87 pub fn group_mut(&mut self, id: GroupId) -> Option<GroupMut<'_>> {
89 self.groups.contains_key(&id).then(move || GroupMut::new(self, id))
90 }
91}