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
use std::collections::HashSet;
use apollo_encoder::InterfaceDefinition;
use arbitrary::Result;
use crate::{
description::Description, directive::Directive, field::FieldDef, name::Name, DocumentBuilder,
};
#[derive(Debug, Clone)]
pub struct InterfaceTypeDef {
pub(crate) description: Option<Description>,
pub(crate) name: Name,
pub(crate) interfaces: HashSet<Name>,
pub(crate) directives: Vec<Directive>,
pub(crate) fields_def: Vec<FieldDef>,
pub(crate) extend: bool,
}
impl From<InterfaceTypeDef> for InterfaceDefinition {
fn from(itf: InterfaceTypeDef) -> Self {
let mut itf_def = InterfaceDefinition::new(itf.name.into());
itf_def.description(itf.description.map(String::from));
itf.fields_def
.into_iter()
.for_each(|f| itf_def.field(f.into()));
itf.directives
.into_iter()
.for_each(|directive| itf_def.directive(directive.into()));
itf.interfaces
.into_iter()
.for_each(|interface| itf_def.interface(interface.into()));
if itf.extend {
itf_def.extend();
}
itf_def
}
}
impl<'a> DocumentBuilder<'a> {
pub fn interface_type_definition(&mut self) -> Result<InterfaceTypeDef> {
let description = self
.u
.arbitrary()
.unwrap_or(false)
.then(|| self.description())
.transpose()?;
let name = self.type_name()?;
let fields_def = self.fields_definition(&[])?;
let directives = self.directives()?;
let interfaces = self.implements_interfaces()?;
Ok(InterfaceTypeDef {
description,
name,
fields_def,
directives,
extend: self.u.arbitrary().unwrap_or(false),
interfaces,
})
}
pub fn implements_interfaces(&mut self) -> Result<HashSet<Name>> {
if self.interface_type_defs.is_empty() {
return Ok(HashSet::new());
}
let num_itf = self
.u
.int_in_range(0..=(self.interface_type_defs.len() - 1))?;
let mut interface_impls = HashSet::with_capacity(num_itf);
for _ in 0..num_itf {
interface_impls.insert(self.u.choose(&self.interface_type_defs)?.name.clone());
}
Ok(interface_impls)
}
}