keepice/src/db.rs
2026-08-26 12:35:59 -07:00

64 lines
1.6 KiB
Rust

use std::{fs, io};
#[derive(Clone, Copy, Default, PartialEq, Eq)]
pub enum BasicField {
Title,
#[default]
Username,
Password,
Url,
}
impl BasicField {
pub fn next(&mut self) {
*self = match self {
Self::Title => Self::Username,
Self::Username => Self::Password,
Self::Password => Self::Url,
Self::Url => Self::Title,
}
}
pub fn previous(&mut self) {
*self = match self {
Self::Title => Self::Url,
Self::Username => Self::Title,
Self::Password => Self::Username,
Self::Url => Self::Password,
}
}
}
pub trait EntrySearchExt {
fn contains(&self, s: impl AsRef<str>) -> bool;
}
impl EntrySearchExt for keepass::db::EntryRef<'_> {
fn contains(&self, s: impl AsRef<str>) -> bool {
let str = s.as_ref();
self.get_title().is_some_and(|title| title.contains(str))
|| self.get_url().is_some_and(|url| url.contains(str))
}
}
pub fn get_last_opened() -> io::Result<String> {
Ok(fs::read_to_string(
dirs::data_dir()
.ok_or(io::Error::new(
io::ErrorKind::NotFound,
"unable to find data_dir",
))?
.join("keepice.txt"),
)?)
}
pub fn set_last_opened(path: impl AsRef<str>) -> io::Result<()> {
fs::write(
dirs::data_dir()
.ok_or(io::Error::new(
io::ErrorKind::NotFound,
"unable to find data_dir",
))?
.join("keepice.txt"),
path.as_ref(),
)?;
Ok(())
}