1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
use crate::ast::definition::{ArgumentsDefinition, Context, Directives, OutputType};
use crate::ast::{ConstDirectives, FromTokens, Parse, ParseError, Tokens, TryFromTokens};
use crate::lexical_token::{Name, PunctuatorType, StringValue};
use bluejay_core::definition::{FieldDefinition as CoreFieldDefinition, HasDirectives};

#[derive(Debug)]
pub struct FieldDefinition<'a, C: Context> {
    description: Option<StringValue<'a>>,
    name: Name<'a>,
    arguments_definition: Option<ArgumentsDefinition<'a, C>>,
    r#type: OutputType<'a, C>,
    directives: Option<Directives<'a, C>>,
    is_builtin: bool,
}

impl<'a, C: Context> FieldDefinition<'a, C> {
    const __TYPENAME_DEFINITION: &'static str = "__typename: String!";
    const __SCHEMA_DEFINITION: &'static str = "__schema: __Schema!";
    const __TYPE_DEFINITION: &'static str = "__type(name: String!): __Type";

    fn builtin(s: &'static str) -> Self {
        let mut definition = Self::parse(s).unwrap();
        definition.is_builtin = true;
        definition
    }

    pub(crate) fn __typename() -> Self {
        Self::builtin(Self::__TYPENAME_DEFINITION)
    }

    pub(crate) fn __schema() -> Self {
        Self::builtin(Self::__SCHEMA_DEFINITION)
    }

    pub(crate) fn __type() -> Self {
        Self::builtin(Self::__TYPE_DEFINITION)
    }
}

impl<'a, C: Context> CoreFieldDefinition for FieldDefinition<'a, C> {
    type ArgumentsDefinition = ArgumentsDefinition<'a, C>;
    type OutputType = OutputType<'a, C>;

    fn description(&self) -> Option<&str> {
        self.description.as_ref().map(AsRef::as_ref)
    }

    fn name(&self) -> &str {
        self.name.as_ref()
    }

    fn arguments_definition(&self) -> Option<&Self::ArgumentsDefinition> {
        self.arguments_definition.as_ref()
    }

    fn r#type(&self) -> &Self::OutputType {
        &self.r#type
    }

    fn is_builtin(&self) -> bool {
        self.is_builtin
    }
}

impl<'a, C: Context> FromTokens<'a> for FieldDefinition<'a, C> {
    fn from_tokens(tokens: &mut impl Tokens<'a>) -> Result<Self, ParseError> {
        let description = tokens.next_if_string_value();
        let name = tokens.expect_name()?;
        let arguments_definition = ArgumentsDefinition::try_from_tokens(tokens).transpose()?;
        tokens.expect_punctuator(PunctuatorType::Colon)?;
        let r#type = OutputType::from_tokens(tokens)?;
        let directives = ConstDirectives::try_from_tokens(tokens).transpose()?;
        Ok(Self {
            description,
            name,
            arguments_definition,
            r#type,
            directives: directives.map(Directives::from),
            is_builtin: false,
        })
    }
}

impl<'a, C: Context> HasDirectives for FieldDefinition<'a, C> {
    type Directives = Directives<'a, C>;

    fn directives(&self) -> Option<&Self::Directives> {
        self.directives.as_ref()
    }
}