json_syntax/parse/
array.rs1use super::{Context, Error, Parse, Parser};
2use decoded_char::DecodedChar;
3use locspan::Meta;
4use locspan_derive::*;
5
6#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
7pub enum StartFragment {
8 Empty,
9 NonEmpty,
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 parser.end_fragment(i);
29 Ok(Meta(StartFragment::Empty, i))
30 }
31 _ => {
32 Ok(Meta(StartFragment::NonEmpty, i))
34 }
35 }
36 }
37 (p, unexpected) => Err(Error::unexpected(p, unexpected)),
38 }
39 }
40}
41
42#[derive(
43 Clone,
44 Copy,
45 PartialEq,
46 Eq,
47 PartialOrd,
48 Ord,
49 Hash,
50 Debug,
51 StrippedPartialEq,
52 StrippedEq,
53 StrippedPartialOrd,
54 StrippedOrd,
55 StrippedHash,
56)]
57pub enum ContinueFragment {
58 Item,
59 End,
60}
61
62impl ContinueFragment {
63 pub fn parse_in<C, E>(parser: &mut Parser<C, E>, array: usize) -> Result<Self, Error<E>>
64 where
65 C: Iterator<Item = Result<DecodedChar, E>>,
66 {
67 parser.skip_whitespaces()?;
68 match parser.next_char()? {
69 (_, Some(',')) => Ok(Self::Item),
70 (_, Some(']')) => {
71 parser.end_fragment(array);
72 Ok(Self::End)
73 }
74 (p, unexpected) => Err(Error::unexpected(p, unexpected)),
75 }
76 }
77}