1use crate::directive::DirectiveLocation;
2use crate::input_value::Constness;
3use crate::input_value::InputValue;
4use crate::input_value::InputValueDef;
5use crate::name::Name;
6use crate::DocumentBuilder;
7use apollo_compiler::ast;
8use apollo_compiler::Node;
9use arbitrary::Result as ArbitraryResult;
10use indexmap::IndexSet;
11
12#[derive(Debug, Clone, PartialEq)]
20pub struct ArgumentsDef {
21 pub(crate) input_value_definitions: Vec<InputValueDef>,
22}
23
24impl From<ArgumentsDef> for Vec<Node<ast::InputValueDefinition>> {
25 fn from(args_def: ArgumentsDef) -> Self {
26 args_def
27 .input_value_definitions
28 .into_iter()
29 .map(|x| Node::new(x.into()))
30 .collect()
31 }
32}
33
34impl TryFrom<apollo_parser::cst::ArgumentsDefinition> for ArgumentsDef {
35 type Error = crate::FromError;
36
37 fn try_from(args_def: apollo_parser::cst::ArgumentsDefinition) -> Result<Self, Self::Error> {
38 Ok(Self {
39 input_value_definitions: args_def
40 .input_value_definitions()
41 .map(InputValueDef::try_from)
42 .collect::<Result<_, _>>()?,
43 })
44 }
45}
46
47#[derive(Debug, Clone, PartialEq)]
54pub struct Argument {
55 pub(crate) name: Name,
56 pub(crate) value: InputValue,
57}
58
59impl From<Argument> for ast::Argument {
60 fn from(arg: Argument) -> Self {
61 Self {
62 name: arg.name.into(),
63 value: Node::new(arg.value.into()),
64 }
65 }
66}
67
68impl TryFrom<apollo_parser::cst::Argument> for Argument {
69 type Error = crate::FromError;
70
71 fn try_from(argument: apollo_parser::cst::Argument) -> Result<Self, Self::Error> {
72 Ok(Self {
73 name: argument.name().unwrap().into(),
74 value: argument.value().unwrap().try_into()?,
75 })
76 }
77}
78
79impl DocumentBuilder<'_> {
80 pub fn arguments(&mut self, constness: Constness) -> ArbitraryResult<Vec<Argument>> {
82 let num_arguments = self.u.int_in_range(0..=4)?;
83 let arguments = (0..num_arguments)
84 .map(|_| self.argument(constness))
85 .collect::<ArbitraryResult<Vec<_>>>()?;
86
87 Ok(arguments)
88 }
89
90 pub fn arguments_with_def(
92 &mut self,
93 args_def: &ArgumentsDef,
94 ) -> ArbitraryResult<Vec<Argument>> {
95 let arguments = args_def
96 .input_value_definitions
97 .iter()
98 .map(|input_val_def| self.argument_with_def(input_val_def))
99 .collect::<ArbitraryResult<Vec<_>>>()?;
100
101 Ok(arguments)
102 }
103
104 pub fn argument(&mut self, constness: Constness) -> ArbitraryResult<Argument> {
106 let name = self.name()?;
107 let value = self.input_value(constness)?;
108
109 Ok(Argument { name, value })
110 }
111
112 pub fn argument_with_def(
114 &mut self,
115 input_val_def: &InputValueDef,
116 ) -> ArbitraryResult<Argument> {
117 let name = input_val_def.name.clone();
118 let value = self.input_value_for_type(&input_val_def.ty)?;
119
120 Ok(Argument { name, value })
121 }
122
123 pub fn arguments_definition(&mut self) -> ArbitraryResult<ArgumentsDef> {
125 Ok(ArgumentsDef {
126 input_value_definitions: self.input_values_def(
127 DirectiveLocation::ArgumentDefinition,
128 &IndexSet::new(),
129 None,
130 )?,
131 })
132 }
133}