|
| 1 | +use apollo_compiler::ast::{Definition, InputObjectTypeDefinition, Name, Type}; |
| 2 | +use apollo_compiler::schema::ExtendedType; |
| 3 | +use arbitrary::Unstructured; |
| 4 | + |
| 5 | +pub(crate) trait DefinitionExt { |
| 6 | + fn ty(&self, u: &mut Unstructured) -> arbitrary::Result<Type>; |
| 7 | +} |
| 8 | + |
| 9 | +impl DefinitionExt for Definition { |
| 10 | + fn ty(&self, u: &mut Unstructured) -> arbitrary::Result<Type> { |
| 11 | + let name = self.name().expect("definition must have a name").clone(); |
| 12 | + Ok(ty(u, name)?) |
| 13 | + } |
| 14 | +} |
| 15 | + |
| 16 | +impl DefinitionExt for InputObjectTypeDefinition { |
| 17 | + fn ty(&self, u: &mut Unstructured) -> arbitrary::Result<Type> { |
| 18 | + Ok(ty(u, self.name.clone())?) |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +fn ty(u: &mut Unstructured, name: Name) -> arbitrary::Result<Type> { |
| 23 | + let mut ty = if u.arbitrary()? { |
| 24 | + Type::Named(name) |
| 25 | + } else { |
| 26 | + Type::NonNullNamed(name) |
| 27 | + }; |
| 28 | + |
| 29 | + for _ in 0..u.int_in_range(0..=5)? { |
| 30 | + if u.arbitrary()? { |
| 31 | + ty = Type::List(Box::new(ty)) |
| 32 | + } else { |
| 33 | + ty = Type::NonNullList(Box::new(ty)) |
| 34 | + }; |
| 35 | + } |
| 36 | + Ok(ty) |
| 37 | +} |
| 38 | + |
| 39 | +#[derive(Debug)] |
| 40 | +pub(crate) enum DefinitionKind { |
| 41 | + OperationDefinition, |
| 42 | + FragmentDefinition, |
| 43 | + DirectiveDefinition, |
| 44 | + SchemaDefinition, |
| 45 | + ScalarTypeDefinition, |
| 46 | + ObjectTypeDefinition, |
| 47 | + InterfaceTypeDefinition, |
| 48 | + UnionTypeDefinition, |
| 49 | + EnumTypeDefinition, |
| 50 | + InputObjectTypeDefinition, |
| 51 | + SchemaExtension, |
| 52 | + ScalarTypeExtension, |
| 53 | + ObjectTypeExtension, |
| 54 | + InterfaceTypeExtension, |
| 55 | + UnionTypeExtension, |
| 56 | + EnumTypeExtension, |
| 57 | + InputObjectTypeExtension, |
| 58 | +} |
| 59 | + |
| 60 | +impl DefinitionKind { |
| 61 | + pub(crate) fn matches(&self, definition: &Definition) -> bool { |
| 62 | + match (self, definition) { |
| 63 | + (Self::OperationDefinition, Definition::OperationDefinition(_)) => true, |
| 64 | + (Self::FragmentDefinition, Definition::FragmentDefinition(_)) => true, |
| 65 | + (Self::DirectiveDefinition, Definition::DirectiveDefinition(_)) => true, |
| 66 | + (Self::SchemaDefinition, Definition::SchemaDefinition(_)) => true, |
| 67 | + (Self::ScalarTypeDefinition, Definition::ScalarTypeDefinition(_)) => true, |
| 68 | + (Self::ObjectTypeDefinition, Definition::ObjectTypeDefinition(_)) => true, |
| 69 | + (Self::InterfaceTypeDefinition, Definition::InterfaceTypeDefinition(_)) => true, |
| 70 | + (Self::UnionTypeDefinition, Definition::UnionTypeDefinition(_)) => true, |
| 71 | + (Self::EnumTypeDefinition, Definition::EnumTypeDefinition(_)) => true, |
| 72 | + (Self::InputObjectTypeDefinition, Definition::InputObjectTypeDefinition(_)) => true, |
| 73 | + (Self::SchemaExtension, Definition::SchemaExtension(_)) => true, |
| 74 | + (Self::ScalarTypeExtension, Definition::ScalarTypeExtension(_)) => true, |
| 75 | + (Self::ObjectTypeExtension, Definition::ObjectTypeExtension(_)) => true, |
| 76 | + (Self::InterfaceTypeExtension, Definition::InterfaceTypeExtension(_)) => true, |
| 77 | + (Self::UnionTypeExtension, Definition::UnionTypeExtension(_)) => true, |
| 78 | + (Self::EnumTypeExtension, Definition::EnumTypeExtension(_)) => true, |
| 79 | + (Self::InputObjectTypeExtension, Definition::InputObjectTypeExtension(_)) => true, |
| 80 | + _ => false, |
| 81 | + } |
| 82 | + } |
| 83 | +} |
0 commit comments