diff --git a/parse-zoneinfo/src/line.rs b/parse-zoneinfo/src/line.rs index 725fdca..2fb0c10 100644 --- a/parse-zoneinfo/src/line.rs +++ b/parse-zoneinfo/src/line.rs @@ -73,6 +73,7 @@ //! }))); //! ``` +use std::fmt; use std::str::FromStr; // we still support rust that doesn't have the inherent methods #[allow(deprecated, unused_imports)] @@ -107,6 +108,35 @@ pub enum Error { NotParsedAsLinkLine, } +impl fmt::Display for Error { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match self { + Error::FailedYearParse(s) => write!(f, "failed to parse as a year value: \"{}\"", s), + Error::FailedMonthParse(s) => write!(f, "failed to parse as a month value: \"{}\"", s), + Error::FailedWeekdayParse(s) => { + write!(f, "failed to parse as a weekday value: \"{}\"", s) + } + Error::InvalidLineType(s) => write!(f, "line with invalid format: \"{}\"", s), + Error::TypeColumnContainedNonHyphen(s) => write!( + f, + "'type' column is not a hyphen but has the value: \"{}\"", + s + ), + Error::CouldNotParseSaving(s) => write!(f, "failed to parse RULES column: \"{}\"", s), + Error::InvalidDaySpec(s) => write!(f, "invalid day specification ('ON'): \"{}\"", s), + Error::InvalidTimeSpecAndType(s) => write!(f, "invalid time: \"{}\"", s), + Error::NonWallClockInTimeSpec(s) => { + write!(f, "time value not given as wall time: \"{}\"", s) + } + Error::NotParsedAsRuleLine => write!(f, "failed to parse line as a rule"), + Error::NotParsedAsZoneLine => write!(f, "failed to parse line as a zone"), + Error::NotParsedAsLinkLine => write!(f, "failed to parse line as a link"), + } + } +} + +impl std::error::Error for Error {} + impl Default for LineParser { fn default() -> Self { LineParser { diff --git a/parse-zoneinfo/src/table.rs b/parse-zoneinfo/src/table.rs index 0de5682..05dee78 100644 --- a/parse-zoneinfo/src/table.rs +++ b/parse-zoneinfo/src/table.rs @@ -35,7 +35,6 @@ //! ``` use std::collections::hash_map::{Entry, HashMap}; -use std::error::Error as ErrorTrait; use std::fmt; use crate::line::{self, ChangeTime, DaySpec, Month, TimeType, Year}; @@ -388,12 +387,18 @@ pub enum Error<'line> { impl<'line> fmt::Display for Error<'line> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "{}", self.description()) + match self { + Error::SurpriseContinuationLine => write!( + f, + "continuation line follows line that isn't a zone definition line" + ), + Error::UnknownRuleset(_) => { + write!(f, "zone definition refers to a ruleset that isn't defined") + } + Error::DuplicateLink(_) => write!(f, "link line with name that already exists"), + Error::DuplicateZone => write!(f, "zone line with name that already exists"), + } } } -impl<'line> ErrorTrait for Error<'line> { - fn description(&self) -> &str { - "interpretation error" - } -} +impl<'line> std::error::Error for Error<'line> {}