Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Serde Support For AST #80

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@ jobs:

- name: Test Rust
run: cargo test

- name: Test Rust with serde feature
run: cargo test --all-features
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/Cargo.lock
/.vagga
/target
/.idea
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ edition = "2018"

[dependencies]
combine = "4.6.6"
serde = { version = "1.0.163", features = ["derive"], optional = true }
thiserror = "1.0.11"

[dev-dependencies]
Expand Down
28 changes: 28 additions & 0 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,35 @@ use std::{collections::BTreeMap, fmt};
use combine::easy::{Error, Info};
use combine::{choice, many, many1, optional, position, StdParseResult};
use combine::{parser, Parser};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

use crate::helpers::{ident, kind, name, punct};
use crate::position::Pos;
use crate::tokenizer::{Kind as T, Token, TokenStream};

#[cfg(feature = "serde")]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if this is a good idea, but I think to not duplicate everything you could do something like:

#[cfg(feature = "serde")]
mod text_traits {
    use serde::{Serialize, Deserialize};

    pub trait SerdeTraits<'a>: Serialize + Deserialize<'a> {}

    impl<'a, T> SerdeTraits<'a> for T where T: Serialize + Deserialize<'a> {}
}

#[cfg(not(feature = "serde"))]
mod text_traits {
    pub trait SerdeTraits<'a> {}

    impl<'a, T> SerdeTraits<'a> for T {}
}

// Define the main trait with conditional compilation
pub trait Text<'a>: 'a {
    type Value: 'a
        + From<&'a str>
        + AsRef<str>
        + std::borrow::Borrow<str>
        + PartialEq
        + Eq
        + PartialOrd
        + Ord
        + fmt::Debug
        + Clone
        + text_traits::SerdeTraits<'a>;
}

/// Text abstracts over types that hold a string value.
/// It is used to make the AST generic over the string type.
pub trait Text<'a>
where
Self: 'a,
{
type Value: 'a
+ From<&'a str>
+ AsRef<str>
+ std::borrow::Borrow<str>
+ PartialEq
+ Eq
+ PartialOrd
+ Ord
+ fmt::Debug
+ Clone
+ Serialize
+ Deserialize<'a>;
}

#[cfg(not(feature = "serde"))]
/// Text abstracts over types that hold a string value.
/// It is used to make the AST generic over the string type.
pub trait Text<'a>: 'a {
Expand Down Expand Up @@ -36,6 +60,7 @@ impl<'a> Text<'a> for std::borrow::Cow<'a, str> {
}

#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Directive<'a, T: Text<'a>> {
pub position: Pos,
pub name: T::Value,
Expand All @@ -49,11 +74,13 @@ pub struct Directive<'a, T: Text<'a>> {
/// in `serde_json`: encapsulate value in new-type, allowing type
/// to be extended later.
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
// we use i64 as a reference implementation: graphql-js thinks even 32bit
// integers is enough. We might consider lift this limit later though
pub struct Number(pub(crate) i64);

#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum Value<'a, T: Text<'a>> {
Variable(T::Value),
Int(Number),
Expand Down Expand Up @@ -87,6 +114,7 @@ impl<'a, T: Text<'a>> Value<'a, T> {
}

#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum Type<'a, T: Text<'a>> {
NamedType(T::Value),
ListType(Box<Type<'a, T>>),
Expand Down
12 changes: 7 additions & 5 deletions src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use std::default::Default;

use crate::common::Directive;


#[derive(Debug, PartialEq)]
pub(crate) struct Formatter<'a> {
buf: String,
Expand Down Expand Up @@ -171,13 +170,16 @@ impl<'a> Formatter<'a> {
}

fn dec_indent(&mut self) {
self.indent = self.indent.checked_sub(self.style.indent)
self.indent = self
.indent
.checked_sub(self.style.indent)
.expect("negative indent");
}
}

pub(crate) fn format_directives<'a, T>(dirs: &[Directive<'a, T>], f: &mut Formatter)
where T: crate::common::Text<'a>,
pub(crate) fn format_directives<'a, T>(dirs: &[Directive<'a, T>], f: &mut Formatter)
where
T: crate::common::Text<'a>,
{
for dir in dirs {
f.write(" ");
Expand All @@ -198,7 +200,7 @@ macro_rules! impl_display {

('a $($typ: ident, )+) => {
$(
impl<'a, T> fmt::Display for $typ<'a, T>
impl<'a, T> fmt::Display for $typ<'a, T>
where T: Text<'a>,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Expand Down
15 changes: 8 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,20 +91,21 @@
//!
#![warn(missing_debug_implementations)]

#[cfg(test)] #[macro_use] extern crate pretty_assertions;

#[cfg(test)]
#[macro_use]
extern crate pretty_assertions;

mod common;
#[macro_use]
mod format;
mod position;
mod tokenizer;
mod helpers;
mod position;
pub mod query;
pub mod schema;
mod tokenizer;

pub use crate::format::Style;
pub use crate::position::Pos;
pub use crate::query::minify_query;
pub use crate::query::parse_query;
pub use crate::schema::parse_schema;
pub use crate::query::minify_query;
pub use crate::position::Pos;
pub use crate::format::Style;
3 changes: 3 additions & 0 deletions src/position.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use std::fmt;

/// Original position of element in source code
#[derive(PartialOrd, Ord, PartialEq, Eq, Clone, Copy, Default, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Pos {
/// One-based line number
pub line: usize,
Expand Down
86 changes: 84 additions & 2 deletions src/query/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,28 @@
//!
//! [graphql grammar]: http://facebook.github.io/graphql/October2016/#sec-Appendix-Grammar-Summary
//!
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

pub use crate::common::{Directive, Number, Text, Type, Value};
use crate::position::Pos;
pub use crate::common::{Directive, Number, Value, Text, Type};

/// Root of query data
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(
feature = "serde",
serde(bound(deserialize = "'a: 'de, T: Deserialize<'de>"))
)]
pub struct Document<'a, T: Text<'a>> {
#[cfg_attr(feature = "serde", serde(borrow))]
pub definitions: Vec<Definition<'a, T>>,
}

impl<'a> Document<'a, String> {
pub fn into_static(self) -> Document<'static, String> {
// To support both reference and owned values in the AST,
// all string data is represented with the ::common::Str<'a, T: Text<'a>>
// all string data is represented with the ::common::Str<'a, T: Text<'a>>
// wrapper type.
// This type must carry the lifetime of the query string,
// and is stored in a PhantomData value on the Str type.
Expand All @@ -34,102 +43,175 @@ impl<'a> Document<'a, String> {
}

#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(
feature = "serde",
serde(bound(deserialize = "'a: 'de, T: Deserialize<'de>"))
)]
pub enum Definition<'a, T: Text<'a>> {
Operation(OperationDefinition<'a, T>),
#[cfg_attr(feature = "serde", serde(borrow))]
Fragment(FragmentDefinition<'a, T>),
}

#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(
feature = "serde",
serde(bound(deserialize = "'a: 'de, T: Deserialize<'de>"))
)]
pub struct FragmentDefinition<'a, T: Text<'a>> {
pub position: Pos,
pub name: T::Value,
#[cfg_attr(feature = "serde", serde(borrow))]
pub type_condition: TypeCondition<'a, T>,
pub directives: Vec<Directive<'a, T>>,
pub selection_set: SelectionSet<'a, T>,
}

#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(
feature = "serde",
serde(bound(deserialize = "'a: 'de, T: Deserialize<'de>"))
)]
pub enum OperationDefinition<'a, T: Text<'a>> {
SelectionSet(SelectionSet<'a, T>),
#[cfg_attr(feature = "serde", serde(borrow))]
Query(Query<'a, T>),
Mutation(Mutation<'a, T>),
Subscription(Subscription<'a, T>),
}

#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(
feature = "serde",
serde(bound(deserialize = "'a: 'de, T: Deserialize<'de>"))
)]
pub struct Query<'a, T: Text<'a>> {
pub position: Pos,
pub name: Option<T::Value>,
#[cfg_attr(feature = "serde", serde(borrow))]
pub variable_definitions: Vec<VariableDefinition<'a, T>>,
pub directives: Vec<Directive<'a, T>>,
pub selection_set: SelectionSet<'a, T>,
}

#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(
feature = "serde",
serde(bound(deserialize = "'a: 'de, T: Deserialize<'de>"))
)]
pub struct Mutation<'a, T: Text<'a>> {
pub position: Pos,
pub name: Option<T::Value>,
#[cfg_attr(feature = "serde", serde(borrow))]
pub variable_definitions: Vec<VariableDefinition<'a, T>>,
pub directives: Vec<Directive<'a, T>>,
pub selection_set: SelectionSet<'a, T>,
}

#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(
feature = "serde",
serde(bound(deserialize = "'a: 'de, T: Deserialize<'de>"))
)]
pub struct Subscription<'a, T: Text<'a>> {
pub position: Pos,
pub name: Option<T::Value>,
#[cfg_attr(feature = "serde", serde(borrow))]
pub variable_definitions: Vec<VariableDefinition<'a, T>>,
pub directives: Vec<Directive<'a, T>>,
pub selection_set: SelectionSet<'a, T>,
}

#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(
feature = "serde",
serde(bound(deserialize = "'a: 'de, T: Deserialize<'de>"))
)]
pub struct SelectionSet<'a, T: Text<'a>> {
pub span: (Pos, Pos),
#[cfg_attr(feature = "serde", serde(borrow))]
pub items: Vec<Selection<'a, T>>,
}

#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(
feature = "serde",
serde(bound(deserialize = "'a: 'de, T: Deserialize<'de>"))
)]
pub struct VariableDefinition<'a, T: Text<'a>> {
pub position: Pos,
pub name: T::Value,
#[cfg_attr(feature = "serde", serde(borrow))]
pub var_type: Type<'a, T>,
pub default_value: Option<Value<'a, T>>,
}

#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(
feature = "serde",
serde(bound(deserialize = "'a: 'de, T: Deserialize<'de>"))
)]
pub enum Selection<'a, T: Text<'a>> {
#[cfg_attr(feature = "serde", serde(borrow))]
Field(Field<'a, T>),
FragmentSpread(FragmentSpread<'a, T>),
InlineFragment(InlineFragment<'a, T>),
}

#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(
feature = "serde",
serde(bound(deserialize = "'a: 'de, T: Deserialize<'de>"))
)]
pub struct Field<'a, T: Text<'a>> {
pub position: Pos,
pub alias: Option<T::Value>,
pub name: T::Value,
#[cfg_attr(feature = "serde", serde(borrow))]
pub arguments: Vec<(T::Value, Value<'a, T>)>,
pub directives: Vec<Directive<'a, T>>,
pub selection_set: SelectionSet<'a, T>,
}

#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(
feature = "serde",
serde(bound(deserialize = "'a: 'de, T: Deserialize<'de>"))
)]
pub struct FragmentSpread<'a, T: Text<'a>> {
pub position: Pos,
pub fragment_name: T::Value,
#[cfg_attr(feature = "serde", serde(borrow))]
pub directives: Vec<Directive<'a, T>>,
}

#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum TypeCondition<'a, T: Text<'a>> {
On(T::Value),
}

#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(
feature = "serde",
serde(bound(deserialize = "'a: 'de, T: Deserialize<'de>"))
)]
pub struct InlineFragment<'a, T: Text<'a>> {
pub position: Pos,
pub type_condition: Option<TypeCondition<'a, T>>,
#[cfg_attr(feature = "serde", serde(borrow))]
pub directives: Vec<Directive<'a, T>>,
pub selection_set: SelectionSet<'a, T>,
}
3 changes: 1 addition & 2 deletions src/query/error.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use combine::easy::Errors;
use thiserror::Error;

use crate::tokenizer::Token;
use crate::position::Pos;
use crate::tokenizer::Token;

pub type InternalError<'a> = Errors<Token<'a>, Token<'a>, Pos>;


/// Error parsing query
///
/// This structure is opaque for forward compatibility. We are exploring a
Expand Down
4 changes: 2 additions & 2 deletions src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ mod format;
mod grammar;
mod minify;

pub use self::grammar::{parse_query, consume_definition};
pub use self::error::ParseError;
pub use self::ast::*;
pub use self::error::ParseError;
pub use self::grammar::{consume_definition, parse_query};
pub use self::minify::minify_query;
Loading
Loading