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; impl Store { pub fn load_database_for_mailbox>(mailbox: S) -> Result { 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>(&self, uid: S) -> BR { self.mb()?.contains_key(uid.into()) } pub fn mark_in_feed>(&self, uid: S) -> ER { self.feed()?.insert(uid.into(), &[1])?; Ok(()) } pub fn is_in_feed>(&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 { self.db.open_tree(&self.mailbox) } fn feed(&self) -> Result { self.db.open_tree("feed") } }