bluejay_parser/ast/
parse.rs

1use crate::ast::{depth_limiter::DEFAULT_MAX_DEPTH, DepthLimiter, FromTokens, LexerTokens, Tokens};
2use crate::lexer::LogosLexer;
3use crate::Error;
4
5pub struct ParseOptions {
6    pub graphql_ruby_compatibility: bool,
7    pub max_depth: usize,
8}
9
10impl Default for ParseOptions {
11    fn default() -> Self {
12        Self {
13            graphql_ruby_compatibility: false,
14            max_depth: DEFAULT_MAX_DEPTH,
15        }
16    }
17}
18
19pub trait Parse<'a>: Sized {
20    #[inline]
21    fn parse(s: &'a str) -> Result<Self, Vec<Error>> {
22        Self::parse_with_options(s, Default::default())
23    }
24
25    #[inline]
26    fn parse_with_options(s: &'a str, options: ParseOptions) -> Result<Self, Vec<Error>> {
27        let lexer =
28            LogosLexer::new(s).with_graphql_ruby_compatibility(options.graphql_ruby_compatibility);
29        let tokens = LexerTokens::new(lexer);
30
31        Self::parse_from_tokens(tokens, options.max_depth)
32    }
33
34    fn parse_from_tokens(tokens: impl Tokens<'a>, max_depth: usize) -> Result<Self, Vec<Error>>;
35}
36
37impl<'a, T: FromTokens<'a>> Parse<'a> for T {
38    #[inline]
39    fn parse_from_tokens(
40        mut tokens: impl Tokens<'a>,
41        max_depth: usize,
42    ) -> Result<Self, Vec<Error>> {
43        let result = T::from_tokens(&mut tokens, DepthLimiter::new(max_depth));
44
45        let errors = tokens.into_errors();
46
47        if errors.is_empty() {
48            result.map_err(|err| vec![err.into()])
49        } else {
50            Err(errors.into_iter().map(Into::into).collect())
51        }
52    }
53}