From b6979b750c40a26ea1c665e064b17820cfd2c0eb Mon Sep 17 00:00:00 2001 From: Nick Alexander Date: Thu, 16 Feb 2017 16:06:07 -0800 Subject: [PATCH] Add map functions for Error<> and Info<> ranges. (#86) It's possible to `map_err_range` for `ParseResult<>` too, but it's awkward because the output type needs to be a compatible `StreamOnce`. As suggested in https://github.com/Marwes/combine/issues/86#issuecomment-280487165, it's probably best to either change the parse result type entirely, or wait for https://github.com/rust-lang/rust/issues/21903. This at least helps consumers convert `ParseError<>` into something that can implement `std::fmt::Display`. --- src/primitives.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/primitives.rs b/src/primitives.rs index 47acca32..c84df147 100644 --- a/src/primitives.rs +++ b/src/primitives.rs @@ -56,6 +56,18 @@ pub enum Info { Borrowed(&'static str), } +impl Info { + pub fn map_range(self, f: F) -> Info where F: FnOnce(R) -> S { + use self::Info::*; + match self { + Token(t) => Token(t), + Range(r) => Range(f(r)), + Owned(s) => Owned(s), + Borrowed(x) => Borrowed(x), + } + } +} + impl PartialEq for Info { fn eq(&self, other: &Info) -> bool { match (self, other) { @@ -110,6 +122,18 @@ pub enum Error { Other(Box), } +impl Error { + pub fn map_err_range(self, f: F) -> Error where F: FnOnce(R) -> S { + use self::Error::*; + match self { + Unexpected(x) => Unexpected(x.map_range(f)), + Expected(x) => Expected(x.map_range(f)), + Message(x) => Message(x.map_range(f)), + Other(x) => Other(x), + } + } +} + impl PartialEq for Error { fn eq(&self, other: &Error) -> bool { match (self, other) {