bluejay_parser/ast/definition/
field_definition.rs

1use crate::ast::definition::{ArgumentsDefinition, Context, Directives, OutputType};
2use crate::ast::{ConstDirectives, FromTokens, Parse, ParseError, Tokens, TryFromTokens};
3use crate::lexical_token::{Name, PunctuatorType, StringValue};
4use bluejay_core::definition::{FieldDefinition as CoreFieldDefinition, HasDirectives};
5
6#[derive(Debug)]
7pub struct FieldDefinition<'a, C: Context> {
8    description: Option<StringValue<'a>>,
9    name: Name<'a>,
10    arguments_definition: Option<ArgumentsDefinition<'a, C>>,
11    r#type: OutputType<'a, C>,
12    directives: Option<Directives<'a, C>>,
13    is_builtin: bool,
14}
15
16impl<'a, C: Context> FieldDefinition<'a, C> {
17    const __TYPENAME_DEFINITION: &'static str = "__typename: String!";
18    const __SCHEMA_DEFINITION: &'static str = "__schema: __Schema!";
19    const __TYPE_DEFINITION: &'static str = "__type(name: String!): __Type";
20
21    fn builtin(s: &'static str) -> Self {
22        let mut definition = Self::parse(s).unwrap();
23        definition.is_builtin = true;
24        definition
25    }
26
27    pub(crate) fn __typename() -> Self {
28        Self::builtin(Self::__TYPENAME_DEFINITION)
29    }
30
31    pub(crate) fn __schema() -> Self {
32        Self::builtin(Self::__SCHEMA_DEFINITION)
33    }
34
35    pub(crate) fn __type() -> Self {
36        Self::builtin(Self::__TYPE_DEFINITION)
37    }
38}
39
40impl<'a, C: Context> CoreFieldDefinition for FieldDefinition<'a, C> {
41    type ArgumentsDefinition = ArgumentsDefinition<'a, C>;
42    type OutputType = OutputType<'a, C>;
43
44    fn description(&self) -> Option<&str> {
45        self.description.as_ref().map(AsRef::as_ref)
46    }
47
48    fn name(&self) -> &str {
49        self.name.as_ref()
50    }
51
52    fn arguments_definition(&self) -> Option<&Self::ArgumentsDefinition> {
53        self.arguments_definition.as_ref()
54    }
55
56    fn r#type(&self) -> &Self::OutputType {
57        &self.r#type
58    }
59
60    fn is_builtin(&self) -> bool {
61        self.is_builtin
62    }
63}
64
65impl<'a, C: Context> FromTokens<'a> for FieldDefinition<'a, C> {
66    fn from_tokens(tokens: &mut impl Tokens<'a>) -> Result<Self, ParseError> {
67        let description = tokens.next_if_string_value();
68        let name = tokens.expect_name()?;
69        let arguments_definition = ArgumentsDefinition::try_from_tokens(tokens).transpose()?;
70        tokens.expect_punctuator(PunctuatorType::Colon)?;
71        let r#type = OutputType::from_tokens(tokens)?;
72        let directives = ConstDirectives::try_from_tokens(tokens).transpose()?;
73        Ok(Self {
74            description,
75            name,
76            arguments_definition,
77            r#type,
78            directives: directives.map(Directives::from),
79            is_builtin: false,
80        })
81    }
82}
83
84impl<'a, C: Context> HasDirectives for FieldDefinition<'a, C> {
85    type Directives = Directives<'a, C>;
86
87    fn directives(&self) -> Option<&Self::Directives> {
88        self.directives.as_ref()
89    }
90}