diff options
author | Daniel <kingdread@gmx.de> | 2020-04-21 13:59:28 +0200 |
---|---|---|
committer | Daniel <kingdread@gmx.de> | 2020-04-21 13:59:28 +0200 |
commit | 185a5b2f802f9d05c3eb40f807c0488f168c6661 (patch) | |
tree | aef0c8939dc9ccb9b92538bc1306c6431bdefa64 /src/fexpr/mod.rs | |
parent | 0e4e148a0890ba206df40cffe5a5f1cc47c8079e (diff) | |
download | raidgrep-185a5b2f802f9d05c3eb40f807c0488f168c6661.tar.gz raidgrep-185a5b2f802f9d05c3eb40f807c0488f168c6661.tar.bz2 raidgrep-185a5b2f802f9d05c3eb40f807c0488f168c6661.zip |
better error outputs
Diffstat (limited to 'src/fexpr/mod.rs')
-rw-r--r-- | src/fexpr/mod.rs | 57 |
1 files changed, 46 insertions, 11 deletions
diff --git a/src/fexpr/mod.rs b/src/fexpr/mod.rs index aafdea7..5754d94 100644 --- a/src/fexpr/mod.rs +++ b/src/fexpr/mod.rs @@ -4,28 +4,63 @@ //! type and convert it to a [`Filter`][super::filters::Filter]. // Make it available in the grammar mod. use super::{filters, FightOutcome, SearchField, Weekday}; -use lalrpop_util::{lalrpop_mod, lexer::Token, ParseError}; +use std::{error, fmt}; + +use lalrpop_util::{lalrpop_mod, lexer::Token, ParseError}; use thiserror::Error; -lalrpop_mod!(pub grammar, "/fexpr/grammar.rs"); +lalrpop_mod!(#[allow(clippy::all)] pub grammar, "/fexpr/grammar.rs"); + +#[derive(Debug)] +pub struct FError { + location: usize, + data: String, + kind: FErrorKind, +} + +impl fmt::Display for FError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{} (at {})", self.kind, self.location) + } +} + +impl error::Error for FError { + fn source(&self) -> Option<&(dyn error::Error + 'static)> { + Some(&self.kind) + } +} #[derive(Debug, Error)] -pub enum FError { +pub enum FErrorKind { #[error("invalid regular expression: {0}")] - InvalidRegex(String), - #[error("invalid fight outcome: {0}")] - InvalidFightOutcome(String), - #[error("invalid weekday: {0}")] - InvalidWeekday(String), + InvalidRegex(#[from] regex::Error), + #[error("invalid fight outcome")] + InvalidFightOutcome, + #[error("invalid weekday")] + InvalidWeekday, #[error("invalid timestamp: {0}")] - InvalidTimestamp(String), - #[error("invalid boss name: {0}")] - InvalidBoss(String), + InvalidTimestamp(#[from] chrono::format::ParseError), + #[error("invalid boss name")] + InvalidBoss, } +/// Shortcut to create a new parser and parse the given input. pub fn parse_logfilter( input: &str, ) -> Result<Box<dyn filters::log::LogFilter>, ParseError<usize, Token, FError>> { grammar::LogFilterParser::new().parse(input) } + +/// Extract the location from the given error. +pub fn location<T>(err: &ParseError<usize, T, FError>) -> usize { + match *err { + ParseError::InvalidToken { location } => location, + ParseError::UnrecognizedEOF { location, .. } => location, + ParseError::UnrecognizedToken { + token: (l, _, _), .. + } => l, + ParseError::ExtraToken { token: (l, _, _) } => l, + ParseError::User { ref error } => error.location, + } +} |