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 #77

Closed
wants to merge 7 commits into from
Closed
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
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
20 changes: 20 additions & 0 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ 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::Serialize;

use crate::helpers::{ident, kind, name, punct};
use crate::position::Pos;
Expand All @@ -11,6 +13,20 @@ use crate::tokenizer::{Kind as T, Token, TokenStream};
/// 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 {
#[cfg(feature = "serde")]
type Value: 'a
+ From<&'a str>
+ AsRef<str>
+ std::borrow::Borrow<str>
+ PartialEq
+ Eq
+ PartialOrd
+ Ord
+ fmt::Debug
+ Clone
+ Serialize;

#[cfg(not(feature = "serde"))]
type Value: 'a
+ From<&'a str>
+ AsRef<str>
Expand All @@ -36,6 +52,7 @@ impl<'a> Text<'a> for std::borrow::Cow<'a, str> {
}

#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub struct Directive<'a, T: Text<'a>> {
pub position: Pos,
pub name: T::Value,
Expand All @@ -49,11 +66,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))]
// 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))]
pub enum Value<'a, T: Text<'a>> {
Variable(T::Value),
Int(Number),
Expand Down Expand Up @@ -87,6 +106,7 @@ impl<'a, T: Text<'a>> Value<'a, T> {
}

#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize))]
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::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))]
pub struct Pos {
/// One-based line number
pub line: usize,
Expand Down
21 changes: 19 additions & 2 deletions src/query/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,23 @@
//!
//! [graphql grammar]: http://facebook.github.io/graphql/October2016/#sec-Appendix-Grammar-Summary
//!
#[cfg(feature = "serde")]
use serde::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))]
pub struct Document<'a, T: Text<'a>> {
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,12 +38,14 @@ impl<'a> Document<'a, String> {
}

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

#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub struct FragmentDefinition<'a, T: Text<'a>> {
pub position: Pos,
pub name: T::Value,
Expand All @@ -49,6 +55,7 @@ pub struct FragmentDefinition<'a, T: Text<'a>> {
}

#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub enum OperationDefinition<'a, T: Text<'a>> {
SelectionSet(SelectionSet<'a, T>),
Query(Query<'a, T>),
Expand All @@ -57,6 +64,7 @@ pub enum OperationDefinition<'a, T: Text<'a>> {
}

#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub struct Query<'a, T: Text<'a>> {
pub position: Pos,
pub name: Option<T::Value>,
Expand All @@ -66,6 +74,7 @@ pub struct Query<'a, T: Text<'a>> {
}

#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub struct Mutation<'a, T: Text<'a>> {
pub position: Pos,
pub name: Option<T::Value>,
Expand All @@ -75,6 +84,7 @@ pub struct Mutation<'a, T: Text<'a>> {
}

#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub struct Subscription<'a, T: Text<'a>> {
pub position: Pos,
pub name: Option<T::Value>,
Expand All @@ -84,12 +94,14 @@ pub struct Subscription<'a, T: Text<'a>> {
}

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

#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub struct VariableDefinition<'a, T: Text<'a>> {
pub position: Pos,
pub name: T::Value,
Expand All @@ -98,13 +110,15 @@ pub struct VariableDefinition<'a, T: Text<'a>> {
}

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

#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub struct Field<'a, T: Text<'a>> {
pub position: Pos,
pub alias: Option<T::Value>,
Expand All @@ -115,18 +129,21 @@ pub struct Field<'a, T: Text<'a>> {
}

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

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

#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub struct InlineFragment<'a, T: Text<'a>> {
pub position: Pos,
pub type_condition: Option<TypeCondition<'a, T>>,
Expand Down
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