1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
|
use core::num::NonZeroUsize;
use hifijson::token::{Lex, Token};
use hifijson::value::{self, Value};
use hifijson::{escape, ignore, num, str, Error, Expect, IterLexer, LexAlloc, SliceLexer};
fn bol<Num, Str>(b: bool) -> Value<Num, Str> {
Value::Bool(b)
}
fn num<Num, Str>(n: Num, dot: Option<usize>, exp: Option<usize>) -> Value<Num, Str> {
let dot = dot.map(|i| NonZeroUsize::new(i).unwrap());
let exp = exp.map(|i| NonZeroUsize::new(i).unwrap());
Value::Number((n, hifijson::num::Parts { dot, exp }))
}
fn int<Num, Str>(i: Num) -> Value<Num, Str> {
num(i, None, None)
}
fn arr<Num, Str, const N: usize>(v: [Value<Num, Str>; N]) -> Value<Num, Str> {
Value::Array(v.into())
}
fn obj<Num, Str, const N: usize>(v: [(Str, Value<Num, Str>); N]) -> Value<Num, Str> {
Value::Object(v.into())
}
fn iter_of_slice(slice: &[u8]) -> impl Iterator<Item = Result<u8, ()>> + '_ {
slice.iter().copied().map(Ok)
}
fn parses_to(slice: &[u8], v: Value<&str, &str>) -> Result<(), Error> {
SliceLexer::new(slice).exactly_one(ignore::parse)?;
IterLexer::new(iter_of_slice(slice)).exactly_one(ignore::parse)?;
let parsed = SliceLexer::new(slice).exactly_one(value::parse_unbounded)?;
assert_eq!(parsed, v);
let parsed = IterLexer::new(iter_of_slice(slice)).exactly_one(value::parse_unbounded)?;
assert_eq!(parsed, v);
Ok(())
}
fn parses_to_binary_string(slice: &[u8], v: &[u8]) -> Result<(), Error> {
SliceLexer::new(slice).exactly_one(ignore::parse)?;
IterLexer::new(iter_of_slice(slice)).exactly_one(ignore::parse)?;
let parsed = SliceLexer::new(slice).exactly_one(parse_binary_string)?;
assert_eq!(parsed, v);
let parsed = IterLexer::new(iter_of_slice(slice)).exactly_one(parse_binary_string)?;
assert_eq!(parsed, v);
Ok(())
}
fn parse_binary_string<L: LexAlloc>(token: Token, lexer: &mut L) -> Result<Vec<u8>, Error> {
if token != hifijson::Token::Quote {
Err(Error::Token(Expect::String))?
}
let on_string = |bytes: &mut L::Bytes, out: &mut Vec<u8>| {
out.extend_from_slice(bytes);
Ok(())
};
lexer.str_fold(Vec::new(), on_string, |lexer, escape, out| {
let c = lexer.escape_char(escape).map_err(str::Error::Escape)?;
out.extend_from_slice(c.encode_utf8(&mut [0; 4]).as_bytes());
Ok(())
})
}
fn fails_with(slice: &[u8], e: Error) {
let parsed = SliceLexer::new(slice).exactly_one(ignore::parse);
assert_eq!(parsed.unwrap_err(), e);
let parsed = IterLexer::new(iter_of_slice(slice)).exactly_one(ignore::parse);
assert_eq!(parsed.unwrap_err(), e);
parse_fails_with(slice, e)
}
fn parse_fails_with(slice: &[u8], e: Error) {
let parsed = SliceLexer::new(slice).exactly_one(value::parse_unbounded);
assert_eq!(parsed.unwrap_err(), e);
let parsed = IterLexer::new(iter_of_slice(slice)).exactly_one(value::parse_unbounded);
assert_eq!(parsed.unwrap_err(), e);
}
#[test]
fn basic() -> Result<(), Error> {
parses_to(b"null", Value::Null)?;
parses_to(b"false", Value::Bool(false))?;
parses_to(b"true", Value::Bool(true))?;
fails_with(b"nul", Expect::Value.into());
fails_with(b"fal", Expect::Value.into());
fails_with(b"t", Expect::Value.into());
fails_with(b"a", Expect::Value.into());
fails_with(b"true false", Expect::Eof.into());
Ok(())
}
#[test]
fn numbers() -> Result<(), Error> {
parses_to(b"0", num("0", None, None))?;
parses_to(b"42", num("42", None, None))?;
parses_to(b"-0", num("-0", None, None))?;
parses_to(b"-42", num("-42", None, None))?;
parses_to(b"3.14", num("3.14", Some(1), None))?;
// speed of light in m/s
parses_to(b"299e6", num("299e6", None, Some(3)))?;
// now a bit more precise
parses_to(b"299.792e6", num("299.792e6", Some(3), Some(7)))?;
fails_with(b"-", num::Error::ExpectedDigit.into());
Ok(())
}
#[test]
fn strings() -> Result<(), Error> {
// greetings to Japan
parses_to(r#""Hello 日本""#.as_bytes(), Value::String("Hello 日本"))?;
// single-character escape sequences
parses_to(
br#""\"\\\/\b\f\n\r\t""#,
Value::String("\"\\/\u{8}\u{c}\n\r\t"),
)?;
// UTF-16 surrogate pairs
parses_to(br#""\uD801\uDC37""#, Value::String("𐐷"))?;
// the smallest value representable with a surrogate pair
parses_to(br#""\ud800\udc00""#, Value::String("𐀀"))?;
// the largest value representable with a surrogate pair
parses_to(br#""\udbff\udfff""#, Value::String(""))?;
parses_to(br#""aa\nbb\ncc""#, Value::String("aa\nbb\ncc"))?;
let escape = |e| Error::Str(str::Error::Escape(e));
fails_with(br#""\x""#, escape(escape::Error::UnknownKind));
fails_with(br#""\U""#, escape(escape::Error::UnknownKind));
fails_with(br#""\"#, escape(escape::Error::Eof));
fails_with(br#""\u00"#, escape(escape::Error::Eof));
fails_with("\"\u{0}\"".as_bytes(), str::Error::Control.into());
// corresponds to ASCII code 31 in decimal notation
fails_with("\"\u{1F}\"".as_bytes(), str::Error::Control.into());
fails_with(br#""abcd"#, str::Error::Eof.into());
parse_fails_with(br#""\uDC37""#, escape(escape::Error::InvalidChar(0xdc37)));
parse_fails_with(br#""\uD801""#, escape(escape::Error::ExpectedLowSurrogate));
let s = [34, 159, 146, 150];
let err = core::str::from_utf8(&s[1..]).unwrap_err();
parse_fails_with(&s, str::Error::Utf8(err).into());
Ok(())
}
#[test]
fn arrays() -> Result<(), Error> {
parses_to(b"[]", arr([]))?;
parses_to(b"[false, true]", arr([bol(false), bol(true)]))?;
parses_to(b"[0, 1]", arr([int("0"), int("1")]))?;
parses_to(b"[[]]", arr([arr([])]))?;
fails_with(b"[", Expect::ValueOrEnd.into());
fails_with(b"[1", Expect::CommaOrEnd.into());
fails_with(b"[1 2", Expect::CommaOrEnd.into());
fails_with(b"[1,", Expect::Value.into());
Ok(())
}
#[test]
fn objects() -> Result<(), Error> {
parses_to(b"{}", obj([]))?;
parses_to(br#"{"a": 0}"#, obj([("a", int("0"))]))?;
parses_to(
br#"{"a": 0, "b": 1}"#,
obj([("a", int("0")), ("b", int("1"))]),
)?;
fails_with(b"{", Expect::ValueOrEnd.into());
fails_with(b"{0", Expect::String.into());
fails_with(br#"{"a" 1"#, Expect::Colon.into());
fails_with(br#"{"a": 1"#, Expect::CommaOrEnd.into());
fails_with(br#"{"a": 1,"#, Expect::Value.into());
Ok(())
}
#[test]
fn binary_strings() -> Result<(), Error> {
parses_to_binary_string(br#""aaa\nbbb\nccc""#, b"aaa\nbbb\nccc")?;
parses_to_binary_string(b"\"aaa\xffbbb\xffccc\"", b"aaa\xffbbb\xffccc")?;
parses_to_binary_string(
b"\"aaa\\u2200\xe2\x88\x80ccc\"",
"aaa\u{2200}\u{2200}ccc".as_bytes(),
)?;
Ok(())
}
|