Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(error)!: Move ErrorConvert to error module #73

Merged
merged 1 commit into from
Jan 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/bits/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down
58 changes: 58 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,12 @@ pub trait FromExternalError<I, E> {
fn from_external_error(input: I, kind: ErrorKind, e: E) -> Self;
}

/// Equivalent From implementation to avoid orphan rules in bits parsers
pub trait ErrorConvert<E> {
/// 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<I> {
Expand Down Expand Up @@ -552,6 +558,24 @@ impl<I, E> FromExternalError<I, E> for Error<I> {
}
}

impl<I> ErrorConvert<Error<(I, usize)>> for Error<I> {
fn convert(self) -> Error<(I, usize)> {
Error {
input: (self.input, 0),
code: self.code,
}
}
}

impl<I> ErrorConvert<Error<I>> for Error<(I, usize)> {
fn convert(self) -> Error<I> {
Error {
input: self.input.0,
code: self.code,
}
}
}

/// The Display implementation allows the `std::error::Error` implementation
impl<I: fmt::Display> fmt::Display for Error<I> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Expand Down Expand Up @@ -582,6 +606,18 @@ impl<I, E> FromExternalError<I, E> for (I, ErrorKind) {
}
}

impl<I> ErrorConvert<(I, ErrorKind)> for ((I, usize), ErrorKind) {
fn convert(self) -> (I, ErrorKind) {
((self.0).0, self.1)
}
}

impl<I> ErrorConvert<((I, usize), ErrorKind)> for (I, ErrorKind) {
fn convert(self) -> ((I, usize), ErrorKind) {
((self.0, 0), self.1)
}
}

impl<I> ParseError<I> for () {
fn from_error_kind(_: I, _: ErrorKind) -> Self {}

Expand All @@ -594,6 +630,10 @@ impl<I, E> FromExternalError<I, E> 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<I, E: ParseError<I>>(input: I, kind: ErrorKind) -> E {
Expand Down Expand Up @@ -659,6 +699,24 @@ impl<I, E> FromExternalError<I, E> for VerboseError<I> {
}
}

#[cfg(feature = "alloc")]
impl<I> ErrorConvert<VerboseError<I>> for VerboseError<(I, usize)> {
fn convert(self) -> VerboseError<I> {
VerboseError {
errors: self.errors.into_iter().map(|(i, e)| (i.0, e)).collect(),
}
}
}

#[cfg(feature = "alloc")]
impl<I> ErrorConvert<VerboseError<(I, usize)>> for VerboseError<I> {
fn convert(self) -> VerboseError<(I, usize)> {
VerboseError {
errors: self.errors.into_iter().map(|(i, e)| ((i, 0), e)).collect(),
}
}
}

#[cfg(feature = "alloc")]
impl<I: fmt::Display> fmt::Display for VerboseError<I> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Expand Down
59 changes: 0 additions & 59 deletions src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2719,65 +2719,6 @@ impl ToUsize for u64 {
}
}

/// Equivalent From implementation to avoid orphan rules in bits parsers
pub trait ErrorConvert<E> {
/// Transform to another error type
fn convert(self) -> E;
}

impl<I> ErrorConvert<(I, ErrorKind)> for ((I, usize), ErrorKind) {
fn convert(self) -> (I, ErrorKind) {
((self.0).0, self.1)
}
}

impl<I> ErrorConvert<((I, usize), ErrorKind)> for (I, ErrorKind) {
fn convert(self) -> ((I, usize), ErrorKind) {
((self.0, 0), self.1)
}
}

use crate::error;
impl<I> ErrorConvert<error::Error<I>> for error::Error<(I, usize)> {
fn convert(self) -> error::Error<I> {
error::Error {
input: self.input.0,
code: self.code,
}
}
}

impl<I> ErrorConvert<error::Error<(I, usize)>> for error::Error<I> {
fn convert(self) -> error::Error<(I, usize)> {
error::Error {
input: (self.input, 0),
code: self.code,
}
}
}

#[cfg(feature = "alloc")]
impl<I> ErrorConvert<error::VerboseError<I>> for error::VerboseError<(I, usize)> {
fn convert(self) -> error::VerboseError<I> {
error::VerboseError {
errors: self.errors.into_iter().map(|(i, e)| (i.0, e)).collect(),
}
}
}

#[cfg(feature = "alloc")]
impl<I> ErrorConvert<error::VerboseError<(I, usize)>> for error::VerboseError<I> {
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 {
Expand Down