Use DateTime
struct in GemtextNode::Timestamp
This commit is contained in:
parent
2bf74ecbab
commit
5c82b53a28
3 changed files with 36 additions and 5 deletions
|
@ -1,4 +1,4 @@
|
||||||
use crate::prelude::{Link, Mailbox, Recipients};
|
use crate::prelude::{DateTime, Link, Mailbox, Recipients};
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
|
@ -18,7 +18,7 @@ enum State<'a> {
|
||||||
pub enum GemtextNode {
|
pub enum GemtextNode {
|
||||||
Sender(Mailbox),
|
Sender(Mailbox),
|
||||||
Recipients(Recipients),
|
Recipients(Recipients),
|
||||||
Timestamp(String),
|
Timestamp(DateTime),
|
||||||
Text(String),
|
Text(String),
|
||||||
Heading1(String),
|
Heading1(String),
|
||||||
Heading2(String),
|
Heading2(String),
|
||||||
|
@ -110,7 +110,11 @@ impl<'a> GemtextNode {
|
||||||
let Some(line) = text.strip_prefix('@').map(|x| x.trim()) else {
|
let Some(line) = text.strip_prefix('@').map(|x| x.trim()) else {
|
||||||
return Self::Text(text.to_string());
|
return Self::Text(text.to_string());
|
||||||
};
|
};
|
||||||
Self::Timestamp(line.to_string())
|
if let Ok(dt) = line.parse() {
|
||||||
|
Self::Timestamp(dt)
|
||||||
|
} else {
|
||||||
|
Self::Text(text.to_string())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -326,7 +330,7 @@ mod tests {
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
nodes.next().cloned().unwrap(),
|
nodes.next().cloned().unwrap(),
|
||||||
GemtextNode::Timestamp("2023-06-07T21:27:15Z".to_string()),
|
GemtextNode::Timestamp("2023-06-07T21:27:15Z".parse().unwrap()),
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
nodes.next().cloned().unwrap(),
|
nodes.next().cloned().unwrap(),
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
pub use super::{
|
pub use super::{
|
||||||
certificate::{Certificate, CertificateStore, ClientCertificateStore},
|
certificate::{Certificate, CertificateStore, ClientCertificateStore},
|
||||||
fingerprint::{Error as FingerprintError, Fingerprint, GetFingerprint},
|
fingerprint::{Error as FingerprintError, Fingerprint, GetFingerprint},
|
||||||
gemtext::{GemtextNode, Parser},
|
gemtext::{GemtextNode, Parser as GemtextParser},
|
||||||
host::{Error as ParseHostError, Host},
|
host::{Error as ParseHostError, Host},
|
||||||
mailbox::{Error as ParseMailboxError, Mailbox},
|
mailbox::{Error as ParseMailboxError, Mailbox},
|
||||||
mailuser::Mailuser,
|
mailuser::Mailuser,
|
||||||
|
@ -14,4 +14,5 @@ pub use super::{
|
||||||
AuthenticationFailure, Error as ParseStatusError, PermanentFailure, Redirect, Status,
|
AuthenticationFailure, Error as ParseStatusError, PermanentFailure, Redirect, Status,
|
||||||
TemporaryFailure,
|
TemporaryFailure,
|
||||||
},
|
},
|
||||||
|
time::{Error as ParseTimeError, DateTime, TimeZone, Sign, Parser as TimeParser},
|
||||||
};
|
};
|
||||||
|
|
|
@ -120,6 +120,14 @@ impl PartialOrd for DateTime {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl FromStr for DateTime {
|
||||||
|
type Err = Error;
|
||||||
|
|
||||||
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||||
|
Parser::new(s).parse()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl DateTime {
|
impl DateTime {
|
||||||
pub fn normalize(&self) -> Option<Self> {
|
pub fn normalize(&self) -> Option<Self> {
|
||||||
if self.tz == Some(TimeZone::UTC) {
|
if self.tz == Some(TimeZone::UTC) {
|
||||||
|
@ -266,4 +274,22 @@ mod test {
|
||||||
};
|
};
|
||||||
assert_eq!(dt.to_string(), "2023-05-07Z");
|
assert_eq!(dt.to_string(), "2023-05-07Z");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn ordering() {
|
||||||
|
let mut dates = vec![
|
||||||
|
"2023-06-10T10:01:52Z",
|
||||||
|
"2023-06-10T08:01:52+05",
|
||||||
|
"2023-06-10T11:30:30-05:15",
|
||||||
|
];
|
||||||
|
dates.sort_by(|a, b| a.partial_cmp(b).unwrap());
|
||||||
|
assert_eq!(
|
||||||
|
dates,
|
||||||
|
vec![
|
||||||
|
"2023-06-10T08:01:52+05",
|
||||||
|
"2023-06-10T10:01:52Z",
|
||||||
|
"2023-06-10T11:30:30-05:15",
|
||||||
|
]
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue