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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
use crate::ast::executable::SelectionSet;
use crate::ast::{
    FromTokens, IsMatch, ParseError, Tokens, TryFromTokens, VariableArguments, VariableDirectives,
};
use crate::lexical_token::{Name, PunctuatorType};
use crate::{HasSpan, Span};
use std::cmp::{Eq, PartialEq};
use std::hash::{Hash, Hasher};

#[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>>,
    span: Span,
}

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()?;
        let start_span = alias.as_ref().unwrap_or(&name).span();
        let end_span = if let Some(selection_set) = &selection_set {
            selection_set.span()
        } else if let Some(directive_span) = directives.span() {
            directive_span
        } else if let Some(arguments) = &arguments {
            arguments.span()
        } else {
            name.span()
        };
        let span = start_span.merge(end_span);
        Ok(Self {
            alias,
            name,
            arguments,
            directives,
            selection_set,
            span,
        })
    }
}

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()
    }
}

impl<'a> HasSpan for Field<'a> {
    fn span(&self) -> &Span {
        &self.span
    }
}

impl<'a> Hash for Field<'a> {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.span().hash(state);
    }
}

impl<'a> PartialEq for Field<'a> {
    fn eq(&self, other: &Self) -> bool {
        self.span() == other.span()
    }
}

impl<'a> Eq for Field<'a> {}