use std::fmt;
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ParseError {
pub pos: usize,
pub message: String,
}
impl ParseError {
pub fn new(pos: usize, message: impl Into<String>) -> ParseError {
ParseError {
pos,
message: message.into(),
}
}
}
impl fmt::Display for ParseError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "parse error at {}: {}", self.pos, self.message)
}
}
impl std::error::Error for ParseError {}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Warning {
pub pos: usize,
pub message: String,
}
impl Warning {
pub fn new(pos: usize, message: impl Into<String>) -> Warning {
Warning {
pos,
message: message.into(),
}
}
}
impl fmt::Display for Warning {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "warning at {}: {}", self.pos, self.message)
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct UnknownTimeZone {
pub id: String,
pub message: String,
}
impl UnknownTimeZone {
pub fn new(id: impl Into<String>, message: impl Into<String>) -> UnknownTimeZone {
UnknownTimeZone {
id: id.into(),
message: message.into(),
}
}
}
impl fmt::Display for UnknownTimeZone {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "unknown time zone {}: {}", self.id, self.message)
}
}
impl std::error::Error for UnknownTimeZone {}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parse_error_displays_position_and_message() {
let e = ParseError::new(7, "bad selector");
assert_eq!(e.to_string(), "parse error at 7: bad selector");
}
#[test]
fn warning_displays_position_and_message() {
let w = Warning::new(3, "unsatisfiable");
assert_eq!(w.to_string(), "warning at 3: unsatisfiable");
}
}