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