json_syntax/parse/
object.rs

1use super::{Context, Error, Parse, Parser};
2use crate::object::Key;
3use decoded_char::DecodedChar;
4use locspan::Meta;
5
6#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
7pub enum StartFragment {
8	Empty,
9	NonEmpty(Meta<Key, usize>),
10}
11
12impl Parse for StartFragment {
13	fn parse_in<C, E>(
14		parser: &mut Parser<C, E>,
15		_context: Context,
16	) -> Result<Meta<Self, usize>, Error<E>>
17	where
18		C: Iterator<Item = Result<DecodedChar, E>>,
19	{
20		let i = parser.begin_fragment();
21		match parser.next_char()? {
22			(_, Some('{')) => {
23				parser.skip_whitespaces()?;
24
25				match parser.peek_char()? {
26					Some('}') => {
27						parser.next_char()?;
28						Ok(Meta(StartFragment::Empty, i))
29					}
30					_ => {
31						let e = parser.begin_fragment();
32						let key = Key::parse_in(parser, Context::ObjectKey)?;
33						parser.skip_whitespaces()?;
34						match parser.next_char()? {
35							(_, Some(':')) => Ok(Meta(Self::NonEmpty(Meta(key.0, e)), i)),
36							(p, unexpected) => Err(Error::unexpected(p, unexpected)),
37						}
38					}
39				}
40			}
41			(p, unexpected) => Err(Error::unexpected(p, unexpected)),
42		}
43	}
44}
45
46#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
47pub enum ContinueFragment {
48	End,
49	Entry(Meta<Key, usize>),
50}
51
52impl ContinueFragment {
53	pub fn parse_in<C, E>(parser: &mut Parser<C, E>, object: usize) -> Result<Self, Error<E>>
54	where
55		C: Iterator<Item = Result<DecodedChar, E>>,
56	{
57		parser.skip_whitespaces()?;
58		match parser.next_char()? {
59			(_, Some(',')) => {
60				parser.skip_whitespaces()?;
61				let e = parser.begin_fragment();
62				let key = Key::parse_in(parser, Context::ObjectKey)?;
63				parser.skip_whitespaces()?;
64				match parser.next_char()? {
65					(_, Some(':')) => Ok(Self::Entry(Meta(key.0, e))),
66					(p, unexpected) => Err(Error::unexpected(p, unexpected)),
67				}
68			}
69			(_, Some('}')) => {
70				parser.end_fragment(object);
71				Ok(Self::End)
72			}
73			(p, unexpected) => Err(Error::unexpected(p, unexpected)),
74		}
75	}
76}