crabmail

Static HTML email archive viewer in Rust
git clone git://git.alexwennerberg.com/crabmail
Log | Files | Refs | README | LICENSE

commit 16d2b88428a9f4609601bbefa76b4cc078e624b5
parent b9610494797289c66f89d684609317e1866149e9
Author: alex wennerberg <alex@alexwennerberg.com>
Date:   Sun, 19 Dec 2021 09:04:04 -0800

refactor out_dir into global scope

Diffstat:
Msrc/config.rs | 4+++-
Msrc/main.rs | 22+++++++++++-----------
2 files changed, 14 insertions(+), 12 deletions(-)

diff --git a/src/config.rs b/src/config.rs @@ -1,7 +1,7 @@ use once_cell::sync::OnceCell; use std::fs::File; use std::io::{self, BufRead}; -use std::path::Path; +use std::path::{Path, PathBuf}; // Config file structure is very simple: // key=value\n @@ -11,6 +11,7 @@ pub struct Config { pub list_email: String, pub url: String, pub homepage: String, + pub out_dir: PathBuf, } pub static INSTANCE: OnceCell<Config> = OnceCell::new(); @@ -52,6 +53,7 @@ impl Config { list_email, url, homepage, + out_dir: PathBuf::from(""), }) } } diff --git a/src/main.rs b/src/main.rs @@ -92,7 +92,7 @@ fn short_name(s: &SingleInfo) -> &str { } impl<'a> ThreadList<'a> { - pub fn write_to_file(&self, out_dir: &Path) -> Result<()> { + pub fn write_to_file(&self) -> Result<()> { let tmp = html! { h1(class="page-title"): &Config::global().list_name; @@ -123,7 +123,7 @@ impl<'a> ThreadList<'a> { } }; - let file = File::create(&out_dir.join("index.html"))?; + let file = File::create(&Config::global().out_dir.join("index.html"))?; let mut br = BufWriter::new(file); layout(Config::global().list_name.as_str(), tmp).write_to_io(&mut br)?; Ok(()) @@ -135,7 +135,7 @@ impl<'a> MailThread<'a> { return self.messages[self.messages.len() - 1].date; } - fn write_atom_feed(&self, out_dir: &Path) -> Result<()> { + fn write_atom_feed(&self) -> Result<()> { // TODO dry let mut entries: String = String::new(); for message in &self.messages { @@ -188,13 +188,13 @@ impl<'a> MailThread<'a> { feed_id = "tbd", entry_list = entries, ); - let thread_dir = out_dir.join("threads"); + let thread_dir = Config::global().out_dir.join("threads"); let mut file = File::create(&thread_dir.join(format!("{}.xml", &self.hash)))?; file.write(atom.as_bytes())?; Ok(()) } - fn write_to_file(&self, out_dir: &Path) -> Result<()> { + fn write_to_file(&self) -> Result<()> { let root = self.messages[0]; let tmp = html! { h1(class="page-title"): &root.subject; @@ -238,7 +238,7 @@ impl<'a> MailThread<'a> { } } }; - let thread_dir = out_dir.join("threads"); + let thread_dir = Config::global().out_dir.join("threads"); std::fs::create_dir(&thread_dir).ok(); let file = File::create(&thread_dir.join(format!("{}.html", &self.hash)))?; @@ -380,8 +380,8 @@ fn main() -> Result<()> { .unwrap_or("crabmail.conf".into()); let in_mbox = pargs.value_from_os_str(["-m", "--mbox"], parse_path)?; - // out_dir is temp hack - let config = Config::from_file(&config_file).unwrap(); // TODO better err handling + let mut config = Config::from_file(&config_file).unwrap(); // TODO better err handling + config.out_dir = out_dir.to_owned(); INSTANCE.set(config).unwrap(); let mbox = mbox::from_file(&in_mbox)?; @@ -455,8 +455,8 @@ fn main() -> Result<()> { thread.last_reply = thread.last_reply(); - thread.write_to_file(&out_dir)?; - thread.write_atom_feed(&out_dir)?; + thread.write_to_file()?; + thread.write_atom_feed()?; curr_threads.remove(&thread.hash); threads.push(thread); } @@ -471,7 +471,7 @@ fn main() -> Result<()> { threads.sort_by_key(|a| a.last_reply); threads.reverse(); - ThreadList { threads }.write_to_file(&out_dir)?; + ThreadList { threads }.write_to_file()?; // kinda clunky let css = include_bytes!("style.css"); let mut css_root = File::create(out_dir.join("style.css"))?;