crabmail

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

commit dcd2b4845e82d951e14da39efd849a9e3b249aea
parent 698e8ccf7ae68326ee45c0cc00c3725fb28ca30b
Author: alex wennerberg <alex@alexwennerberg.com>
Date:   Sun, 13 Mar 2022 16:51:40 -0700

add time function

Diffstat:
Msrc/main.rs | 15+--------------
Msrc/models.rs | 18++++++++++++++++--
Msrc/utils.rs | 30++++++++++++++++++++++++++++++
3 files changed, 47 insertions(+), 16 deletions(-)

diff --git a/src/main.rs b/src/main.rs @@ -59,19 +59,7 @@ fn pathescape_msg_id(s: &str) -> PathBuf { PathBuf::from(s.replace("/", ";")) } -enum Format { - XML, - HTML, - GMI, -} - impl List { - // TODO move to main - // fn from_maildir() -> Self { // TODO figure out init - // where to live - // List { threads: vec![] } - // - fn persist(&self) { // let written = hashset self.write_index(); @@ -158,9 +146,8 @@ fn main() -> Result<()> { let msg = mail_parser::Message::parse(&data).context("Missing mail bytes")?; list.add_email(&msg, f.path().to_path_buf()); } - // id list into thread list.finalize(); - // lists.lists.push(list); + lists.add(list, &dir_name); } lists.write_lists(); diff --git a/src/models.rs b/src/models.rs @@ -17,8 +17,22 @@ pub struct Lists { pub out_dir: PathBuf, } +impl Lists { + pub fn add(&mut self, thread_idx: ThreadIdx, name: &str) { + // TODO safe name? + let config = match Config::global().get_subsection(name) { + Some(sub) => sub, + None => Config::global().default_subsection(name), + }; + self.lists.push(List { + thread_idx, + config, + out_dir: self.out_dir.join(name), + }) + } +} pub struct List { - pub thread_idx: crate::threading::ThreadIdx, + pub thread_idx: ThreadIdx, // Thread topics pub config: Subsection, // path pub out_dir: PathBuf, @@ -32,7 +46,7 @@ impl List { None => con.default_subsection(name), }; Self { - thread_idx: crate::threading::ThreadIdx::default(), + thread_idx: ThreadIdx::default(), config: sub, out_dir: Config::global().out_dir.join(name), } diff --git a/src/utils.rs b/src/utils.rs @@ -1,3 +1,4 @@ +use mail_parser::DateTime; use std::fs::{read, write}; use std::io::prelude::*; use std::path::PathBuf; @@ -13,3 +14,32 @@ fn write_if_unchanged(path: PathBuf, data: &[u8]) -> bool { } return true; } + +// from https://github.com/protocolbuffers/upb/blob/22182e6e/upb/json_decode.c#L982-L992 +fn epoch_days(y: u32, m: u32, d: u32) -> i64 { + let year_base = 4800; + let m_adj = m - 3; + let carry = match m_adj > m { + true => 1, + false => 0, + }; + let adjust = carry * 12; + let y_adj = m + year_base - carry; + let month_days = ((m_adj + adjust) * 62719 + 769) / 2048; + let leap_days = y_adj / 4 - y_adj / 100 + y_adj / 400; + y_adj as i64 * 365 + leap_days as i64 + month_days as i64 + (d as i64 - 1) - 2472632 +} + +fn epoch_time(dt: &DateTime) -> i64 { + let mut h = dt.hour as i64; + let mut m = dt.minute as i64; + let s = dt.second; + let adj = match dt.tz_before_gmt { + true => 1, + false => -1, + }; + h += dt.tz_hour as i64 * adj; + m += dt.tz_minute as i64 * adj; + + return epoch_days(dt.year, dt.month, dt.day) * 86400 + h * 3600 + m * 60 + dt.second as i64; +}