diff options
| author | Philipp A | 2019-12-26 23:01:00 +0100 | 
|---|---|---|
| committer | Philipp A | 2019-12-26 23:36:48 +0100 | 
| commit | a0e3c53758d526bb418c068bce1c99fa5a597ed3 (patch) | |
| tree | e640238b011a9ea7806ccccaf1a435e4b371a376 /parser/src/pair_ext_parse.rs | |
| parent | 7018f5d3c42f18b6c83f398db9f1915361a7c679 (diff) | |
| download | rust-rst-a0e3c53758d526bb418c068bce1c99fa5a597ed3.tar.bz2 | |
Split into smaller crates
Diffstat (limited to 'parser/src/pair_ext_parse.rs')
| -rw-r--r-- | parser/src/pair_ext_parse.rs | 21 | 
1 files changed, 21 insertions, 0 deletions
| 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<R> where R: pest::RuleType { +	fn parse<T, E>(&self) -> Result<T, Error<R>> where T: FromStr<Err = E>, E: ToString; +} + +impl<'l, R> PairExt<R> for Pair<'l, R> where R: pest::RuleType { +	fn parse<T, E>(&self) -> Result<T, Error<R>> where T: FromStr<Err = E>, E: ToString { +		self.as_str().parse().map_err(|e| to_parse_error(self.as_span(), &e)) +	} +} + +pub(crate) fn to_parse_error<E, R>(span: Span, e: &E) -> Error<R> where E: ToString, R: pest::RuleType { +	let var: ErrorVariant<R> = ErrorVariant::CustomError { message: e.to_string() }; +	Error::new_from_span(var, span) +} | 
