فهرست منبع

refactor: use more DateTime objects

Johann150 2 سال پیش
والد
کامیت
984194da1a
4فایلهای تغییر یافته به همراه11 افزوده شده و 13 حذف شده
  1. 4 1
      src/config.rs
  2. 5 10
      src/models.rs
  3. 1 1
      src/templates/gmi.rs
  4. 1 1
      src/templates/html.rs

+ 4 - 1
src/config.rs

@@ -83,7 +83,10 @@ impl Config {
     }
 
     pub fn get_subsection(&self, name: &str) -> Option<Subsection> {
-        self.subsections.iter().find(|sub| sub.name == name).cloned()
+        self.subsections
+            .iter()
+            .find(|sub| sub.name == name)
+            .cloned()
     }
 
     pub fn from_file<P: AsRef<Path>>(path: P) -> Result<Config, std::io::Error> {

+ 5 - 10
src/models.rs

@@ -1,6 +1,6 @@
 use crate::config::{Config, Subsection};
 use crate::threading::{Msg, ThreadIdx};
-use mail_builder::headers::text::Text;
+use mail_builder::headers::date::Date;
 use mail_builder::MessageBuilder;
 use mail_parser::{Addr, DateTime, HeaderValue, Message, RfcHeader};
 use std::borrow::Cow;
@@ -88,7 +88,7 @@ pub struct StrMessage {
     pub received: DateTime,
     pub preview: String,
     pub from: MailAddress,
-    pub date: String, // TODO better dates
+    pub date: DateTime,
     pub body: String,
     pub flowed: bool,
     pub mailto: String, // mailto link
@@ -156,7 +156,7 @@ impl StrMessage {
             .iter()
             .map(|x| (x.name.clone().unwrap_or(String::new()), x.address.clone()))
             .collect::<Vec<(String, String)>>());
-        message.header("Date", Text::from(self.date.as_str()));
+        message.header("Date", Date::new(self.date.to_timestamp()));
         if let Some(irt) = &self.in_reply_to {
             message.in_reply_to(irt.as_str());
         }
@@ -229,12 +229,7 @@ impl StrMessage {
             _ => &invalid_email,
         };
         let from = MailAddress::from_addr(from);
-        let date = msg
-            .get_rfc_header(RfcHeader::Date)
-            .and_then(|x| Some(x.get(0).unwrap_or(&Cow::from("")).to_string()))
-            .unwrap_or(String::new())
-            .trim()
-            .to_owned(); // TODO awkawrd
+        let date = msg.get_date().cloned().unwrap_or(crate::util::EPOCH);
         let to = match msg.get_to() {
             HeaderValue::Address(fr) => vec![MailAddress::from_addr(fr)],
             HeaderValue::AddressList(fr) => fr.iter().map(|a| MailAddress::from_addr(a)).collect(),
@@ -276,7 +271,7 @@ impl StrMessage {
             cc,
             url: String::new(),
             thread_subject: thread_subject.to_owned(),
-            date: date.to_owned(),
+            date,
             body: body.to_string(),
             flowed,
             mailto: String::new(),

+ 1 - 1
src/templates/gmi.rs

@@ -110,7 +110,7 @@ impl Thread {
                     "{body}",
                 ),
                 subject = h(&msg.subject),
-                date = h(&msg.date),
+                date = msg.date,
                 msg_id = h(&msg.id),
                 to = h(&msg
                     .to

+ 1 - 1
src/templates/html.rs

@@ -230,7 +230,7 @@ impl Thread {
                 subject = x(&msg.subject),
                 mailto = x(&msg.mailto),
                 from = msg.from.to_html(),
-                date = x(&msg.date),
+                date = msg.date,
                 in_reply_to = in_reply_to,
                 extra_headers = extra_headers,
                 body = email_body(&msg.body, msg.flowed),