pub fn i16<Input, Error>(endian: Endianness) -> impl Parser<Input, i16, Error>
Expand description
Recognizes a signed 2 byte integer
If the parameter is winnow::binary::Endianness::Big
, parse a big endian i16 integer,
otherwise if winnow::binary::Endianness::Little
parse a little endian i16 integer.
Complete version: returns an error if there is not enough input data
Partial version: Will return Err(winnow::error::ErrMode::Incomplete(_))
if there is not enough data.
ยงExample
use winnow::binary::i16;
fn be_i16(input: &mut &[u8]) -> ModalResult<i16> {
i16(winnow::binary::Endianness::Big).parse_next(input)
};
assert_eq!(be_i16.parse_peek(&b"\x00\x03abcefg"[..]), Ok((&b"abcefg"[..], 0x0003)));
assert!(be_i16.parse_peek(&b"\x01"[..]).is_err());
fn le_i16(input: &mut &[u8]) -> ModalResult<i16> {
i16(winnow::binary::Endianness::Little).parse_next(input)
};
assert_eq!(le_i16.parse_peek(&b"\x00\x03abcefg"[..]), Ok((&b"abcefg"[..], 0x0300)));
assert!(le_i16.parse_peek(&b"\x01"[..]).is_err());
use winnow::binary::i16;
fn be_i16(input: &mut Partial<&[u8]>) -> ModalResult<i16> {
i16(winnow::binary::Endianness::Big).parse_next(input)
};
assert_eq!(be_i16.parse_peek(Partial::new(&b"\x00\x03abcefg"[..])), Ok((Partial::new(&b"abcefg"[..]), 0x0003)));
assert_eq!(be_i16.parse_peek(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(1))));
fn le_i16(input: &mut Partial<&[u8]>) -> ModalResult<i16> {
i16(winnow::binary::Endianness::Little).parse_next(input)
};
assert_eq!(le_i16.parse_peek(Partial::new(&b"\x00\x03abcefg"[..])), Ok((Partial::new(&b"abcefg"[..]), 0x0300)));
assert_eq!(le_i16.parse_peek(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(1))));