bluejay_parser/ast/definition/
explicit_schema_definition.rs1use crate::ast::{
2 definition::{Context, Directives},
3 ConstDirectives, FromTokens, ParseError, Tokens, TryFromTokens,
4};
5use crate::lexical_token::{Name, PunctuatorType, StringValue};
6use crate::Span;
7use bluejay_core::OperationType;
8use std::str::FromStr;
9
10#[derive(Debug)]
11pub struct ExplicitSchemaDefinition<'a, C: Context> {
12 description: Option<StringValue<'a>>,
13 schema_identifier_span: Span,
14 directives: Option<Directives<'a, C>>,
15 root_operation_type_definitions: Vec<RootOperationTypeDefinition<'a>>,
16 root_operation_type_definitions_span: Span,
17}
18
19impl<'a, C: Context> ExplicitSchemaDefinition<'a, C> {
20 pub(crate) const SCHEMA_IDENTIFIER: &'static str = "schema";
21
22 pub(crate) fn description(&self) -> Option<&StringValue> {
23 self.description.as_ref()
24 }
25
26 pub(crate) fn root_operation_type_definitions(&self) -> &[RootOperationTypeDefinition<'a>] {
27 &self.root_operation_type_definitions
28 }
29
30 pub(crate) fn directives(&self) -> Option<&Directives<'a, C>> {
31 self.directives.as_ref()
32 }
33
34 pub(crate) fn schema_identifier_span(&self) -> &Span {
35 &self.schema_identifier_span
36 }
37
38 pub(crate) fn root_operation_type_definitions_span(&self) -> &Span {
39 &self.root_operation_type_definitions_span
40 }
41}
42
43impl<'a, C: Context> FromTokens<'a> for ExplicitSchemaDefinition<'a, C> {
44 fn from_tokens(tokens: &mut impl Tokens<'a>) -> Result<Self, ParseError> {
45 let description = tokens.next_if_string_value();
46
47 let schema_identifier_span = tokens.expect_name_value(Self::SCHEMA_IDENTIFIER)?;
48
49 let directives = ConstDirectives::try_from_tokens(tokens).transpose()?;
50
51 let mut root_operation_type_definitions = Vec::new();
52
53 let open_span = tokens.expect_punctuator(PunctuatorType::OpenBrace)?;
54
55 let close_span = loop {
56 root_operation_type_definitions.push(RootOperationTypeDefinition::from_tokens(tokens)?);
57 if let Some(close_span) = tokens.next_if_punctuator(PunctuatorType::CloseBrace) {
58 break close_span;
59 }
60 };
61
62 let root_operation_type_definitions_span = open_span.merge(&close_span);
63
64 Ok(Self {
65 description,
66 schema_identifier_span,
67 directives: directives.map(Directives::from),
68 root_operation_type_definitions,
69 root_operation_type_definitions_span,
70 })
71 }
72}
73
74#[derive(Debug)]
75pub struct RootOperationTypeDefinition<'a> {
76 operation_type: OperationType,
77 name: Name<'a>,
78}
79
80impl<'a> RootOperationTypeDefinition<'a> {
81 pub(crate) fn operation_type(&self) -> OperationType {
82 self.operation_type
83 }
84
85 pub(crate) fn name_token(&self) -> &Name<'a> {
86 &self.name
87 }
88
89 pub(crate) fn name(&self) -> &str {
90 self.name.as_ref()
91 }
92}
93
94impl<'a> FromTokens<'a> for RootOperationTypeDefinition<'a> {
95 fn from_tokens(tokens: &mut impl Tokens<'a>) -> Result<Self, ParseError> {
96 let operation_type = tokens.expect_name().and_then(|name| {
97 OperationType::from_str(name.as_str()).map_err(|_| ParseError::ExpectedOneOf {
98 span: name.into(),
99 values: OperationType::POSSIBLE_VALUES,
100 })
101 })?;
102 tokens.expect_punctuator(PunctuatorType::Colon)?;
103 let name = tokens.expect_name()?;
104 Ok(Self {
105 operation_type,
106 name,
107 })
108 }
109}