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
175
176
177
178
179
use std::fmt;
use crate::{Directive, StringValue, Type_};
#[derive(Debug, PartialEq, Clone)]
pub struct InputValueDefinition {
name: String,
pub(crate) description: Option<StringValue>,
type_: Type_,
default_value: Option<String>,
directives: Vec<Directive>,
}
impl InputValueDefinition {
pub fn new(name: String, type_: Type_) -> Self {
Self {
description: None,
name,
type_,
default_value: None,
directives: Vec::new(),
}
}
pub fn description(&mut self, description: String) {
self.description = Some(StringValue::Input {
source: description,
});
}
pub fn default_value(&mut self, default_value: String) {
self.default_value = Some(default_value);
}
pub fn directive(&mut self, directive: Directive) {
self.directives.push(directive)
}
}
impl fmt::Display for InputValueDefinition {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match &self.description {
Some(description) => {
writeln!(f, "\n {}", description)?;
write!(f, " {}: {}", self.name, self.type_)?;
}
None => {
write!(f, "{}: {}", self.name, self.type_)?;
}
}
if let Some(default) = &self.default_value {
write!(f, " = {}", default)?;
}
for directive in &self.directives {
write!(f, " {}", directive)?;
}
Ok(())
}
}
#[cfg(test)]
mod tests {
use crate::{Argument, Value};
use super::*;
use pretty_assertions::assert_eq;
#[test]
fn it_encodes_simple_values() {
let ty_1 = Type_::NamedType {
name: "SpaceProgram".to_string(),
};
let ty_2 = Type_::List { ty: Box::new(ty_1) };
let ty_3 = Type_::NonNull { ty: Box::new(ty_2) };
let value = InputValueDefinition::new("spaceCat".to_string(), ty_3);
assert_eq!(value.to_string(), r#"spaceCat: [SpaceProgram]!"#);
}
#[test]
fn it_encodes_input_values_with_default() {
let ty_1 = Type_::NamedType {
name: "Breed".to_string(),
};
let ty_2 = Type_::NonNull { ty: Box::new(ty_1) };
let mut value = InputValueDefinition::new("spaceCat".to_string(), ty_2);
value.default_value("\"Norwegian Forest\"".to_string());
assert_eq!(
value.to_string(),
r#"spaceCat: Breed! = "Norwegian Forest""#
);
}
#[test]
fn it_encodes_value_with_directive() {
let ty_1 = Type_::NamedType {
name: "SpaceProgram".to_string(),
};
let ty_2 = Type_::List { ty: Box::new(ty_1) };
let mut value = InputValueDefinition::new("cat".to_string(), ty_2);
let mut directive = Directive::new(String::from("testDirective"));
directive.arg(Argument::new(String::from("first"), Value::Int(1)));
value.description("Very good cats".to_string());
value.directive(directive);
assert_eq!(
value.to_string(),
r#"
"Very good cats"
cat: [SpaceProgram] @testDirective(first: 1)"#
);
}
#[test]
fn it_encodes_valueuments_with_description() {
let ty_1 = Type_::NamedType {
name: "SpaceProgram".to_string(),
};
let ty_2 = Type_::NonNull { ty: Box::new(ty_1) };
let ty_3 = Type_::List { ty: Box::new(ty_2) };
let ty_4 = Type_::NonNull { ty: Box::new(ty_3) };
let mut value = InputValueDefinition::new("spaceCat".to_string(), ty_4);
value.description("Very good space cats".to_string());
assert_eq!(
value.to_string(),
r#"
"Very good space cats"
spaceCat: [SpaceProgram!]!"#
);
}
}