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
use std::collections::HashMap;
use arbitrary::Result;
use crate::{
description::Description,
directive::{Directive, DirectiveLocation},
name::Name,
DocumentBuilder,
};
#[derive(Debug)]
pub struct ScalarTypeDef {
pub(crate) name: Name,
pub(crate) description: Option<Description>,
pub(crate) directives: HashMap<Name, Directive>,
pub(crate) extend: bool,
}
impl From<ScalarTypeDef> for apollo_encoder::ScalarDefinition {
fn from(scalar_def: ScalarTypeDef) -> Self {
let mut new_scalar_def = Self::new(scalar_def.name.into());
if let Some(description) = scalar_def.description {
new_scalar_def.description(description.into());
}
scalar_def
.directives
.into_iter()
.for_each(|(_, directive)| new_scalar_def.directive(directive.into()));
if scalar_def.extend {
new_scalar_def.extend();
}
new_scalar_def
}
}
#[cfg(feature = "parser-impl")]
impl From<apollo_parser::ast::ScalarTypeDefinition> for ScalarTypeDef {
fn from(scalar_def: apollo_parser::ast::ScalarTypeDefinition) -> Self {
Self {
description: scalar_def
.description()
.map(|d| Description::from(d.to_string())),
name: scalar_def.name().unwrap().into(),
directives: scalar_def
.directives()
.map(|d| {
d.directives()
.map(|d| (d.name().unwrap().into(), Directive::from(d)))
.collect()
})
.unwrap_or_default(),
extend: false,
}
}
}
#[cfg(feature = "parser-impl")]
impl From<apollo_parser::ast::ScalarTypeExtension> for ScalarTypeDef {
fn from(scalar_def: apollo_parser::ast::ScalarTypeExtension) -> Self {
Self {
description: None,
name: scalar_def.name().unwrap().into(),
directives: scalar_def
.directives()
.map(|d| {
d.directives()
.map(|d| (d.name().unwrap().into(), Directive::from(d)))
.collect()
})
.unwrap_or_default(),
extend: true,
}
}
}
impl<'a> DocumentBuilder<'a> {
pub fn scalar_type_definition(&mut self) -> Result<ScalarTypeDef> {
let extend = !self.scalar_type_defs.is_empty() && self.u.arbitrary().unwrap_or(false);
let name = if extend {
let available_scalars: Vec<&Name> = self
.scalar_type_defs
.iter()
.filter_map(|scalar| {
if scalar.extend {
None
} else {
Some(&scalar.name)
}
})
.collect();
(*self.u.choose(&available_scalars)?).clone()
} else {
self.type_name()?
};
let description = self
.u
.arbitrary()
.unwrap_or(false)
.then(|| self.description())
.transpose()?;
let directives = self.directives(DirectiveLocation::Scalar)?;
let extend = !directives.is_empty() && self.u.arbitrary().unwrap_or(false);
Ok(ScalarTypeDef {
name,
description,
directives,
extend,
})
}
}