From a0e3c53758d526bb418c068bce1c99fa5a597ed3 Mon Sep 17 00:00:00 2001 From: Philipp A Date: Thu, 26 Dec 2019 23:01:00 +0100 Subject: Split into smaller crates --- parser/src/pair_ext_parse.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 parser/src/pair_ext_parse.rs (limited to 'parser/src/pair_ext_parse.rs') diff --git a/parser/src/pair_ext_parse.rs b/parser/src/pair_ext_parse.rs new file mode 100644 index 0000000..a04b3dd --- /dev/null +++ b/parser/src/pair_ext_parse.rs @@ -0,0 +1,21 @@ +use std::str::FromStr; + +use pest::Span; +use pest::iterators::Pair; +use pest::error::{Error,ErrorVariant}; + + +pub trait PairExt where R: pest::RuleType { + fn parse(&self) -> Result> where T: FromStr, E: ToString; +} + +impl<'l, R> PairExt for Pair<'l, R> where R: pest::RuleType { + fn parse(&self) -> Result> where T: FromStr, E: ToString { + self.as_str().parse().map_err(|e| to_parse_error(self.as_span(), &e)) + } +} + +pub(crate) fn to_parse_error(span: Span, e: &E) -> Error where E: ToString, R: pest::RuleType { + let var: ErrorVariant = ErrorVariant::CustomError { message: e.to_string() }; + Error::new_from_span(var, span) +} -- cgit v1.2.3