sark-http 0.40.0

HTTP wire types, semantics, codecs, and ownership invariants for Sark
Documentation
use sark_http::field::text::{Ascii, IntError};

#[test]
fn usize_parser_bounds() {
    let max = usize::MAX.to_string();
    let overflow = format!("{max}0");

    assert!(Ascii::new(b"000000000000000000000").parse_usize() == Ok(0));
    assert!(Ascii::new(max.as_bytes()).parse_usize() == Ok(usize::MAX));
    assert!(Ascii::new(overflow.as_bytes()).parse_usize() == Err(IntError::Overflow));
    assert!(Ascii::new(b"").parse_usize() == Err(IntError::Empty));
    assert!(Ascii::new(b"+1").parse_usize() == Err(IntError::InvalidByte));
    assert!(Ascii::new(b"42x").parse_prefix() == Ok((42, 2)));
    assert!(Ascii::new(b"x42").parse_prefix() == Err(IntError::InvalidByte));
}

#[test]
fn u64_parser_bounds() {
    let max = u64::MAX.to_string();
    let overflow = format!("{max}0x");

    assert!(Ascii::new(b"9999999999999999999").parse_u64() == Ok(9_999_999_999_999_999_999));
    assert!(Ascii::new(max.as_bytes()).parse_u64() == Ok(u64::MAX));
    assert!(Ascii::new(overflow.as_bytes()).parse_u64() == Err(IntError::Overflow));
    assert!(Ascii::new(b"42\n").parse_u64() == Err(IntError::InvalidByte));
}

#[test]
fn narrow_unsigned_bounds() {
    assert_eq!(Ascii::new(b"255").parse_u8(), Ok(u8::MAX));
    assert_eq!(Ascii::new(b"255x").parse_u8(), Err(IntError::InvalidByte));
    assert_eq!(Ascii::new(b"256x").parse_u8(), Err(IntError::Overflow));
    assert_eq!(Ascii::new(b"65535").parse_u16(), Ok(u16::MAX));
    assert_eq!(
        Ascii::new(b"65535x").parse_u16(),
        Err(IntError::InvalidByte)
    );
    assert_eq!(Ascii::new(b"65536x").parse_u16(), Err(IntError::Overflow));
}

#[test]
fn fragmented_decimal_matches_the_first_wire_error() {
    for wire in [
        b"".as_slice(),
        b"0000000000000000000018446744073709551615",
        b"18446744073709551616x",
        b"1x8446744073709551616",
    ] {
        let expected = Ascii::new(wire).parse_u64();
        for split in 0..=wire.len() {
            let (first, rest) = wire.split_at(split);
            let value = Ascii::new(first).extend_u64(Err(IntError::Empty));
            let value = Ascii::new(b"").extend_u64(value);
            assert_eq!(Ascii::new(rest).extend_u64(value), expected);
        }
    }
}