graphql_toolkit_ast/
lib.rs

1//! A collection of Rust modules to process GraphQL documents
2//!
3//! This module contains the AST types and traits for the GraphQL query language.
4
5pub use graphql_toolkit_value::*;
6pub use pos::*;
7pub use types::*;
8
9mod pos;
10mod types;
11
12/// A value-to-AST conversion trait that consumes the input value.
13pub trait IntoAst<T> {
14    /// Convert this value into an AST type.
15    #[must_use]
16    fn into_ast(self) -> T;
17}
18
19macro_rules! impl_into_ast {
20    ($($ty:ty),* $(,)?) => {
21        $(
22            impl IntoAst<$ty> for $ty {
23                /// Returns the input value unchanged.
24                #[inline(always)]
25                fn into_ast(self) -> $ty {
26                    self
27                }
28            }
29        )*
30    };
31}
32
33impl<S> IntoAst<Name> for S
34where
35    S: AsRef<str>,
36{
37    #[inline]
38    fn into_ast(self) -> Name {
39        Name::new(self)
40    }
41}
42
43// Alphabetically sorted list of AST types
44impl_into_ast! {
45    BaseType, ConstValue, Directive, DocumentOperations, Field, FragmentDefinition, FragmentSpread,
46    InlineFragment, Number, OperationDefinition, OperationType, Selection, SelectionSet,
47    Type, TypeCondition, Value, VariableDefinition,
48}
49
50/// Extension trait for adding position information to AST nodes.
51pub trait AstPositionExt: Sized {
52    /// Create a positioned version of this AST node with the default position (0:0).
53    #[must_use]
54    #[inline]
55    fn default_position(self) -> Positioned<Self> {
56        Positioned::new(self, Default::default())
57    }
58
59    /// Create a positioned version of this AST node with the given position.
60    #[must_use]
61    #[inline]
62    fn with_position(self, pos: Pos) -> Positioned<Self> {
63        Positioned::new(self, pos)
64    }
65}
66
67macro_rules! impl_ast_position_ext {
68    ($($ty:ty),* $(,)?) => {
69        $(
70            impl AstPositionExt for $ty {}
71        )*
72    };
73}
74
75// Alphabetically sorted list of AST types
76impl_ast_position_ext! {
77    BaseType, ConstValue, Directive, DocumentOperations, Field, FragmentDefinition, FragmentSpread,
78    InlineFragment, Name, Number, OperationDefinition, OperationType, Selection, SelectionSet,
79    Type, TypeCondition, Value, VariableDefinition,
80}