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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
use rowan::{GreenToken, SyntaxKind};
use crate::{ast, ast::AstNode, SyntaxNode, TokenText};
use std::num::{ParseFloatError, ParseIntError};
impl ast::Name {
    pub fn text(&self) -> TokenText {
        text_of_first_token(self.syntax())
    }
}
impl ast::Variable {
    pub fn text(&self) -> TokenText {
        self.name()
            .expect("Cannot get variable's NAME token")
            .text()
    }
}
impl ast::EnumValue {
    pub fn text(&self) -> TokenText {
        self.name()
            .expect("Cannot get enum value's NAME token")
            .text()
    }
}
impl ast::DirectiveLocation {
    pub fn text(self) -> Option<TokenText> {
        let txt = if self.query_token().is_some() {
            Some("QUERY")
        } else if self.mutation_token().is_some() {
            Some("MUTATION")
        } else if self.subscription_token().is_some() {
            Some("SUBSCRIPTION")
        } else if self.field_token().is_some() {
            Some("FIELD")
        } else if self.fragment_definition_token().is_some() {
            Some("FRAGMENT_DEFINITION")
        } else if self.fragment_spread_token().is_some() {
            Some("FRAGMENT_SPREAD")
        } else if self.inline_fragment_token().is_some() {
            Some("INLINE_FRAGMENT")
        } else if self.variable_definition_token().is_some() {
            Some("VARIABLE_DEFINITION")
        } else if self.schema_token().is_some() {
            Some("SCHEMA")
        } else if self.scalar_token().is_some() {
            Some("SCALAR")
        } else if self.object_token().is_some() {
            Some("OBJECT")
        } else if self.field_definition_token().is_some() {
            Some("FIELD_DEFINITION")
        } else if self.argument_definition_token().is_some() {
            Some("ARGUMENT_DEFINITION")
        } else if self.interface_token().is_some() {
            Some("INTERFACE")
        } else if self.union_token().is_some() {
            Some("UNION")
        } else if self.enum_token().is_some() {
            Some("ENUM")
        } else if self.enum_value_token().is_some() {
            Some("ENUM_VALUE")
        } else if self.input_object_token().is_some() {
            Some("INPUT_OBJECT")
        } else if self.input_field_definition_token().is_some() {
            Some("INPUT_FIELD_DEFINITION")
        } else {
            None
        };
        txt.map(|txt| {
            TokenText(GreenToken::new(
                SyntaxKind(crate::SyntaxKind::DIRECTIVE_LOCATION as u16),
                txt,
            ))
        })
    }
}
impl From<ast::StringValue> for String {
    fn from(val: ast::StringValue) -> Self {
        Self::from(&val)
    }
}
impl From<&'_ ast::StringValue> for String {
    fn from(val: &'_ ast::StringValue) -> Self {
        let text = text_of_first_token(val.syntax());
        text.trim_start_matches('"')
            .trim_end_matches('"')
            .to_string()
    }
}
impl TryFrom<ast::IntValue> for i32 {
    type Error = ParseIntError;
    fn try_from(val: ast::IntValue) -> Result<Self, Self::Error> {
        Self::try_from(&val)
    }
}
impl TryFrom<&'_ ast::IntValue> for i32 {
    type Error = ParseIntError;
    fn try_from(val: &'_ ast::IntValue) -> Result<Self, Self::Error> {
        let text = text_of_first_token(val.syntax());
        text.parse()
    }
}
impl TryFrom<ast::IntValue> for f64 {
    type Error = ParseFloatError;
    fn try_from(val: ast::IntValue) -> Result<Self, Self::Error> {
        Self::try_from(&val)
    }
}
impl TryFrom<&'_ ast::IntValue> for f64 {
    type Error = ParseFloatError;
    fn try_from(val: &'_ ast::IntValue) -> Result<Self, Self::Error> {
        let text = text_of_first_token(val.syntax());
        text.parse()
    }
}
impl TryFrom<ast::FloatValue> for f64 {
    type Error = ParseFloatError;
    fn try_from(val: ast::FloatValue) -> Result<Self, Self::Error> {
        Self::try_from(&val)
    }
}
impl TryFrom<&'_ ast::FloatValue> for f64 {
    type Error = ParseFloatError;
    fn try_from(val: &'_ ast::FloatValue) -> Result<Self, Self::Error> {
        let text = text_of_first_token(val.syntax());
        text.parse()
    }
}
impl TryFrom<ast::BooleanValue> for bool {
    type Error = std::str::ParseBoolError;
    fn try_from(val: ast::BooleanValue) -> Result<Self, Self::Error> {
        Self::try_from(&val)
    }
}
impl TryFrom<&'_ ast::BooleanValue> for bool {
    type Error = std::str::ParseBoolError;
    fn try_from(val: &'_ ast::BooleanValue) -> Result<Self, Self::Error> {
        let text = text_of_first_token(val.syntax());
        text.parse()
    }
}
fn text_of_first_token(node: &SyntaxNode) -> TokenText {
    let first_token = node
        .green()
        .children()
        .next()
        .and_then(|it| it.into_token())
        .unwrap()
        .to_owned();
    TokenText(first_token)
}