Skip to content

Commit

Permalink
Implement std::error::Error
Browse files Browse the repository at this point in the history
  • Loading branch information
pitdicker committed Apr 14, 2024
1 parent 481453c commit 5e327ee
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 7 deletions.
30 changes: 30 additions & 0 deletions src/line.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::fmt;
use std::str::FromStr;
// we still support rust that doesn't have the inherent methods
#[allow(deprecated, unused_imports)]
Expand Down Expand Up @@ -32,6 +33,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 {
Expand Down
19 changes: 12 additions & 7 deletions src/table.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
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};
Expand Down Expand Up @@ -352,12 +351,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> {}

0 comments on commit 5e327ee

Please sign in to comment.