diff --git a/Cargo.toml b/Cargo.toml index 06ae7b2..d875710 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,7 +14,6 @@ version = "0.7.0" authors = ["paul@colomiets.name"] [dependencies] -quick-error = "2.0.0" hostname = { version = "^0.3", optional = true } [features] diff --git a/src/grammar.rs b/src/grammar.rs index 14fad22..8b4a779 100644 --- a/src/grammar.rs +++ b/src/grammar.rs @@ -3,42 +3,62 @@ use std::str::{Utf8Error, from_utf8}; use {AddrParseError, Config, Network, Lookup, Family}; -quick_error!{ - /// Error while parsing resolv.conf file - #[derive(Debug)] - pub enum ParseError { - /// Error that may be returned when the string to parse contains invalid UTF-8 sequences - InvalidUtf8(line: usize, err: Utf8Error) { - display("bad unicode at line {}: {}", line, err) - source(err) - } - /// Error returned a value for a given directive is invalid. - /// This can also happen when the value is missing, if the directive requires a value. - InvalidValue(line: usize) { - display("directive at line {} is improperly formatted \ - or contains invalid value", line) - } - /// Error returned when a value for a given option is invalid. - /// This can also happen when the value is missing, if the option requires a value. - InvalidOptionValue(line: usize) { - display("directive options at line {} contains invalid \ - value of some option", line) - } - /// Error returned when a invalid option is found. - InvalidOption(line: usize) { - display("option at line {} is not recognized", line) - } - /// Error returned when a invalid directive is found. - InvalidDirective(line: usize) { - display("directive at line {} is not recognized", line) - } - /// Error returned when a value cannot be parsed an an IP address. - InvalidIp(line: usize, err: AddrParseError) { - display("directive at line {} contains invalid IP: {}", line, err) +/// Error while parsing resolv.conf file +#[derive(Debug)] +pub enum ParseError { + /// Error that may be returned when the string to parse contains invalid UTF-8 sequences + InvalidUtf8(usize, Utf8Error), + /// Error returned a value for a given directive is invalid. + /// This can also happen when the value is missing, if the directive requires a value. + InvalidValue(usize), + /// Error returned when a value for a given option is invalid. + /// This can also happen when the value is missing, if the option requires a value. + InvalidOptionValue(usize), + /// Error returned when a invalid option is found. + InvalidOption(usize), + /// Error returned when a invalid directive is found. + InvalidDirective(usize), + /// Error returned when a value cannot be parsed an an IP address. + InvalidIp(usize, AddrParseError), + /// Error returned when there is extra data at the end of a line. + ExtraData(usize), +} + +impl std::fmt::Display for ParseError { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + match self { + ParseError::InvalidUtf8(line, err) => { + write!(f, "bad unicode at line {}: {}", line, err) + } + ParseError::InvalidValue(line) => write!( + f, + "directive at line {} is improperly formatted or contains invalid value", + line + ), + ParseError::InvalidOptionValue(line) => write!( + f, + "directive options at line {} contains invalid value of some option", + line + ), + ParseError::InvalidOption(line) => { + write!(f, "option at line {} is not recognized", line) + } + ParseError::InvalidDirective(line) => { + write!(f, "directive at line {} is not recognized", line) + } + ParseError::InvalidIp(line, err) => { + write!(f, "directive at line {} contains invalid IP: {}", line, err) + } + ParseError::ExtraData(line) => write!(f, "extra data at the end of the line {}", line), } - /// Error returned when there is extra data at the end of a line. - ExtraData(line: usize) { - display("extra data at the end of the line {}", line) + } +} + +impl std::error::Error for ParseError { + fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { + match self { + ParseError::InvalidUtf8(_, err) => Some(err), + _ => None, } } } diff --git a/src/lib.rs b/src/lib.rs index a55a9d7..b6f064a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -91,8 +91,6 @@ #![warn(missing_debug_implementations)] #![warn(missing_docs)] -#[macro_use] -extern crate quick_error; #[cfg(feature = "system")] extern crate hostname;