Skip to content

Commit

Permalink
fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Geal committed Jun 17, 2019
1 parent 55034c0 commit 5ce22d8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
15 changes: 10 additions & 5 deletions src/bits/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ mod macros;
pub mod streaming;
pub mod complete;

use crate::error::ParseError;
use crate::internal::{Err, IResult};
use crate::error::{ParseError, ErrorKind};
use crate::internal::{Err, IResult, Needed};
use crate::lib::std::ops::RangeFrom;
use crate::traits::{Slice, ErrorConvert};

Expand Down Expand Up @@ -86,7 +86,7 @@ where
/// ```
pub fn bytes<I, O, E1: ParseError<I>+ErrorConvert<E2>, E2: ParseError<(I, usize)>, P>(parser: P) -> impl Fn((I, usize)) -> IResult<(I, usize), O, E2>
where
I: Slice<RangeFrom<usize>>,
I: Slice<RangeFrom<usize>> + Clone,
P: Fn(I) -> IResult<I, O, E1>,
{
move |(input, offset): (I, usize)| {
Expand All @@ -95,9 +95,14 @@ where
} else {
input.slice((offset / 8)..)
};
let i = (input.clone(), offset);
match parser(inner) {
Ok((rest, res)) => Ok(((rest, 0), res)),
Err(Err::Incomplete(n)) => Err(Err::Incomplete(n.map(|u| u * 8))),
Err(Err::Incomplete(Needed::Unknown)) => Err(Err::Incomplete(Needed::Unknown)),
Err(Err::Incomplete(Needed::Size(sz))) => Err(match sz.checked_mul(8) {
Some(v) => Err::Incomplete(Needed::Size(v)),
None => Err::Failure(E2::from_error_kind(i, ErrorKind::TooLarge)),
}),
Err(Err::Error(e)) => Err(Err::Error(e.convert())),
Err(Err::Failure(e)) => Err(Err::Failure(e.convert())),
}
Expand All @@ -107,7 +112,7 @@ where
#[doc(hidden)]
pub fn bytesc<I, O, E1: ParseError<I>+ErrorConvert<E2>, E2: ParseError<(I, usize)>, P>(input: (I, usize), parser: P) -> IResult<(I, usize), O, E2>
where
I: Slice<RangeFrom<usize>>,
I: Slice<RangeFrom<usize>> + Clone,
P: Fn(I) -> IResult<I, O, E1>,
{
bytes(parser)(input)
Expand Down
3 changes: 2 additions & 1 deletion tests/issues.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,8 @@ named!(issue_775, take_till1!(|_| true));

#[test]
fn issue_848_overflow_incomplete_bits_to_bytes() {
named!(parser<&[u8], &[u8]>, bits!(bytes!(take!(0x2000000000000000))));
named!(take, take!(0x2000000000000000));
named!(parser<&[u8], &[u8]>, bits!(bytes!(take)));
assert_eq!(parser(&b""[..]), Err(Err::Failure(error_position!(&b""[..], ErrorKind::TooLarge))));
}

Expand Down

0 comments on commit 5ce22d8

Please sign in to comment.