use super::parse_error::fail; use super::parse_number::number; use super::parse_string::string; use super::Value; use crate::fault::Fault; /// Containers nested deeper than this are rejected (keeps recursion bounded). pub const MAX_DEPTH: usize = 128; fn skip_ws(bytes: &[u8], from: usize) -> usize { from + bytes[from..].iter().take_while(|b| matches!(**b, b' ' | b'\t' | b'\n' | b'\r')).count() } fn literal(bytes: &[u8], pos: usize, word: &[u8], value: Value) -> Result<(Value, usize), Fault> { if bytes[pos..].starts_with(word) { Ok((value, pos + word.len())) } else { Err(fail(pos, "invalid literal")) } } fn array(bytes: &[u8], start: usize, depth: usize) -> Result<(Value, usize), Fault> { let mut items = Vec::new(); let mut pos = skip_ws(bytes, start + 1); if bytes.get(pos) == Some(&b']') { return Ok((Value::Array(items), pos + 1)); } loop { let (item, after) = value(bytes, pos, depth + 1)?; items.push(item); pos = skip_ws(bytes, after); match bytes.get(pos) { Some(b',') => pos = skip_ws(bytes, pos + 1), Some(b']') => return Ok((Value::Array(items), pos + 1)), _ => return Err(fail(pos, "expected ',' or ']'")), } } } fn member(bytes: &[u8], pos: usize, depth: usize) -> Result<((String, Value), usize), Fault> { if bytes.get(pos) != Some(&b'"') { return Err(fail(pos, "expected a string key")); } let (key, after_key) = string(bytes, pos)?; let colon = skip_ws(bytes, after_key); if bytes.get(colon) != Some(&b':') { return Err(fail(colon, "expected ':'")); } let (val, after) = value(bytes, skip_ws(bytes, colon + 1), depth + 1)?; Ok(((key, val), after)) } fn object(bytes: &[u8], start: usize, depth: usize) -> Result<(Value, usize), Fault> { let mut members = Vec::new(); let mut pos = skip_ws(bytes, start + 1); if bytes.get(pos) == Some(&b'}') { return Ok((Value::Object(members), pos + 1)); } loop { let (entry, after) = member(bytes, pos, depth)?; members.push(entry); pos = skip_ws(bytes, after); match bytes.get(pos) { Some(b',') => pos = skip_ws(bytes, pos + 1), Some(b'}') => return Ok((Value::Object(members), pos + 1)), _ => return Err(fail(pos, "expected ',' or '}'")), } } } fn value(bytes: &[u8], pos: usize, depth: usize) -> Result<(Value, usize), Fault> { if depth > MAX_DEPTH { return Err(fail(pos, "nesting too deep")); } match bytes.get(pos) { None => Err(fail(pos, "unexpected end of input")), Some(b'{') => object(bytes, pos, depth), Some(b'[') => array(bytes, pos, depth), Some(b'"') => string(bytes, pos).map(|(s, end)| (Value::String(s), end)), Some(b't') => literal(bytes, pos, b"true", Value::Bool(true)), Some(b'f') => literal(bytes, pos, b"false", Value::Bool(false)), Some(b'n') => literal(bytes, pos, b"null", Value::Null), Some(b'-' | b'0'..=b'9') => number(bytes, pos).map(|(n, end)| (Value::Number(n), end)), Some(_) => Err(fail(pos, "unexpected character")), } } /// Strict RFC 8259 parse of one complete document. pub fn parse(text: &str) -> Result { let bytes = text.as_bytes(); let (parsed, after) = value(bytes, skip_ws(bytes, 0), 0)?; let end = skip_ws(bytes, after); if end < bytes.len() { Err(fail(end, "trailing characters")) } else { Ok(parsed) } }