crabmail

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

xml.rs (3429B) - raw


      1 use super::util::xml_safe as x;
      2 use crate::models::*;
      3 use crate::time::Date;
      4 use crate::util::unformat_flowed;
      5 // use crate::templates::util::xml_safe;
      6 // use anyhow::{Context, Result};
      7 use nanotemplate::template;
      8 
      9 const FEED_TEMPLATE: &str = r#"<?xml version="1.0" encoding="utf-8"?>
     10 <feed xmlns="http://www.w3.org/2005/Atom">
     11 <title>{feed_title}</title>
     12 <link href="{feed_link}"/>
     13 <updated>{last_updated}</updated>
     14 <author>
     15 <name>{author_name}</name>
     16 <email>{author_email}</email>
     17 </author>
     18 <id>{feed_id}</id>
     19 {entry_list}
     20 </feed>"#;
     21 
     22 const MESSAGE_TEMPLATE: &str = r#"<entry>
     23 <title>{title}</title>
     24 <link href="{item_link}"/>
     25 <id>{entry_id}</id>
     26 <updated>{updated_at}</updated>
     27 <author>
     28 <name>{author_name}</name>
     29 <email>{author_email}</email>
     30 </author>
     31 <content type="text/plain">
     32 {content}
     33 </content>
     34 </entry>
     35 "#;
     36 
     37 impl StrMessage {
     38     pub fn to_xml(&self) -> String {
     39         let msg = self;
     40         let body = match self.flowed {
     41             true => unformat_flowed(&self.body),
     42             false => self.body.clone(),
     43         };
     44         template(
     45             MESSAGE_TEMPLATE,
     46             &[
     47                 ("title", &x(&msg.subject)),
     48                 ("item_link", &x(&self.url)),
     49                 ("entry_id", &x(&msg.id)),
     50                 ("updated_at", &Date::from(msg.received).rfc3339()),
     51                 (
     52                     "author_name",
     53                     &x(&msg.from.clone().name.unwrap_or(msg.from.clone().address)),
     54                 ),
     55                 ("author_email", &x(&msg.from.address)),
     56                 ("content", &x(&body)),
     57             ],
     58         )
     59         .unwrap()
     60     }
     61 }
     62 
     63 // TODO dedup
     64 impl List {
     65     pub fn to_xml(&self) -> String {
     66         let mut entry_list = String::new();
     67         for msg in &self.recent_messages {
     68             entry_list.push_str(&msg.to_xml());
     69         }
     70         // Sometimes its unclear whether to do stuff like this in models.rs or here. could refactor
     71         let last_updated = self
     72             .recent_messages
     73             .get(0)
     74             .and_then(|x| Some(x.received))
     75             .unwrap_or(1);
     76         template(
     77             FEED_TEMPLATE,
     78             &[
     79                 ("feed_link", &self.url),
     80                 ("feed_id", &self.url),
     81                 ("feed_title", &self.config.name),
     82                 ("last_updated", &Date::from(last_updated).rfc3339()),
     83                 ("entry_list", &entry_list),
     84                 ("author_name", &self.config.email),
     85                 ("author_email", &self.config.email),
     86             ],
     87         )
     88         .unwrap()
     89     }
     90 }
     91 
     92 impl Thread {
     93     pub fn to_xml(&self) -> String {
     94         let mut entry_list = String::new();
     95         for msg in &self.messages {
     96             entry_list.push_str(&msg.to_xml());
     97         }
     98         // Sometimes its unclear whether to do stuff like this in models.rs or here. could refactor
     99         let root = &self.messages[0];
    100         template(
    101             FEED_TEMPLATE,
    102             &[
    103                 ("feed_link", &self.url),
    104                 ("feed_id", &self.url),
    105                 ("feed_title", &root.subject),
    106                 ("last_updated", &Date::from(root.received).rfc3339()),
    107                 ("entry_list", &entry_list),
    108                 (
    109                     "author_name",
    110                     &root.from.name.clone().unwrap_or(root.from.address.clone()),
    111                 ),
    112                 ("author_email", &root.from.address),
    113             ],
    114         )
    115         .unwrap()
    116     }
    117 }