52 lines
1.3 KiB
Rust
52 lines
1.3 KiB
Rust
|
use sled::{Db, Transactional};
|
||
|
|
||
|
use crate::Message;
|
||
|
use std::str::FromStr;
|
||
|
|
||
|
pub(crate) struct Store {
|
||
|
db: Db,
|
||
|
mailbox: String,
|
||
|
}
|
||
|
|
||
|
type ER = Result<(), sled::Error>;
|
||
|
type BR = Result<bool, sled::Error>;
|
||
|
|
||
|
impl Store {
|
||
|
pub fn load_database_for_mailbox<S: Into<String>>(mailbox: S) -> Result<Self, sled::Error> {
|
||
|
let db = sled::open("data/maildb")?;
|
||
|
Ok(Store {
|
||
|
db,
|
||
|
mailbox: mailbox.into(),
|
||
|
})
|
||
|
}
|
||
|
|
||
|
pub fn store_mail(&self, message: &Message) -> ER {
|
||
|
self.mb()?.insert(message.get_uid(), &*message.data)?;
|
||
|
Ok(())
|
||
|
}
|
||
|
|
||
|
pub fn has_mail<S: Into<String>>(&self, uid: S) -> BR {
|
||
|
self.mb()?.contains_key(uid.into())
|
||
|
}
|
||
|
|
||
|
pub fn mark_in_feed<S: Into<String>>(&self, uid: S) -> ER {
|
||
|
self.feed()?.insert(uid.into(), &[1])?;
|
||
|
Ok(())
|
||
|
}
|
||
|
|
||
|
pub fn is_in_feed<S: Into<String>>(&self, uid: S) -> BR {
|
||
|
match self.feed()?.get(uid.into())? {
|
||
|
Some(v) => Ok(bincode::deserialize(&v).expect("Cannot convert to bool")),
|
||
|
None => Ok(false),
|
||
|
}
|
||
|
}
|
||
|
|
||
|
fn mb(&self) -> Result<sled::Tree, sled::Error> {
|
||
|
self.db.open_tree(&self.mailbox)
|
||
|
}
|
||
|
|
||
|
fn feed(&self) -> Result<sled::Tree, sled::Error> {
|
||
|
self.db.open_tree("feed")
|
||
|
}
|
||
|
}
|