diff --git a/README.md b/README.md index cf16005..db12183 100644 --- a/README.md +++ b/README.md @@ -104,7 +104,7 @@ R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7 ); assert_eq!( - message.get_date().unwrap().to_iso8601(), + message.get_date().unwrap().to_rfc3339(), "2021-11-20T14:22:01-08:00" ); diff --git a/examples/message_parse.rs b/examples/message_parse.rs index 257110f..8c00759 100644 --- a/examples/message_parse.rs +++ b/examples/message_parse.rs @@ -82,7 +82,7 @@ R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7 ); assert_eq!( - message.get_date().unwrap().to_iso8601(), + message.get_date().unwrap().to_rfc3339(), "2021-11-20T14:22:01-08:00" ); diff --git a/src/lib.rs b/src/lib.rs index 4d29948..ee09549 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -183,7 +183,7 @@ //! ); //! //! assert_eq!( -//! message.get_date().unwrap().to_iso8601(), +//! message.get_date().unwrap().to_rfc3339(), //! "2021-11-20T14:22:01-08:00" //! ); //! @@ -767,15 +767,15 @@ pub struct ContentType<'x> { #[derive(Clone, Debug, PartialEq, Eq)] #[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))] pub struct DateTime { - pub year: u32, - pub month: u32, - pub day: u32, - pub hour: u32, - pub minute: u32, - pub second: u32, + pub year: u16, + pub month: u8, + pub day: u8, + pub hour: u8, + pub minute: u8, + pub second: u8, pub tz_before_gmt: bool, - pub tz_hour: u32, - pub tz_minute: u32, + pub tz_hour: u8, + pub tz_minute: u8, } impl<'x> Message<'x> { diff --git a/src/mailbox/maildir.rs b/src/mailbox/maildir.rs index 4a959b2..513a550 100644 --- a/src/mailbox/maildir.rs +++ b/src/mailbox/maildir.rs @@ -35,6 +35,7 @@ pub struct Message { internal_date: u64, flags: Vec, contents: Vec, + path: PathBuf, } /// Flags of Maildir message @@ -232,6 +233,7 @@ impl Iterator for MessageIterator { contents, internal_date, flags, + path: path.to_path_buf(), })); } } @@ -241,14 +243,6 @@ impl Iterator for MessageIterator { } impl Message { - pub fn new(internal_date: u64, flags: Vec, contents: Vec) -> Message { - Message { - internal_date, - flags, - contents, - } - } - /// Returns the message creation date in seconds since UNIX epoch pub fn internal_date(&self) -> u64 { self.internal_date @@ -259,6 +253,11 @@ impl Message { &self.flags } + /// Returns the path to the message file + pub fn path(&self) -> &Path { + &self.path + } + /// Returns the message contents pub fn contents(&self) -> &[u8] { &self.contents @@ -292,6 +291,7 @@ mod tests { internal_date: 0, flags: vec![Flag::Seen], contents: vec![98, 10], + path: "unknown".into(), }, ), ( @@ -300,6 +300,7 @@ mod tests { internal_date: 0, flags: vec![Flag::Seen, Flag::Trashed], contents: vec![97, 10], + path: "unknown".into(), }, ), ( @@ -308,6 +309,7 @@ mod tests { internal_date: 0, flags: vec![], contents: vec![100, 10], + path: "unknown".into(), }, ), ( @@ -316,6 +318,7 @@ mod tests { internal_date: 0, flags: vec![Flag::Trashed, Flag::Draft, Flag::Replied], contents: vec![99, 10], + path: "unknown".into(), }, ), ( @@ -324,6 +327,7 @@ mod tests { internal_date: 0, flags: vec![Flag::Replied, Flag::Draft, Flag::Flagged], contents: vec![102, 10], + path: "unknown".into(), }, ), ( @@ -332,6 +336,7 @@ mod tests { internal_date: 0, flags: vec![Flag::Flagged, Flag::Passed], contents: vec![101, 10], + path: "unknown".into(), }, ), ]; @@ -343,7 +348,9 @@ mod tests { for message in folder { let mut message = message.unwrap(); assert_ne!(message.internal_date(), 0); + assert!(message.path.exists()); message.internal_date = 0; + message.path = PathBuf::from("unknown"); messages.push((name.clone(), message)); } } diff --git a/src/mailbox/mbox.rs b/src/mailbox/mbox.rs index 0475559..7fa77d8 100644 --- a/src/mailbox/mbox.rs +++ b/src/mailbox/mbox.rs @@ -112,12 +112,12 @@ impl Message { .and_then(|hdr| hdr.split_once(' ')) { let mut dt = DateTime { - year: u32::MAX, - month: u32::MAX, - day: u32::MAX, - hour: u32::MAX, - minute: u32::MAX, - second: u32::MAX, + year: u16::MAX, + month: u8::MAX, + day: u8::MAX, + hour: u8::MAX, + minute: u8::MAX, + second: u8::MAX, tz_before_gmt: false, tz_hour: 0, tz_minute: 0, @@ -151,23 +151,23 @@ impl Message { } else if part.eq_ignore_ascii_case("dec") { 12 } else { - u32::MAX + u8::MAX }; } 2 => { - dt.day = part.parse().unwrap_or(u32::MAX); + dt.day = part.parse().unwrap_or(u8::MAX); } 3 => { for (pos, part) in part.split(':').enumerate() { match pos { 0 => { - dt.hour = part.parse().unwrap_or(u32::MAX); + dt.hour = part.parse().unwrap_or(u8::MAX); } 1 => { - dt.minute = part.parse().unwrap_or(u32::MAX); + dt.minute = part.parse().unwrap_or(u8::MAX); } 2 => { - dt.second = part.parse().unwrap_or(u32::MAX); + dt.second = part.parse().unwrap_or(u8::MAX); } _ => { break; @@ -176,7 +176,7 @@ impl Message { } } 4 => { - dt.year = part.parse().unwrap_or(u32::MAX); + dt.year = part.parse().unwrap_or(u16::MAX); } _ => (), } diff --git a/src/parsers/fields/date.rs b/src/parsers/fields/date.rs index 06c12fd..a09a762 100644 --- a/src/parsers/fields/date.rs +++ b/src/parsers/fields/date.rs @@ -14,24 +14,42 @@ use std::fmt; use crate::{parsers::message::MessageStream, DateTime, HeaderValue}; impl DateTime { - /// Returns an ISO-8601 representation of the parsed RFC5322 datetime field - pub fn to_iso8601(&self) -> String { - format!( - "{:04}-{:02}-{:02}T{:02}:{:02}:{:02}{}{:02}:{:02}", - self.year, - self.month, - self.day, - self.hour, - self.minute, - self.second, - if self.tz_before_gmt && (self.tz_hour > 0 || self.tz_minute > 0) { - "-" - } else { - "+" - }, - self.tz_hour, - self.tz_minute - ) + /// Parses an RFC822 date + pub fn parse_rfc822(&self, value: &str) -> Option { + match parse_date(&mut MessageStream { + data: value.as_bytes(), + pos: 0, + }) { + HeaderValue::DateTime(dt) => dt.into(), + _ => None, + } + } + + /// Returns an RFC3339 representation of the parsed RFC5322 datetime field + pub fn to_rfc3339(&self) -> String { + if self.tz_hour != 0 || self.tz_minute != 0 { + format!( + "{:04}-{:02}-{:02}T{:02}:{:02}:{:02}{}{:02}:{:02}", + self.year, + self.month, + self.day, + self.hour, + self.minute, + self.second, + if self.tz_before_gmt && (self.tz_hour > 0 || self.tz_minute > 0) { + "-" + } else { + "+" + }, + self.tz_hour, + self.tz_minute + ) + } else { + format!( + "{:04}-{:02}-{:02}T{:02}:{:02}:{:02}Z", + self.year, self.month, self.day, self.hour, self.minute, self.second, + ) + } } /// Returns true if the date is valid @@ -50,9 +68,10 @@ impl DateTime { /// or None if the date is invalid. pub fn to_timestamp(&self) -> i64 { // Ported from https://github.com/protocolbuffers/upb/blob/22182e6e/upb/json_decode.c#L982-L992 + let month = self.month as u32; let year_base = 4800; /* Before min year, multiple of 400. */ - let m_adj = self.month.wrapping_sub(3); /* March-based month. */ - let carry = if m_adj > self.month { 1 } else { 0 }; + let m_adj = month.wrapping_sub(3); /* March-based month. */ + let carry = if m_adj > month { 1 } else { 0 }; let adjust = if carry > 0 { 12 } else { 0 }; let y_adj = self.year as i64 + year_base - carry; let month_days = ((m_adj.wrapping_add(adjust)) * 62719 + 769) / 2048; @@ -84,7 +103,7 @@ impl Ord for DateTime { impl fmt::Display for DateTime { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { - fmt.write_str(&self.to_iso8601()) + fmt.write_str(&self.to_rfc3339()) } } @@ -207,18 +226,18 @@ pub fn parse_date<'x>(stream: &mut MessageStream<'x>) -> HeaderValue<'x> { parts[2] + 1900 } else { parts[2] - }, + } as u16, month: if month_pos == 3 && month_hash <= 30 { - MONTH_MAP[month_hash] as u32 + MONTH_MAP[month_hash] as u8 } else { - parts[1] + parts[1] as u8 }, - day: parts[0], - hour: parts[3], - minute: parts[4], - second: parts[5], - tz_hour: parts[6] / 100, - tz_minute: parts[6] % 100, + day: parts[0] as u8, + hour: parts[3] as u8, + minute: parts[4] as u8, + second: parts[5] as u8, + tz_hour: (parts[6] / 100) as u8, + tz_minute: (parts[6] % 100) as u8, tz_before_gmt: !is_plus, }) } else { @@ -288,7 +307,7 @@ mod tests { "Tue, 1 Jul 2003 ((tricky)\n comment) 10:52:37 +0200", "2003-07-01T10:52:37+02:00", ), - ("21 Nov 97 09:55:06 GMT", "1997-11-21T09:55:06+00:00"), + ("21 Nov 97 09:55:06 GMT", "1997-11-21T09:55:06Z"), ( "20 11 (some \n 44 comments(more comments\n )) 79 05:34:27 -0300", "1979-11-20T05:34:27-03:00", @@ -315,11 +334,10 @@ mod tests { ("13 zzz 2021 09:55:06 +0200", "2021-00-13T09:55:06+02:00"), ]; - for input in inputs { - let str = input.0.to_string(); - match parse_date(&mut MessageStream::new(str.as_bytes())) { + for (input, expected_result) in inputs { + match parse_date(&mut MessageStream::new(input.as_bytes())) { HeaderValue::DateTime(datetime) => { - assert_eq!(input.1, datetime.to_iso8601()); + assert_eq!(expected_result, datetime.to_rfc3339()); if datetime.is_valid() { if let LocalResult::Single(chrono_datetime) @@ -328,14 +346,21 @@ mod tests { * if datetime.tz_before_gmt { 1i32 } else { -1i32 }, ) .unwrap_or_else(|| FixedOffset::east(0)) - .ymd_opt(datetime.year as i32, datetime.month, datetime.day) - .and_hms_opt(datetime.hour, datetime.minute, datetime.second) - { + .ymd_opt( + datetime.year as i32, + datetime.month as u32, + datetime.day as u32, + ) + .and_hms_opt( + datetime.hour as u32, + datetime.minute as u32, + datetime.second as u32, + ) { assert_eq!( chrono_datetime.timestamp(), datetime.to_timestamp(), "{} -> {} ({}) -> {} ({})", - input.0.escape_debug(), + input.escape_debug(), datetime.to_timestamp(), Utc.timestamp_opt(datetime.to_timestamp(), 0) .unwrap() @@ -350,7 +375,7 @@ mod tests { } HeaderValue::Empty => { //println!("{} -> None", input.0.escape_debug()); - assert!(input.1.is_empty()); + assert!(expected_result.is_empty()); } _ => panic!("Unexpected result"), } diff --git a/tests/integration_test.rs b/tests/integration_test.rs index 2509e95..a26d7b2 100644 --- a/tests/integration_test.rs +++ b/tests/integration_test.rs @@ -90,7 +90,7 @@ R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7 ); assert_eq!( - message.get_date().unwrap().to_iso8601(), + message.get_date().unwrap().to_rfc3339(), "2021-11-20T14:22:01-08:00" );