bluejay_visibility/
warden.rs

1use bluejay_core::{
2    definition::{
3        prelude::*, InputValueDefinition, ScalarTypeDefinition, SchemaDefinition,
4        TypeDefinitionReference,
5    },
6    Directive,
7};
8use std::marker::PhantomData;
9
10pub trait Warden: Sized {
11    type SchemaDefinition: SchemaDefinition;
12    type TypeDefinitionsForName<'a>: Iterator<
13            Item = TypeDefinitionReference<
14                'a,
15                <Self::SchemaDefinition as SchemaDefinition>::TypeDefinition,
16            >,
17        > + 'a
18    where
19        Self: 'a;
20
21    fn is_field_definition_visible(
22        &self,
23        field_definition: &<Self::SchemaDefinition as SchemaDefinition>::FieldDefinition,
24    ) -> bool;
25
26    fn is_input_value_definition_visible(
27        &self,
28        input_value_definition: &<Self::SchemaDefinition as SchemaDefinition>::InputValueDefinition,
29    ) -> bool;
30
31    fn is_enum_value_definition_visible(
32        &self,
33        enum_value_definition: &<Self::SchemaDefinition as SchemaDefinition>::EnumValueDefinition,
34    ) -> bool;
35
36    fn is_union_member_type_visible(
37        &self,
38        union_member_type: &<Self::SchemaDefinition as SchemaDefinition>::UnionMemberType,
39    ) -> bool;
40
41    fn is_interface_implementation_visible(
42        &self,
43        interface_implementation: &<Self::SchemaDefinition as SchemaDefinition>::InterfaceImplementation,
44    ) -> bool;
45
46    fn is_directive_definition_visible(
47        &self,
48        directive_definition: &<Self::SchemaDefinition as SchemaDefinition>::DirectiveDefinition,
49    ) -> bool;
50
51    fn is_custom_scalar_type_definition_visible(
52        &self,
53        custom_scalar_type_definition: &<Self::SchemaDefinition as SchemaDefinition>::CustomScalarTypeDefinition,
54    ) -> bool;
55
56    fn is_enum_type_definition_visible(
57        &self,
58        enum_type_definition: &<Self::SchemaDefinition as SchemaDefinition>::EnumTypeDefinition,
59    ) -> bool;
60
61    fn is_input_object_type_definition_visible(
62        &self,
63        input_object_type_definition: &<Self::SchemaDefinition as SchemaDefinition>::InputObjectTypeDefinition,
64    ) -> bool;
65
66    fn is_interface_type_definition_visible(
67        &self,
68        interface_type_definition: &<Self::SchemaDefinition as SchemaDefinition>::InterfaceTypeDefinition,
69    ) -> bool;
70
71    fn is_object_type_definition_visible(
72        &self,
73        object_type_definition: &<Self::SchemaDefinition as SchemaDefinition>::ObjectTypeDefinition,
74    ) -> bool;
75
76    fn is_union_type_definition_visible(
77        &self,
78        union_type_definition: &<Self::SchemaDefinition as SchemaDefinition>::UnionTypeDefinition,
79    ) -> bool;
80
81    // Each of the following methods is a sign that we should have more specific traits than the `bluejay-core::definition`
82    // ones. The more we add, the greater the argument that we should create dedicated traits instead of using the core ones.
83
84    fn input_value_definition_default_value<'a>(
85        &self,
86        scoped_input_value_definition: &crate::InputValueDefinition<'a, Self::SchemaDefinition, Self>,
87    ) -> Option<&'a <<Self::SchemaDefinition as SchemaDefinition>::InputValueDefinition as InputValueDefinition>::Value>{
88        scoped_input_value_definition.inner().default_value()
89    }
90
91    fn directive_arguments<'a>(
92        &self,
93        scoped_directive: &crate::Directive<'a, Self::SchemaDefinition, Self>,
94    ) -> Option<
95        &'a <<Self::SchemaDefinition as SchemaDefinition>::Directive as Directive<true>>::Arguments,
96    > {
97        scoped_directive.inner().arguments()
98    }
99
100    fn custom_scalar_definition_coerce_input<const CONST: bool>(
101        &self,
102        custom_scalar_type_definition: &<Self::SchemaDefinition as SchemaDefinition>::CustomScalarTypeDefinition,
103        value: &impl bluejay_core::Value<CONST>,
104    ) -> Result<(), std::borrow::Cow<'static, str>> {
105        custom_scalar_type_definition.coerce_input(value)
106    }
107
108    fn type_definitions_for_name<'a>(
109        &self,
110        schema_definition: &'a Self::SchemaDefinition,
111        type_name: &str,
112    ) -> Self::TypeDefinitionsForName<'a>;
113
114    fn object_type_definitions_equal(
115        &self,
116        first: &<Self::SchemaDefinition as SchemaDefinition>::ObjectTypeDefinition,
117        second: &<Self::SchemaDefinition as SchemaDefinition>::ObjectTypeDefinition,
118    ) -> bool {
119        first.name() == second.name()
120    }
121
122    fn scalar_type_definitions_equal(
123        &self,
124        first: &<Self::SchemaDefinition as SchemaDefinition>::CustomScalarTypeDefinition,
125        second: &<Self::SchemaDefinition as SchemaDefinition>::CustomScalarTypeDefinition,
126    ) -> bool {
127        first.name() == second.name()
128    }
129
130    fn enum_type_definitions_equal(
131        &self,
132        first: &<Self::SchemaDefinition as SchemaDefinition>::EnumTypeDefinition,
133        second: &<Self::SchemaDefinition as SchemaDefinition>::EnumTypeDefinition,
134    ) -> bool {
135        first.name() == second.name()
136    }
137
138    fn input_object_type_definitions_equal(
139        &self,
140        first: &<Self::SchemaDefinition as SchemaDefinition>::InputObjectTypeDefinition,
141        second: &<Self::SchemaDefinition as SchemaDefinition>::InputObjectTypeDefinition,
142    ) -> bool {
143        first.name() == second.name()
144    }
145
146    fn interface_type_definitions_equal(
147        &self,
148        first: &<Self::SchemaDefinition as SchemaDefinition>::InterfaceTypeDefinition,
149        second: &<Self::SchemaDefinition as SchemaDefinition>::InterfaceTypeDefinition,
150    ) -> bool {
151        first.name() == second.name()
152    }
153
154    fn union_type_definitions_equal(
155        &self,
156        first: &<Self::SchemaDefinition as SchemaDefinition>::UnionTypeDefinition,
157        second: &<Self::SchemaDefinition as SchemaDefinition>::UnionTypeDefinition,
158    ) -> bool {
159        first.name() == second.name()
160    }
161}
162
163pub struct NullWarden<S: SchemaDefinition>(PhantomData<S>);
164
165impl<S: SchemaDefinition> Default for NullWarden<S> {
166    fn default() -> Self {
167        Self(Default::default())
168    }
169}
170
171impl<S: SchemaDefinition> Warden for NullWarden<S> {
172    type SchemaDefinition = S;
173    type TypeDefinitionsForName<'a>
174        = std::option::IntoIter<TypeDefinitionReference<'a, S::TypeDefinition>>
175    where
176        Self: 'a;
177
178    fn is_field_definition_visible(
179        &self,
180        _: &<Self::SchemaDefinition as SchemaDefinition>::FieldDefinition,
181    ) -> bool {
182        true
183    }
184
185    fn is_input_value_definition_visible(
186        &self,
187        _: &<Self::SchemaDefinition as SchemaDefinition>::InputValueDefinition,
188    ) -> bool {
189        true
190    }
191
192    fn is_enum_value_definition_visible(
193        &self,
194        _: &<Self::SchemaDefinition as SchemaDefinition>::EnumValueDefinition,
195    ) -> bool {
196        true
197    }
198
199    fn is_union_member_type_visible(
200        &self,
201        _: &<Self::SchemaDefinition as SchemaDefinition>::UnionMemberType,
202    ) -> bool {
203        true
204    }
205
206    fn is_interface_implementation_visible(
207        &self,
208        _: &<Self::SchemaDefinition as SchemaDefinition>::InterfaceImplementation,
209    ) -> bool {
210        true
211    }
212
213    fn is_directive_definition_visible(
214        &self,
215        _: &<Self::SchemaDefinition as SchemaDefinition>::DirectiveDefinition,
216    ) -> bool {
217        true
218    }
219
220    fn is_custom_scalar_type_definition_visible(
221        &self,
222        _: &<Self::SchemaDefinition as SchemaDefinition>::CustomScalarTypeDefinition,
223    ) -> bool {
224        true
225    }
226
227    fn is_enum_type_definition_visible(
228        &self,
229        _: &<Self::SchemaDefinition as SchemaDefinition>::EnumTypeDefinition,
230    ) -> bool {
231        true
232    }
233
234    fn is_input_object_type_definition_visible(
235        &self,
236        _: &<Self::SchemaDefinition as SchemaDefinition>::InputObjectTypeDefinition,
237    ) -> bool {
238        true
239    }
240
241    fn is_interface_type_definition_visible(
242        &self,
243        _: &<Self::SchemaDefinition as SchemaDefinition>::InterfaceTypeDefinition,
244    ) -> bool {
245        true
246    }
247
248    fn is_object_type_definition_visible(
249        &self,
250        _: &<Self::SchemaDefinition as SchemaDefinition>::ObjectTypeDefinition,
251    ) -> bool {
252        true
253    }
254
255    fn is_union_type_definition_visible(
256        &self,
257        _: &<Self::SchemaDefinition as SchemaDefinition>::UnionTypeDefinition,
258    ) -> bool {
259        true
260    }
261
262    fn type_definitions_for_name<'a>(
263        &self,
264        schema_definition: &'a Self::SchemaDefinition,
265        type_name: &str,
266    ) -> Self::TypeDefinitionsForName<'a> {
267        schema_definition.get_type_definition(type_name).into_iter()
268    }
269}