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
use crate::ast::executable::{SelectionSet, TypeCondition};
use crate::ast::{FromTokens, IsMatch, ParseError, Tokens, VariableDirectives};
use crate::lexical_token::Name;
use crate::{HasSpan, Span};
use std::cmp::{Eq, Ord, Ordering, PartialEq, PartialOrd};
use std::hash::{Hash, Hasher};

#[derive(Debug)]
pub struct FragmentDefinition<'a> {
    name: Name<'a>,
    type_condition: TypeCondition<'a>,
    directives: VariableDirectives<'a>,
    selection_set: SelectionSet<'a>,
    span: Span,
}

impl<'a> IsMatch<'a> for FragmentDefinition<'a> {
    fn is_match(tokens: &mut impl Tokens<'a>) -> bool {
        tokens.peek_name_matches(0, "fragment")
    }
}

impl<'a> FromTokens<'a> for FragmentDefinition<'a> {
    fn from_tokens(tokens: &mut impl Tokens<'a>) -> Result<Self, ParseError> {
        let fragment_identifier_span = tokens.expect_name_value("fragment")?;
        let name = tokens.expect_name()?;
        if name.as_ref() == TypeCondition::ON {
            // TODO: make this error message better
            return Err(ParseError::UnexpectedToken { span: name.into() });
        }
        let type_condition = TypeCondition::from_tokens(tokens)?;
        let directives = VariableDirectives::from_tokens(tokens)?;
        let selection_set = SelectionSet::from_tokens(tokens)?;
        let span = fragment_identifier_span.merge(selection_set.span());
        Ok(Self {
            name,
            type_condition,
            directives,
            selection_set,
            span,
        })
    }
}

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

    pub fn type_condition(&self) -> &TypeCondition<'a> {
        &self.type_condition
    }

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

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

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

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

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

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

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

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

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

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

impl<'a> Ord for FragmentDefinition<'a> {
    fn cmp(&self, other: &Self) -> Ordering {
        self.span().cmp(other.span())
    }
}

impl<'a> PartialOrd for FragmentDefinition<'a> {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}