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(input)!: Consolidate Input traits #107

Merged
merged 20 commits into from
Feb 1, 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
2 changes: 1 addition & 1 deletion examples/custom_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use winnow::error::ParseError;
use winnow::Err::Error;
use winnow::IResult;

#[derive(Debug, PartialEq)]
#[derive(Debug, PartialEq, Eq)]
pub enum CustomError<I> {
MyError,
Nom(I, ErrorKind),
Expand Down
21 changes: 11 additions & 10 deletions src/bits/complete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
#![allow(deprecated)]

use crate::error::{ErrorKind, ParseError};
use crate::input::{InputIter, Slice, SliceLen, ToUsize};
use crate::lib::std::ops::{AddAssign, Div, RangeFrom, Shl, Shr};
use crate::input::{AsBytes, Input, ToUsize};
use crate::lib::std::ops::{AddAssign, Div, Shl, Shr};
use crate::{Err, IResult};

/// Generates a parser taking `count` bits
Expand Down Expand Up @@ -39,7 +39,7 @@ pub fn take<I, O, C, E: ParseError<(I, usize)>>(
count: C,
) -> impl Fn((I, usize)) -> IResult<(I, usize), O, E>
where
I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + SliceLen,
I: Input<Token = u8> + AsBytes,
C: ToUsize,
O: From<u8> + AddAssign + Shl<usize, Output = O> + Shr<usize, Output = O>,
{
Expand All @@ -52,14 +52,14 @@ pub(crate) fn take_internal<I, O, E: ParseError<(I, usize)>>(
count: usize,
) -> IResult<(I, usize), O, E>
where
I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + SliceLen,
I: Input<Token = u8> + AsBytes,
O: From<u8> + AddAssign + Shl<usize, Output = O> + Shr<usize, Output = O>,
{
if count == 0 {
Ok(((input, bit_offset), 0u8.into()))
} else {
let cnt = (count + bit_offset).div(8);
if input.slice_len() * 8 < count + bit_offset {
if input.input_len() * 8 < count + bit_offset {
Err(Err::Error(E::from_error_kind(
(input, bit_offset),
ErrorKind::Eof,
Expand All @@ -70,7 +70,7 @@ where
let mut remaining: usize = count;
let mut end_offset: usize = 0;

for byte in input.iter_elements().take(cnt + 1) {
for byte in input.as_bytes().iter().copied().take(cnt + 1) {
if remaining == 0 {
break;
}
Expand All @@ -90,7 +90,8 @@ where
offset = 0;
}
}
Ok(((input.slice(cnt..), end_offset), acc))
let (input, _) = input.next_slice(cnt);
Ok(((input, end_offset), acc))
}
}
}
Expand All @@ -104,7 +105,7 @@ pub fn tag<I, O, C, E: ParseError<(I, usize)>>(
count: C,
) -> impl Fn((I, usize)) -> IResult<(I, usize), O, E>
where
I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + SliceLen + Clone,
I: Input<Token = u8> + AsBytes,
C: ToUsize,
O: From<u8> + AddAssign + Shl<usize, Output = O> + Shr<usize, Output = O> + PartialEq,
{
Expand All @@ -118,7 +119,7 @@ pub(crate) fn tag_internal<I, O, E: ParseError<(I, usize)>>(
count: usize,
) -> IResult<(I, usize), O, E>
where
I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + SliceLen + Clone,
I: Input<Token = u8> + AsBytes,
O: From<u8> + AddAssign + Shl<usize, Output = O> + Shr<usize, Output = O> + PartialEq,
{
let inp = input.clone();
Expand Down Expand Up @@ -151,7 +152,7 @@ where
#[deprecated(since = "8.0.0", note = "Replaced with `winnow::bits::bool`")]
pub fn bool<I, E: ParseError<(I, usize)>>(input: (I, usize)) -> IResult<(I, usize), bool, E>
where
I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + SliceLen,
I: Input<Token = u8> + AsBytes,
{
let (res, bit): (_, u32) = take(1usize)(input)?;
Ok((res, bit != 0))
Expand Down
24 changes: 12 additions & 12 deletions src/bits/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ pub mod streaming;
mod tests;

use crate::error::{ErrorConvert, ErrorKind, ParseError};
use crate::input::{InputIsStreaming, InputIter, Slice, SliceLen, ToUsize};
use crate::lib::std::ops::{AddAssign, RangeFrom, Shl, Shr};
use crate::input::{AsBytes, Input, InputIsStreaming, ToUsize};
use crate::lib::std::ops::{AddAssign, Shl, Shr};
use crate::{Err, IResult, Needed, Parser};

/// Converts a byte-level input to a bit-level input, for consumption by a parser that uses bits.
Expand Down Expand Up @@ -42,7 +42,7 @@ pub fn bits<I, O, E1, E2, P>(mut parser: P) -> impl FnMut(I) -> IResult<I, O, E2
where
E1: ParseError<(I, usize)> + ErrorConvert<E2>,
E2: ParseError<I>,
I: Slice<RangeFrom<usize>>,
I: Input,
P: Parser<(I, usize), O, E1>,
{
move |input: I| match parser.parse_next((input, 0)) {
Expand All @@ -51,7 +51,8 @@ where
// The parser functions might already slice away all fully read bytes.
// That's why `offset / 8` isn't necessarily needed at all times.
let remaining_bytes_index = offset / 8 + if offset % 8 == 0 { 0 } else { 1 };
Ok((rest.slice(remaining_bytes_index..), result))
let (input, _) = rest.next_slice(remaining_bytes_index);
Ok((input, result))
}
Err(Err::Incomplete(n)) => Err(Err::Incomplete(n.map(|u| u.get() / 8 + 1))),
Err(Err::Error(e)) => Err(Err::Error(e.convert())),
Expand Down Expand Up @@ -87,14 +88,14 @@ pub fn bytes<I, O, E1, E2, P>(mut parser: P) -> impl FnMut((I, usize)) -> IResul
where
E1: ParseError<I> + ErrorConvert<E2>,
E2: ParseError<(I, usize)>,
I: Slice<RangeFrom<usize>> + Clone,
I: Input,
P: Parser<I, O, E1>,
{
move |(input, offset): (I, usize)| {
let inner = if offset % 8 != 0 {
input.slice((1 + offset / 8)..)
let (inner, _) = if offset % 8 != 0 {
input.next_slice(1 + offset / 8)
} else {
input.slice((offset / 8)..)
input.next_slice(offset / 8)
};
let i = (input, offset);
match parser.parse_next(inner) {
Expand Down Expand Up @@ -139,7 +140,7 @@ pub fn take<I, O, C, E: ParseError<(I, usize)>, const STREAMING: bool>(
count: C,
) -> impl Fn((I, usize)) -> IResult<(I, usize), O, E>
where
I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + SliceLen + InputIsStreaming<STREAMING>,
I: Input<Token = u8> + AsBytes + InputIsStreaming<STREAMING>,
C: ToUsize,
O: From<u8> + AddAssign + Shl<usize, Output = O> + Shr<usize, Output = O>,
{
Expand Down Expand Up @@ -203,8 +204,7 @@ pub fn tag<I, O, C, E: ParseError<(I, usize)>, const STREAMING: bool>(
count: C,
) -> impl Fn((I, usize)) -> IResult<(I, usize), O, E>
where
I:
Slice<RangeFrom<usize>> + InputIter<Item = u8> + SliceLen + InputIsStreaming<STREAMING> + Clone,
I: Input<Token = u8> + AsBytes + InputIsStreaming<STREAMING>,
C: ToUsize,
O: From<u8> + AddAssign + Shl<usize, Output = O> + Shr<usize, Output = O> + PartialEq,
{
Expand Down Expand Up @@ -238,7 +238,7 @@ pub fn bool<I, E: ParseError<(I, usize)>, const STREAMING: bool>(
input: (I, usize),
) -> IResult<(I, usize), bool, E>
where
I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + SliceLen + InputIsStreaming<STREAMING>,
I: Input<Token = u8> + AsBytes + InputIsStreaming<STREAMING>,
{
#![allow(deprecated)]
if STREAMING {
Expand Down
21 changes: 11 additions & 10 deletions src/bits/streaming.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
#![allow(deprecated)]

use crate::error::{ErrorKind, ParseError};
use crate::input::{InputIter, Slice, SliceLen, ToUsize};
use crate::lib::std::ops::{AddAssign, Div, RangeFrom, Shl, Shr};
use crate::input::{AsBytes, Input, ToUsize};
use crate::lib::std::ops::{AddAssign, Div, Shl, Shr};
use crate::{Err, IResult, Needed};

/// Generates a parser taking `count` bits
Expand All @@ -19,7 +19,7 @@ pub fn take<I, O, C, E: ParseError<(I, usize)>>(
count: C,
) -> impl Fn((I, usize)) -> IResult<(I, usize), O, E>
where
I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + SliceLen,
I: Input<Token = u8> + AsBytes,
C: ToUsize,
O: From<u8> + AddAssign + Shl<usize, Output = O> + Shr<usize, Output = O>,
{
Expand All @@ -32,22 +32,22 @@ pub(crate) fn take_internal<I, O, E: ParseError<(I, usize)>>(
count: usize,
) -> IResult<(I, usize), O, E>
where
I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + SliceLen,
I: Input<Token = u8> + AsBytes,
O: From<u8> + AddAssign + Shl<usize, Output = O> + Shr<usize, Output = O>,
{
if count == 0 {
Ok(((input, bit_offset), 0u8.into()))
} else {
let cnt = (count + bit_offset).div(8);
if input.slice_len() * 8 < count + bit_offset {
if input.input_len() * 8 < count + bit_offset {
Err(Err::Incomplete(Needed::new(count)))
} else {
let mut acc: O = 0_u8.into();
let mut offset: usize = bit_offset;
let mut remaining: usize = count;
let mut end_offset: usize = 0;

for byte in input.iter_elements().take(cnt + 1) {
for byte in input.as_bytes().iter().copied().take(cnt + 1) {
if remaining == 0 {
break;
}
Expand All @@ -67,7 +67,8 @@ where
offset = 0;
}
}
Ok(((input.slice(cnt..), end_offset), acc))
let (input, _) = input.next_slice(cnt);
Ok(((input, end_offset), acc))
}
}
}
Expand All @@ -84,7 +85,7 @@ pub fn tag<I, O, C, E: ParseError<(I, usize)>>(
count: C,
) -> impl Fn((I, usize)) -> IResult<(I, usize), O, E>
where
I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + SliceLen + Clone,
I: Input<Token = u8> + AsBytes,
C: ToUsize,
O: From<u8> + AddAssign + Shl<usize, Output = O> + Shr<usize, Output = O> + PartialEq,
{
Expand All @@ -98,7 +99,7 @@ pub(crate) fn tag_internal<I, O, E: ParseError<(I, usize)>>(
count: usize,
) -> IResult<(I, usize), O, E>
where
I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + SliceLen + Clone,
I: Input<Token = u8> + AsBytes,
O: From<u8> + AddAssign + Shl<usize, Output = O> + Shr<usize, Output = O> + PartialEq,
{
let inp = input.clone();
Expand Down Expand Up @@ -131,7 +132,7 @@ where
#[deprecated(since = "8.0.0", note = "Replaced with `winnow::bits::bool`")]
pub fn bool<I, E: ParseError<(I, usize)>>(input: (I, usize)) -> IResult<(I, usize), bool, E>
where
I: Slice<RangeFrom<usize>> + InputIter<Item = u8> + SliceLen,
I: Input<Token = u8> + AsBytes,
{
let (res, bit): (_, u32) = take(1usize)(input)?;
Ok((res, bit != 0))
Expand Down
Loading