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
91
92
use crate::ast::executable::SelectionSet;
use crate::ast::{
    FromTokens, IsMatch, ParseError, Tokens, TryFromTokens, VariableArguments, VariableDirectives,
};
use crate::lexical_token::{Name, PunctuatorType};

#[derive(Debug)]
pub struct Field<'a> {
    alias: Option<Name<'a>>,
    name: Name<'a>,
    arguments: Option<VariableArguments<'a>>,
    directives: VariableDirectives<'a>,
    selection_set: Option<SelectionSet<'a>>,
}

impl<'a> FromTokens<'a> for Field<'a> {
    fn from_tokens(tokens: &mut impl Tokens<'a>) -> Result<Self, ParseError> {
        let has_alias = tokens.peek_punctuator_matches(1, PunctuatorType::Colon);
        let (alias, name) = if has_alias {
            let alias = Some(tokens.expect_name()?);
            tokens.expect_punctuator(PunctuatorType::Colon)?;
            let name = tokens.expect_name()?;
            (alias, name)
        } else {
            (None, tokens.expect_name()?)
        };
        let arguments = VariableArguments::try_from_tokens(tokens).transpose()?;
        let directives = VariableDirectives::from_tokens(tokens)?;
        let selection_set = SelectionSet::try_from_tokens(tokens).transpose()?;
        Ok(Self {
            alias,
            name,
            arguments,
            directives,
            selection_set,
        })
    }
}

impl<'a> IsMatch<'a> for Field<'a> {
    fn is_match(tokens: &mut impl Tokens<'a>) -> bool {
        tokens.peek_name(0).is_some()
    }
}

impl<'a> Field<'a> {
    pub fn response_key(&self) -> &str {
        if let Some(alias) = &self.alias {
            alias.as_ref()
        } else {
            self.name.as_ref()
        }
    }

    pub fn name(&self) -> &Name<'a> {
        &self.name
    }

    pub fn arguments(&self) -> Option<&VariableArguments> {
        self.arguments.as_ref()
    }

    pub fn selection_set(&self) -> Option<&SelectionSet> {
        self.selection_set.as_ref()
    }
}

impl<'a> bluejay_core::executable::Field for Field<'a> {
    type Arguments = VariableArguments<'a>;
    type Directives = VariableDirectives<'a>;
    type SelectionSet = SelectionSet<'a>;

    fn alias(&self) -> Option<&str> {
        self.alias.as_ref().map(|name| name.as_ref())
    }

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

    fn arguments(&self) -> Option<&Self::Arguments> {
        self.arguments.as_ref()
    }

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

    fn selection_set(&self) -> Option<&Self::SelectionSet> {
        self.selection_set.as_ref()
    }
}