json-syntax 0.13.1

Strict JSON parsing and mapping library
Documentation
use super::{JsonParse, JsonParseError, JsonParsingContext, Parser};
use decoded_char::DecodedChar;
use locspan::Span;
use smallstr::SmallString;

fn is_control(c: char) -> bool {
	('\u{0000}'..='\u{001f}').contains(&c)
}

fn parse_hex4<C, E>(parser: &mut Parser<C, E>) -> Result<u32, JsonParseError<E>>
where
	C: Iterator<Item = Result<DecodedChar, E>>,
{
	match parser.next_char()? {
		(p, Some(c)) => match c.to_digit(16) {
			Some(h3) => match parser.next_char()? {
				(p, Some(c)) => match c.to_digit(16) {
					Some(h2) => match parser.next_char()? {
						(p, Some(c)) => match c.to_digit(16) {
							Some(h1) => match parser.next_char()? {
								(p, Some(c)) => match c.to_digit(16) {
									Some(h0) => Ok(h3 << 12 | h2 << 8 | h1 << 4 | h0),
									None => Err(JsonParseError::unexpected(p, Some(c))),
								},
								(p, unexpected) => Err(JsonParseError::unexpected(p, unexpected)),
							},
							None => Err(JsonParseError::unexpected(p, Some(c))),
						},
						(p, unexpected) => Err(JsonParseError::unexpected(p, unexpected)),
					},
					None => Err(JsonParseError::unexpected(p, Some(c))),
				},
				(p, unexpected) => Err(JsonParseError::unexpected(p, unexpected)),
			},
			None => Err(JsonParseError::unexpected(p, Some(c))),
		},
		(p, unexpected) => Err(JsonParseError::unexpected(p, unexpected)),
	}
}

impl<A: smallvec::Array<Item = u8>> JsonParse for SmallString<A> {
	fn parse_in<C, E>(
		parser: &mut Parser<C, E>,
		_context: JsonParsingContext,
	) -> Result<(Self, usize), JsonParseError<E>>
	where
		C: Iterator<Item = Result<DecodedChar, E>>,
	{
		let i = parser.begin_fragment();
		match parser.next_char()? {
			(_, Some('"')) => {
				let mut result = Self::new();
				let mut high_surrogate: Option<(usize, u32)> = None;
				loop {
					let c = match parser.next_char()? {
						(p, Some('"')) => {
							if let Some((p_high, high)) = high_surrogate {
								if parser.options.accept_truncated_surrogate_pair {
									result.push('\u{fffd}');
								} else {
									break Err(JsonParseError::MissingLowSurrogate(
										Span::new(p_high, p),
										high as u16,
									));
								}
							}

							parser.end_fragment(i);
							break Ok((result, i));
						}
						(_, Some('\\')) => {
							match parser.next_char()? {
								(_, Some(c @ ('"' | '\\' | '/'))) => c,
								(_, Some('b')) => '\u{0008}',
								(_, Some('t')) => '\u{0009}',
								(_, Some('n')) => '\u{000a}',
								(_, Some('f')) => '\u{000c}',
								(_, Some('r')) => '\u{000d}',
								(p, Some('u')) => {
									let codepoint = parse_hex4(parser)?;

									match high_surrogate.take() {
										Some((p_high, high)) => {
											if (0xdc00..=0xdfff).contains(&codepoint) {
												let low = codepoint;
												let codepoint = ((high - 0xd800) << 10
													| (low - 0xdc00)) + 0x010000;
												match char::from_u32(codepoint) {
													Some(c) => c,
													None => {
														if parser.options.accept_invalid_codepoints
														{
															'\u{fffd}'
														} else {
															break Err(JsonParseError::InvalidUnicodeCodePoint(
															Span::new(p_high, parser.position),
															codepoint,
														));
														}
													}
												}
											} else if parser.options.accept_truncated_surrogate_pair
											{
												result.push('\u{fffd}');

												match char::from_u32(codepoint) {
													Some(c) => c,
													None => {
														if parser.options.accept_invalid_codepoints
														{
															'\u{fffd}'
														} else {
															break Err(JsonParseError::InvalidUnicodeCodePoint(
															Span::new(p, parser.position),
															codepoint,
														));
														}
													}
												}
											} else {
												break Err(JsonParseError::InvalidLowSurrogate(
													Span::new(p, parser.position),
													high as u16,
													codepoint,
												));
											}
										}
										None => {
											if (0xd800..=0xdbff).contains(&codepoint) {
												high_surrogate = Some((p, codepoint));
												continue;
											} else {
												match char::from_u32(codepoint) {
													Some(c) => c,
													None => {
														if parser.options.accept_invalid_codepoints
														{
															'\u{fffd}'
														} else {
															break Err(JsonParseError::InvalidUnicodeCodePoint(
															Span::new(p, parser.position),
															codepoint,
														));
														}
													}
												}
											}
										}
									}
								}
								(p, unexpected) => {
									break Err(JsonParseError::unexpected(p, unexpected));
								}
							}
						}
						(_, Some(c)) if !is_control(c) => c,
						(p, unexpected) => break Err(JsonParseError::unexpected(p, unexpected)),
					};

					if let Some((p_high, high)) = high_surrogate.take() {
						if parser.options.accept_truncated_surrogate_pair {
							result.push('\u{fffd}');
						} else {
							break Err(JsonParseError::MissingLowSurrogate(
								Span::new(p_high, parser.position),
								high as u16,
							));
						}
					}

					result.push(c);
				}
			}
			(p, unexpected) => Err(JsonParseError::unexpected(p, unexpected)),
		}
	}
}