diff --git a/src/bits/mod.rs b/src/bits/mod.rs index 07a8deac..4b56821e 100644 --- a/src/bits/mod.rs +++ b/src/bits/mod.rs @@ -6,8 +6,8 @@ pub mod streaming; #[cfg(test)] mod tests; -use crate::error::{ErrorKind, ParseError}; -use crate::input::{ErrorConvert, InputIsStreaming, InputIter, InputLength, Slice, ToUsize}; +use crate::error::{ErrorConvert, ErrorKind, ParseError}; +use crate::input::{InputIsStreaming, InputIter, InputLength, Slice, ToUsize}; use crate::lib::std::ops::{AddAssign, RangeFrom, Shl, Shr}; use crate::{Err, IResult, Needed, Parser}; diff --git a/src/error.rs b/src/error.rs index e22916b5..897bbe4d 100644 --- a/src/error.rs +++ b/src/error.rs @@ -517,6 +517,12 @@ pub trait FromExternalError { fn from_external_error(input: I, kind: ErrorKind, e: E) -> Self; } +/// Equivalent From implementation to avoid orphan rules in bits parsers +pub trait ErrorConvert { + /// Transform to another error type + fn convert(self) -> E; +} + /// default error type, only contains the error' location and code #[derive(Debug, Eq, PartialEq)] pub struct Error { @@ -552,6 +558,24 @@ impl FromExternalError for Error { } } +impl ErrorConvert> for Error { + fn convert(self) -> Error<(I, usize)> { + Error { + input: (self.input, 0), + code: self.code, + } + } +} + +impl ErrorConvert> for Error<(I, usize)> { + fn convert(self) -> Error { + Error { + input: self.input.0, + code: self.code, + } + } +} + /// The Display implementation allows the `std::error::Error` implementation impl fmt::Display for Error { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { @@ -582,6 +606,18 @@ impl FromExternalError for (I, ErrorKind) { } } +impl ErrorConvert<(I, ErrorKind)> for ((I, usize), ErrorKind) { + fn convert(self) -> (I, ErrorKind) { + ((self.0).0, self.1) + } +} + +impl ErrorConvert<((I, usize), ErrorKind)> for (I, ErrorKind) { + fn convert(self) -> ((I, usize), ErrorKind) { + ((self.0, 0), self.1) + } +} + impl ParseError for () { fn from_error_kind(_: I, _: ErrorKind) -> Self {} @@ -594,6 +630,10 @@ impl FromExternalError for () { fn from_external_error(_input: I, _kind: ErrorKind, _e: E) -> Self {} } +impl ErrorConvert<()> for () { + fn convert(self) {} +} + /// Creates an error from the input position and an [`ErrorKind`] #[deprecated(since = "8.0.0", note = "Replaced with `ParseError::from_error_kind`")] pub fn make_error>(input: I, kind: ErrorKind) -> E { @@ -659,6 +699,24 @@ impl FromExternalError for VerboseError { } } +#[cfg(feature = "alloc")] +impl ErrorConvert> for VerboseError<(I, usize)> { + fn convert(self) -> VerboseError { + VerboseError { + errors: self.errors.into_iter().map(|(i, e)| (i.0, e)).collect(), + } + } +} + +#[cfg(feature = "alloc")] +impl ErrorConvert> for VerboseError { + fn convert(self) -> VerboseError<(I, usize)> { + VerboseError { + errors: self.errors.into_iter().map(|(i, e)| ((i, 0), e)).collect(), + } + } +} + #[cfg(feature = "alloc")] impl fmt::Display for VerboseError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { diff --git a/src/input.rs b/src/input.rs index 6fe4bf6c..af8bdc3f 100644 --- a/src/input.rs +++ b/src/input.rs @@ -2719,65 +2719,6 @@ impl ToUsize for u64 { } } -/// Equivalent From implementation to avoid orphan rules in bits parsers -pub trait ErrorConvert { - /// Transform to another error type - fn convert(self) -> E; -} - -impl ErrorConvert<(I, ErrorKind)> for ((I, usize), ErrorKind) { - fn convert(self) -> (I, ErrorKind) { - ((self.0).0, self.1) - } -} - -impl ErrorConvert<((I, usize), ErrorKind)> for (I, ErrorKind) { - fn convert(self) -> ((I, usize), ErrorKind) { - ((self.0, 0), self.1) - } -} - -use crate::error; -impl ErrorConvert> for error::Error<(I, usize)> { - fn convert(self) -> error::Error { - error::Error { - input: self.input.0, - code: self.code, - } - } -} - -impl ErrorConvert> for error::Error { - fn convert(self) -> error::Error<(I, usize)> { - error::Error { - input: (self.input, 0), - code: self.code, - } - } -} - -#[cfg(feature = "alloc")] -impl ErrorConvert> for error::VerboseError<(I, usize)> { - fn convert(self) -> error::VerboseError { - error::VerboseError { - errors: self.errors.into_iter().map(|(i, e)| (i.0, e)).collect(), - } - } -} - -#[cfg(feature = "alloc")] -impl ErrorConvert> for error::VerboseError { - fn convert(self) -> error::VerboseError<(I, usize)> { - error::VerboseError { - errors: self.errors.into_iter().map(|(i, e)| ((i, 0), e)).collect(), - } - } -} - -impl ErrorConvert<()> for () { - fn convert(self) {} -} - /// Helper trait to show a byte slice as a hex dump #[cfg(feature = "std")] pub trait HexDisplay {