diff --git a/src/message/mod.rs b/src/message/mod.rs index abc383e..a9b3a41 100644 --- a/src/message/mod.rs +++ b/src/message/mod.rs @@ -44,7 +44,7 @@ pub struct Message { pub from: Mailbox, pub senders: Vec, pub recipients: Vec, - pub timstamp: Option, + pub timestamp: Option, pub title: Option, pub body: String, } @@ -61,7 +61,7 @@ impl fmt::Display for Message { self.recipients.iter().try_for_each(|r| write!(f, " {r}"))?; writeln!(f)?; } - if let Some(ref t) = self.timstamp { + if let Some(ref t) = self.timestamp { writeln!(f, "@ {t}")?; } write!(f, "{}\r\n", self.body) @@ -85,14 +85,4 @@ mod tests { let rec: Recipients = REC.parse().unwrap(); assert_eq!(rec.to_string(), REC); } - - #[test] - fn parse_message() { - todo!() - } - - #[test] - fn print_message() { - todo!() - } } diff --git a/src/message/parser.rs b/src/message/parser.rs index b016989..5d2c44c 100644 --- a/src/message/parser.rs +++ b/src/message/parser.rs @@ -45,26 +45,34 @@ impl Parser { if let Some(t) = s.strip_prefix("###").map(|x| x.trim().to_string()) { self.title = Some(t); } + if !self.body.is_empty() { + self.body.push('\n'); + } self.body.push_str(s); - self.body.push('\n'); } s if s.starts_with("##") && self.title.is_none() => { if let Some(t) = s.strip_prefix("##").map(|x| x.trim().to_string()) { self.title = Some(t); } + if !self.body.is_empty() { + self.body.push('\n'); + } self.body.push_str(s); - self.body.push('\n'); } s if s.starts_with("#") && self.title.is_none() => { if let Some(t) = s.strip_prefix("#").map(|x| x.trim().to_string()) { self.title = Some(t); } + if !self.body.is_empty() { + self.body.push('\n'); + } self.body.push_str(s); - self.body.push('\n'); } s => { + if !self.body.is_empty() { + self.body.push('\n'); + } self.body.push_str(s); - self.body.push('\n'); } } } @@ -76,10 +84,38 @@ impl Parser { from: self.from.unwrap(), senders: self.senders, recipients: self.recipients.boxes, - timstamp: self.timestamp, + timestamp: self.timestamp, title: self.title, body: self.body, }) } } } + +#[cfg(test)] +mod tests { + use super::*; + + const RAW: &'static str = include_str!("../../test/ox42sc69.gmi"); + + #[test] + fn parse_message() { + let msg: Message = Parser::new("ox42sc69").parse(RAW).unwrap(); + assert_eq!(msg.id, "ox42sc69"); + assert_eq!(msg.from.to_string(), "joe@gemini.example.org"); + assert_eq!(msg.timestamp.unwrap(), "2023-06-07T16:09:42Z"); + assert!(msg.senders.is_empty()); + assert!(msg.recipients.is_empty()); + assert_eq!(msg.title.unwrap(), "How 'bout dose Bears?"); + assert_eq!(msg.body, "# How 'bout dose Bears?\nWhen are they coming for dinner anyway?\n"); + } + + #[test] + fn print_message() { + let msg: Message = Parser::new("ox42sc69").parse(RAW).unwrap(); + assert_eq!( + msg.to_string(), + "< joe@gemini.example.org\n@ 2023-06-07T16:09:42Z\n# How 'bout dose Bears?\nWhen are they coming for dinner anyway?\n\r\n" + ); + } +} diff --git a/test/ox42sc69.gmi b/test/ox42sc69.gmi new file mode 100644 index 0000000..900b67e --- /dev/null +++ b/test/ox42sc69.gmi @@ -0,0 +1,5 @@ +< joe@gemini.example.org +@ 2023-06-07T16:09:42Z +# How 'bout dose Bears? +When are they coming for dinner anyway? +