From b164f69c45db556a87020d85dbbaa48f1e2e1392 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Sat, 7 Jan 2023 11:37:57 +0200 Subject: refactor: move RikiError::UnknownTimestamp to src/time.rs Sponsored-by: author --- src/directive/meta.rs | 2 +- src/directive/mod.rs | 3 +++ src/error.rs | 6 +++--- src/time.rs | 13 +++++++++---- 4 files changed, 16 insertions(+), 8 deletions(-) diff --git a/src/directive/meta.rs b/src/directive/meta.rs index 7524bef..974a955 100644 --- a/src/directive/meta.rs +++ b/src/directive/meta.rs @@ -32,7 +32,7 @@ impl DirectiveImplementation for Meta { meta.set_title(title.into()); } if let Some(mtime) = &self.date { - meta.set_mtime(parse_timestamp(mtime).map_err(|e| DirectiveError::Riki(Box::new(e)))?); + meta.set_mtime(parse_timestamp(mtime)?); } Ok(Processed::Markdown("".into())) } diff --git a/src/directive/mod.rs b/src/directive/mod.rs index 4edda32..006e334 100644 --- a/src/directive/mod.rs +++ b/src/directive/mod.rs @@ -28,6 +28,9 @@ pub enum DirectiveError { #[error(transparent)] PageSpec(#[from] crate::pagespec::PageSpecError), + #[error(transparent)] + Time(#[from] crate::time::TimeError), + #[error(transparent)] Riki(#[from] Box), } diff --git a/src/error.rs b/src/error.rs index 7d5d2b5..845f08d 100644 --- a/src/error.rs +++ b/src/error.rs @@ -21,15 +21,15 @@ pub enum RikiError { #[error(transparent)] Parser(#[from] crate::parser::ParserError), + #[error(transparent)] + Time(#[from] crate::time::TimeError), + #[error("string formatting error: {0}")] Format(#[source] std::fmt::Error), #[error("link to missing page {1} on {0}")] PageMissing(PathBuf, PathBuf), - #[error("failed to parse date: {0:?}")] - UnknownTimestamp(String), - #[error("failed to process page {0}")] PageProblem(PathBuf, #[source] Box), diff --git a/src/time.rs b/src/time.rs index 98256b1..a9f3502 100644 --- a/src/time.rs +++ b/src/time.rs @@ -1,4 +1,3 @@ -use crate::error::RikiError; use log::trace; use std::time::{Duration, SystemTime}; use time::{ @@ -9,7 +8,13 @@ use time::{ OffsetDateTime, PrimitiveDateTime, }; -pub fn parse_timestamp(timestamp: &str) -> Result { +#[derive(Debug, thiserror::Error)] +pub enum TimeError { + #[error("failed to parse date: {0:?}")] + UnknownTimestamp(String), +} + +pub fn parse_timestamp(timestamp: &str) -> Result { trace!("parsing timestamp {:?}", timestamp); let odt = parse(timestamp)?; let unix = odt.unix_timestamp(); @@ -24,7 +29,7 @@ fn system_time(unix: i64) -> SystemTime { SystemTime::UNIX_EPOCH.checked_add(offset).unwrap() } -fn parse(timestamp: &str) -> Result { +fn parse(timestamp: &str) -> Result { const SIMPLIFIED_ISO9601: &[FormatItem<'static>] = format_description!("[year]-[month]-[day] [hour]:[minute]:[second]"); const SIMPLIFIED_ISO9601_MIN: &[FormatItem<'static>] = @@ -51,7 +56,7 @@ fn parse(timestamp: &str) -> Result { } else if let Ok(t) = parse_one_time_format(timestamp, "RFC2822", &Rfc2822) { Ok(t) } else { - Err(RikiError::UnknownTimestamp(timestamp.into())) + Err(TimeError::UnknownTimestamp(timestamp.into())) } } -- cgit v1.2.1