1pub mod complete;
5pub mod streaming;
6
7use crate::error::{ErrorKind, ParseError};
8use crate::internal::{Err, IResult, Needed, Parser};
9use crate::lib::std::ops::RangeFrom;
10use crate::traits::{ErrorConvert, Slice};
11
12pub fn bits<I, O, E1, E2, P>(mut parser: P) -> impl FnMut(I) -> IResult<I, O, E2>
41where
42 E1: ParseError<(I, usize)> + ErrorConvert<E2>,
43 E2: ParseError<I>,
44 I: Slice<RangeFrom<usize>>,
45 P: Parser<(I, usize), O, E1>,
46{
47 move |input: I| match parser.parse((input, 0)) {
48 Ok(((rest, offset), result)) => {
49 let remaining_bytes_index = offset / 8 + if offset % 8 == 0 { 0 } else { 1 };
53 Ok((rest.slice(remaining_bytes_index..), result))
54 }
55 Err(Err::Incomplete(n)) => Err(Err::Incomplete(n.map(|u| u.get() / 8 + 1))),
56 Err(Err::Error(e)) => Err(Err::Error(e.convert())),
57 Err(Err::Failure(e)) => Err(Err::Failure(e.convert())),
58 }
59}
60
61pub fn bytes<I, O, E1, E2, P>(mut parser: P) -> impl FnMut((I, usize)) -> IResult<(I, usize), O, E2>
87where
88 E1: ParseError<I> + ErrorConvert<E2>,
89 E2: ParseError<(I, usize)>,
90 I: Slice<RangeFrom<usize>> + Clone,
91 P: Parser<I, O, E1>,
92{
93 move |(input, offset): (I, usize)| {
94 let inner = if offset % 8 != 0 {
95 input.slice((1 + offset / 8)..)
96 } else {
97 input.slice((offset / 8)..)
98 };
99 let i = (input, offset);
100 match parser.parse(inner) {
101 Ok((rest, res)) => Ok(((rest, 0), res)),
102 Err(Err::Incomplete(Needed::Unknown)) => Err(Err::Incomplete(Needed::Unknown)),
103 Err(Err::Incomplete(Needed::Size(sz))) => Err(match sz.get().checked_mul(8) {
104 Some(v) => Err::Incomplete(Needed::new(v)),
105 None => Err::Failure(E2::from_error_kind(i, ErrorKind::TooLarge)),
106 }),
107 Err(Err::Error(e)) => Err(Err::Error(e.convert())),
108 Err(Err::Failure(e)) => Err(Err::Failure(e.convert())),
109 }
110 }
111}
112
113#[cfg(test)]
114mod test {
115 use super::*;
116 use crate::bits::streaming::take;
117 use crate::error::Error;
118 use crate::sequence::tuple;
119
120 #[test]
121 fn test_complete_byte_consumption_bits() {
124 let input = &[0x12, 0x34, 0x56, 0x78];
125
126 let result: IResult<&[u8], (u8, u8, u8)> =
128 bits::<_, _, Error<(&[u8], usize)>, _, _>(tuple((take(4usize), take(8usize), take(4usize))))(
129 input,
130 );
131
132 let output = result.expect("We take 2 bytes and the input is longer than 2 bytes");
133
134 let remaining = output.0;
135 assert_eq!(remaining, [0x56, 0x78]);
136
137 let parsed = output.1;
138 assert_eq!(parsed.0, 0x01);
139 assert_eq!(parsed.1, 0x23);
140 assert_eq!(parsed.2, 0x04);
141 }
142
143 #[test]
144 fn test_partial_byte_consumption_bits() {
149 let input = &[0x12, 0x34, 0x56, 0x78];
150
151 let result: IResult<&[u8], (u8, u8)> =
153 bits::<_, _, Error<(&[u8], usize)>, _, _>(tuple((take(4usize), take(8usize))))(input);
154
155 let output = result.expect("We take 1.5 bytes and the input is longer than 2 bytes");
156
157 let remaining = output.0;
158 assert_eq!(remaining, [0x56, 0x78]);
159
160 let parsed = output.1;
161 assert_eq!(parsed.0, 0x01);
162 assert_eq!(parsed.1, 0x23);
163 }
164
165 #[test]
166 #[cfg(feature = "std")]
167 fn test_incomplete_bits() {
169 let input = &[0x12];
170
171 let result: IResult<&[u8], (u8, u8)> =
173 bits::<_, _, Error<(&[u8], usize)>, _, _>(tuple((take(4usize), take(8usize))))(input);
174
175 assert!(result.is_err());
176 let error = result.err().unwrap();
177 assert_eq!("Parsing requires 2 bytes/chars", error.to_string());
178 }
179}