use std::{env, fs};
use anyhow::Result;
use constcat::concat;
use csv::Trim;
use quirks::Odyssey;
use crate::model::mua::NEOMUTT_XDG_CONFIG_DIR;
const DRAFTBOX_FILE: &str = concat!(NEOMUTT_XDG_CONFIG_DIR, "/.draftboxes");
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
pub struct Draftbox {
pub address: String,
pub path: String,
}
impl Draftbox {
pub fn new(address: &str, path: &str) -> Self {
let address = String::from(address);
let path = String::from(path);
Draftbox { address, path }
}
pub fn exists() -> bool {
match fs::exists(DRAFTBOX_FILE) {
Ok(res) if res == true => true,
Ok(_) => false,
Err(_) => false,
}
}
pub fn load() -> Result<Vec<Self>> {
let mut reader = csv::ReaderBuilder::new()
.has_headers(false)
.delimiter(b'=')
.trim(Trim::All)
.comment(Some(b'#'))
.from_path(Self::draftboxes_file()?)?;
let mut draftboxes = vec![];
for draftbox in reader.deserialize::<Draftbox>() {
let mut draftbox = draftbox?;
draftbox.path = draftbox.path.expand()?;
draftboxes.push(draftbox);
}
Ok(draftboxes)
}
pub fn folders(&self) -> Vec<String> {
vec![
String::from(format!("{}/new", self.path)),
String::from(format!("{}/cur", self.path)),
String::from(format!("{}/tmp", self.path)),
]
}
fn draftboxes_file() -> Result<String> {
Ok(String::from(format!(
"{}/{}",
env::var("HOME")?,
DRAFTBOX_FILE
)))
}
}