use std::{borrow::Cow, fmt};
use crate::Location;
#[derive(Clone, Debug, PartialEq)]
pub struct Document<'a> {
pub definitions: Vec<Definition<'a>>,
}
impl<'a> Document<'a> {
pub fn operations(&self) -> impl Iterator<Item = &OperationDefinition<'a>> {
self.definitions
.iter()
.filter_map(|definition| match definition {
Definition::Operation(operation) => Some(operation),
_ => None,
})
}
pub fn fragments(&self) -> impl Iterator<Item = &FragmentDefinition<'a>> {
self.definitions
.iter()
.filter_map(|definition| match definition {
Definition::Fragment(fragment) => Some(fragment),
_ => None,
})
}
#[must_use]
pub fn fragment(&self, name: &str) -> Option<&FragmentDefinition<'a>> {
self.fragments().find(|fragment| fragment.name == name)
}
}
#[derive(Clone, Debug, PartialEq)]
pub enum Definition<'a> {
Operation(OperationDefinition<'a>),
Fragment(FragmentDefinition<'a>),
TypeSystem(TypeSystemDefinition<'a>),
}
#[derive(Clone, Debug, PartialEq)]
pub enum TypeSystemDefinition<'a> {
Schema(SchemaDefinition<'a>),
Type(TypeDefinition<'a>),
Directive(DirectiveDefinition<'a>),
SchemaExtension(SchemaExtension<'a>),
TypeExtension(TypeExtension<'a>),
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum OperationKind {
Query,
Mutation,
Subscription,
}
impl OperationKind {
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Self::Query => "query",
Self::Mutation => "mutation",
Self::Subscription => "subscription",
}
}
}
impl fmt::Display for OperationKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct OperationDefinition<'a> {
pub kind: OperationKind,
pub name: Option<&'a str>,
pub variables: Vec<VariableDefinition<'a>>,
pub directives: Vec<Directive<'a>>,
pub selection_set: SelectionSet<'a>,
pub location: Location,
}
#[derive(Clone, Debug, PartialEq)]
pub struct VariableDefinition<'a> {
pub name: &'a str,
pub ty: Type<'a>,
pub default_value: Option<Value<'a>>,
pub directives: Vec<Directive<'a>>,
pub location: Location,
}
#[derive(Clone, Debug, PartialEq)]
pub struct FragmentDefinition<'a> {
pub name: &'a str,
pub type_condition: &'a str,
pub directives: Vec<Directive<'a>>,
pub selection_set: SelectionSet<'a>,
pub location: Location,
}
#[derive(Clone, Debug, PartialEq)]
pub struct SelectionSet<'a> {
pub items: Vec<Selection<'a>>,
pub location: Location,
}
impl<'a> SelectionSet<'a> {
#[must_use]
pub fn is_empty(&self) -> bool {
self.items.is_empty()
}
}
#[derive(Clone, Debug, PartialEq)]
pub enum Selection<'a> {
Field(Field<'a>),
FragmentSpread(FragmentSpread<'a>),
InlineFragment(InlineFragment<'a>),
}
impl<'a> Selection<'a> {
#[must_use]
pub fn directives(&self) -> &[Directive<'a>] {
match self {
Self::Field(field) => &field.directives,
Self::FragmentSpread(spread) => &spread.directives,
Self::InlineFragment(fragment) => &fragment.directives,
}
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct Field<'a> {
pub alias: Option<&'a str>,
pub name: &'a str,
pub arguments: Vec<Argument<'a>>,
pub directives: Vec<Directive<'a>>,
pub selection_set: Option<SelectionSet<'a>>,
pub location: Location,
}
impl<'a> Field<'a> {
#[must_use]
pub fn response_key(&self) -> &'a str {
self.alias.unwrap_or(self.name)
}
#[must_use]
pub fn argument(&self, name: &str) -> Option<&Argument<'a>> {
self.arguments.iter().find(|argument| argument.name == name)
}
}
#[derive(Clone, Copy, Debug)]
pub struct FieldNodes<'a> {
nodes: &'a [&'a Field<'a>],
}
impl<'a> FieldNodes<'a> {
#[must_use]
pub const fn empty() -> Self {
Self { nodes: &[] }
}
#[must_use]
pub const fn new(nodes: &'a [&'a Field<'a>]) -> Self {
Self { nodes }
}
#[must_use]
pub(crate) const fn borrowed(nodes: &'a [&'a Field<'a>]) -> Self {
Self { nodes }
}
#[must_use]
pub const fn as_slice(&self) -> &'a [&'a Field<'a>] {
self.nodes
}
#[must_use]
pub fn len(&self) -> usize {
self.nodes.len()
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.nodes.is_empty()
}
#[must_use]
pub fn first(&self) -> Option<&'a Field<'a>> {
self.nodes.first().copied()
}
#[must_use]
pub fn get(&self, index: usize) -> Option<&'a Field<'a>> {
self.nodes.get(index).copied()
}
pub fn iter(&self) -> FieldNodesIter<'a> {
self.nodes.iter().copied()
}
}
impl<'a> IntoIterator for FieldNodes<'a> {
type Item = &'a Field<'a>;
type IntoIter = FieldNodesIter<'a>;
fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}
impl<'a> std::ops::Deref for FieldNodes<'a> {
type Target = [&'a Field<'a>];
fn deref(&self) -> &Self::Target {
self.nodes
}
}
impl<'a> AsRef<[&'a Field<'a>]> for FieldNodes<'a> {
fn as_ref(&self) -> &[&'a Field<'a>] {
self.nodes
}
}
pub type FieldNodesIter<'a> = std::iter::Copied<std::slice::Iter<'a, &'a Field<'a>>>;
#[derive(Clone, Debug, PartialEq)]
pub struct FragmentSpread<'a> {
pub name: &'a str,
pub directives: Vec<Directive<'a>>,
pub location: Location,
}
#[derive(Clone, Debug, PartialEq)]
pub struct InlineFragment<'a> {
pub type_condition: Option<&'a str>,
pub directives: Vec<Directive<'a>>,
pub selection_set: SelectionSet<'a>,
pub location: Location,
}
#[derive(Clone, Debug, PartialEq)]
pub struct Argument<'a> {
pub name: &'a str,
pub value: Value<'a>,
pub location: Location,
}
#[derive(Clone, Debug, PartialEq)]
pub struct Directive<'a> {
pub name: &'a str,
pub arguments: Vec<Argument<'a>>,
pub location: Location,
}
impl<'a> Directive<'a> {
#[must_use]
pub fn argument(&self, name: &str) -> Option<&Argument<'a>> {
self.arguments.iter().find(|argument| argument.name == name)
}
}
#[derive(Clone, Debug, PartialEq)]
pub enum Value<'a> {
Variable(&'a str),
Int(i128),
Float(f64),
String(Cow<'a, str>),
Boolean(bool),
Null,
Enum(&'a str),
List(Vec<Value<'a>>),
Object(Vec<ObjectField<'a>>),
}
impl<'a> Value<'a> {
#[must_use]
pub fn field(&self, name: &str) -> Option<&Value<'a>> {
match self {
Self::Object(fields) => fields
.iter()
.find(|field| field.name == name)
.map(|field| &field.value),
_ => None,
}
}
#[must_use]
pub const fn is_variable(&self) -> bool {
matches!(self, Self::Variable(_))
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct ObjectField<'a> {
pub name: &'a str,
pub value: Value<'a>,
pub location: Location,
}
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum Type<'a> {
Named(&'a str),
List(Box<Type<'a>>),
NonNull(Box<Type<'a>>),
}
impl<'a> Type<'a> {
#[must_use]
pub fn named_type(&self) -> &'a str {
match self {
Self::Named(name) => name,
Self::List(inner) | Self::NonNull(inner) => inner.named_type(),
}
}
#[must_use]
pub const fn is_non_null(&self) -> bool {
matches!(self, Self::NonNull(_))
}
}
impl fmt::Display for Type<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Named(name) => f.write_str(name),
Self::List(inner) => write!(f, "[{inner}]"),
Self::NonNull(inner) => write!(f, "{inner}!"),
}
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct SchemaDefinition<'a> {
pub description: Option<Cow<'a, str>>,
pub directives: Vec<Directive<'a>>,
pub operations: Vec<RootOperationTypeDefinition<'a>>,
pub location: Location,
}
#[derive(Clone, Debug, PartialEq)]
pub struct RootOperationTypeDefinition<'a> {
pub operation: OperationKind,
pub ty: &'a str,
pub location: Location,
}
#[derive(Clone, Debug, PartialEq)]
pub struct TypeDefinition<'a> {
pub description: Option<Cow<'a, str>>,
pub name: &'a str,
pub kind: TypeDefinitionKind<'a>,
pub directives: Vec<Directive<'a>>,
pub location: Location,
}
#[derive(Clone, Debug, PartialEq)]
pub enum TypeDefinitionKind<'a> {
Scalar,
Object(ObjectTypeDefinition<'a>),
Interface(InterfaceTypeDefinition<'a>),
Union(UnionTypeDefinition<'a>),
Enum(EnumTypeDefinition<'a>),
InputObject(InputObjectTypeDefinition<'a>),
}
#[derive(Clone, Debug, PartialEq)]
pub struct ObjectTypeDefinition<'a> {
pub interfaces: Vec<&'a str>,
pub fields: Vec<FieldDefinition<'a>>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct InterfaceTypeDefinition<'a> {
pub interfaces: Vec<&'a str>,
pub fields: Vec<FieldDefinition<'a>>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct UnionTypeDefinition<'a> {
pub types: Vec<&'a str>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct EnumTypeDefinition<'a> {
pub values: Vec<EnumValueDefinition<'a>>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct InputObjectTypeDefinition<'a> {
pub fields: Vec<InputValueDefinition<'a>>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct FieldDefinition<'a> {
pub description: Option<Cow<'a, str>>,
pub name: &'a str,
pub arguments: Vec<InputValueDefinition<'a>>,
pub ty: Type<'a>,
pub directives: Vec<Directive<'a>>,
pub location: Location,
}
#[derive(Clone, Debug, PartialEq)]
pub struct InputValueDefinition<'a> {
pub description: Option<Cow<'a, str>>,
pub name: &'a str,
pub ty: Type<'a>,
pub default_value: Option<Value<'a>>,
pub directives: Vec<Directive<'a>>,
pub location: Location,
}
#[derive(Clone, Debug, PartialEq)]
pub struct EnumValueDefinition<'a> {
pub description: Option<Cow<'a, str>>,
pub name: &'a str,
pub directives: Vec<Directive<'a>>,
pub location: Location,
}
#[derive(Clone, Debug, PartialEq)]
pub struct DirectiveDefinition<'a> {
pub description: Option<Cow<'a, str>>,
pub name: &'a str,
pub arguments: Vec<InputValueDefinition<'a>>,
pub repeatable: bool,
pub directives: Vec<Directive<'a>>,
pub locations: Vec<DirectiveLocation>,
pub location: Location,
}
#[derive(Clone, Debug, PartialEq)]
pub struct SchemaExtension<'a> {
pub directives: Vec<Directive<'a>>,
pub operations: Vec<RootOperationTypeDefinition<'a>>,
pub location: Location,
}
#[derive(Clone, Debug, PartialEq)]
pub struct TypeExtension<'a> {
pub name: &'a str,
pub kind: TypeExtensionKind<'a>,
pub directives: Vec<Directive<'a>>,
pub location: Location,
}
#[derive(Clone, Debug, PartialEq)]
pub enum TypeExtensionKind<'a> {
Scalar,
Object(ObjectTypeDefinition<'a>),
Interface(InterfaceTypeDefinition<'a>),
Union(UnionTypeDefinition<'a>),
Enum(EnumTypeDefinition<'a>),
InputObject(InputObjectTypeDefinition<'a>),
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum DirectiveLocation {
Query,
Mutation,
Subscription,
Field,
FragmentDefinition,
FragmentSpread,
InlineFragment,
VariableDefinition,
Schema,
Scalar,
Object,
FieldDefinition,
DirectiveDefinition,
ArgumentDefinition,
Interface,
Union,
Enum,
EnumValue,
InputObject,
InputFieldDefinition,
}
impl DirectiveLocation {
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Self::Query => "QUERY",
Self::Mutation => "MUTATION",
Self::Subscription => "SUBSCRIPTION",
Self::Field => "FIELD",
Self::FragmentDefinition => "FRAGMENT_DEFINITION",
Self::FragmentSpread => "FRAGMENT_SPREAD",
Self::InlineFragment => "INLINE_FRAGMENT",
Self::VariableDefinition => "VARIABLE_DEFINITION",
Self::Schema => "SCHEMA",
Self::Scalar => "SCALAR",
Self::Object => "OBJECT",
Self::FieldDefinition => "FIELD_DEFINITION",
Self::DirectiveDefinition => "DIRECTIVE_DEFINITION",
Self::ArgumentDefinition => "ARGUMENT_DEFINITION",
Self::Interface => "INTERFACE",
Self::Union => "UNION",
Self::Enum => "ENUM",
Self::EnumValue => "ENUM_VALUE",
Self::InputObject => "INPUT_OBJECT",
Self::InputFieldDefinition => "INPUT_FIELD_DEFINITION",
}
}
#[must_use]
pub fn parse(value: &str) -> Option<Self> {
Some(match value {
"QUERY" => Self::Query,
"MUTATION" => Self::Mutation,
"SUBSCRIPTION" => Self::Subscription,
"FIELD" => Self::Field,
"FRAGMENT_DEFINITION" => Self::FragmentDefinition,
"FRAGMENT_SPREAD" => Self::FragmentSpread,
"INLINE_FRAGMENT" => Self::InlineFragment,
"VARIABLE_DEFINITION" => Self::VariableDefinition,
"SCHEMA" => Self::Schema,
"SCALAR" => Self::Scalar,
"OBJECT" => Self::Object,
"FIELD_DEFINITION" => Self::FieldDefinition,
"DIRECTIVE_DEFINITION" => Self::DirectiveDefinition,
"ARGUMENT_DEFINITION" => Self::ArgumentDefinition,
"INTERFACE" => Self::Interface,
"UNION" => Self::Union,
"ENUM" => Self::Enum,
"ENUM_VALUE" => Self::EnumValue,
"INPUT_OBJECT" => Self::InputObject,
"INPUT_FIELD_DEFINITION" => Self::InputFieldDefinition,
_ => return None,
})
}
}