use std::{
collections::HashMap,
ops::{Deref, DerefMut},
};
use uuid::Uuid;
use crate::{
Database,
db::{GroupId, Times, Value, fields},
};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct EntryId(Uuid);
impl Default for EntryId {
fn default() -> Self {
Self::new()
}
}
impl EntryId {
pub fn new() -> Self {
Self(Uuid::new_v4())
}
pub const fn from_uuid(uuid: Uuid) -> Self {
Self(uuid)
}
pub fn uuid(&self) -> Uuid {
self.0
}
}
impl From<Uuid> for EntryId {
fn from(uuid: Uuid) -> Self {
Self::from_uuid(uuid)
}
}
#[derive(Debug, Eq, PartialEq, Clone)]
pub struct Entry {
pub(crate) id: EntryId,
pub(crate) parent: GroupId,
pub fields: HashMap<String, Value<String>>,
pub times: Times,
}
impl Entry {
pub(crate) fn with_id(id: EntryId, parent: GroupId) -> Self {
Self { id, parent, fields: HashMap::new(), times: Times::new() }
}
pub fn id(&self) -> EntryId {
self.id
}
pub fn get(&self, key: &str) -> Option<&str> {
self.fields.get(key).map(|v| v.as_str())
}
pub fn set(&mut self, key: impl Into<String>, value: Value<String>) {
self.fields.insert(key.into(), value);
}
pub fn set_unprotected(&mut self, key: impl Into<String>, value: impl Into<String>) {
self.set(key, Value::unprotected(value));
}
pub fn update_unprotected(&mut self, key: &str, value: &str) -> bool {
if self.fields.get(key).is_some_and(|current| {
!current.is_protected() && current.get() == value
}) {
return false;
}
self.set(key, Value::unprotected(value.to_owned()));
true
}
pub fn update_protected(&mut self, key: &str, value: &str) -> bool {
if self.fields.get(key).is_some_and(|current| {
current.is_protected() && current.get() == value
}) {
return false;
}
self.set(key, Value::protected(value.to_owned()));
true
}
pub fn set_protected(&mut self, key: impl Into<String>, value: impl Into<String>) {
self.set(key, Value::protected(value));
}
pub fn get_raw_otp_value(&self) -> Option<&str> {
self.get(fields::OTP)
}
pub fn get_title(&self) -> Option<&str> {
self.get(fields::TITLE)
}
pub fn get_username(&self) -> Option<&str> {
self.get(fields::USERNAME)
}
pub fn get_password(&self) -> Option<&str> {
self.get(fields::PASSWORD)
}
pub fn get_url(&self) -> Option<&str> {
self.get(fields::URL)
}
}
impl std::fmt::Display for EntryId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
pub struct EntryRef<'a> {
database: &'a Database,
id: EntryId,
}
impl EntryRef<'_> {
pub(crate) fn new(database: &Database, id: EntryId) -> EntryRef<'_> {
EntryRef { database, id }
}
}
impl Deref for EntryRef<'_> {
type Target = Entry;
#[allow(clippy::expect_used, clippy::missing_panics_doc)] fn deref(&self) -> &Self::Target {
self.database.entries.get(&self.id).expect("Entry not found")
}
}
pub struct EntryMut<'a> {
database: &'a mut Database,
id: EntryId,
}
impl EntryMut<'_> {
pub(crate) fn new(database: &mut Database, id: EntryId) -> EntryMut<'_> {
EntryMut { database, id }
}
#[allow(clippy::expect_used, clippy::missing_panics_doc)] pub fn remove(self) {
let entry = self.database.entries.remove(&self.id).expect("Entry not found");
self.database.group_mut(entry.parent).expect("Parent group not found").entries.shift_remove(&self.id);
}
}
impl Deref for EntryMut<'_> {
type Target = Entry;
#[allow(clippy::expect_used, clippy::missing_panics_doc)] fn deref(&self) -> &Self::Target {
self.database.entries.get(&self.id).expect("Entry not found")
}
}
impl DerefMut for EntryMut<'_> {
#[allow(clippy::expect_used, clippy::missing_panics_doc)] fn deref_mut(&mut self) -> &mut Self::Target {
self.database.entries.get_mut(&self.id).expect("Entry not found")
}
}
#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod tests {
use super::EntryId;
#[test]
fn entry_id_new_generates_distinct_ids() {
assert_ne!(EntryId::new(), EntryId::new());
}
#[test]
fn update_fields_only_when_changed() {
use super::Entry;
use crate::db::GroupId;
let mut entry = Entry::with_id(EntryId::new(), GroupId::new());
assert!(entry.update_protected("Password", "secret"));
assert!(!entry.update_protected("Password", "secret"));
assert!(entry.fields["Password"].is_protected());
assert!(entry.update_protected("Password", "new-secret"));
assert_eq!(entry.get("Password"), Some("new-secret"));
assert!(entry.update_unprotected("Password", "new-secret"));
assert!(!entry.fields["Password"].is_protected());
assert!(!entry.update_unprotected("Password", "new-secret"));
assert!(entry.update_protected("Password", "new-secret"));
assert!(entry.fields["Password"].is_protected());
}
}