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

A sketch of the Iterative visitor #31

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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 src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ mod tokenizer;
mod helpers;
pub mod query;
pub mod schema;
pub mod visitor;

pub use crate::query::parse_query;
pub use crate::schema::parse_schema;
Expand Down
154 changes: 154 additions & 0 deletions src/visitor.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
use std::slice;

use crate::common::Text;
use crate::query::{SelectionSet, Directive, Selection, Field};
use crate::query::{Document, Definition};


pub trait Visit {
fn visit<'x, D: 'x>(&'x self) -> <(&'x Self, &'x D) as VisitorData>::Data
where (&'x Self, &'x D): VisitorData,
<(&'x Self, &'x D) as VisitorData>::Data: CreateData<'x, &'x Self, &'x D>,
{
CreateData::new(self)
}
}

impl<S> Visit for S { }

pub trait VisitorData {
type Data;
}

#[derive(Debug)]
pub struct FieldIter<'a, T>
where T: Text<'a>
{
stack: Vec<slice::Iter<'a, Selection<'a, T>>>,
}

pub trait CreateData<'a, S: ?Sized, D: ?Sized> {
fn new(v: S) -> Self;
}

impl<'a, T> CreateData<'a, &'a SelectionSet<'a, T>, &'a Field<'a, T>>
for FieldIter<'a, T>
where T: Text<'a>,
{
fn new(v: &'a SelectionSet<'a, T>) -> Self {
FieldIter {
stack: vec![v.items.iter()],
}
}
}

impl<'a, T> VisitorData for (&'a SelectionSet<'a, T>, &'a Field<'a, T>)
where T: Text<'a>,
{
type Data = FieldIter<'a, T>;
}

impl<'a, T: 'a> Iterator for FieldIter<'a, T>
where T: Text<'a>,
{
type Item = &'a Field<'a, T>;
fn next(&mut self) -> Option<&'a Field<'a, T>> {
let ref mut stack = self.stack;
while !stack.is_empty() {
match stack.last_mut().and_then(|iter| iter.next()) {
Some(Selection::Field(f)) => {
stack.push(f.selection_set.items.iter());
return Some(f);
}
Some(Selection::InlineFragment(f)) => {
stack.push(f.selection_set.items.iter());
continue;
}
Some(Selection::FragmentSpread(..)) => {}
None => {
stack.pop();
}
}
}
return None;
}
}

#[derive(Debug)]
pub struct DocumentFieldIter<'a, T>
where T: Text<'a>
{
doc_iter: slice::Iter<'a, Definition<'a, T>>,
field_iter: Option<FieldIter<'a, T>>,
}

impl<'a, T> VisitorData for (&'a Document<'a, T>, &'a Field<'a, T>)
where T: Text<'a>,
{
type Data = DocumentFieldIter<'a, T>;
}

impl<'a, T> CreateData<'a, &'a Document<'a, T>, &'a Field<'a, T>>
for DocumentFieldIter<'a, T>
where T: Text<'a>,
{
fn new(v: &'a Document<'a, T>) -> Self {
Self {
doc_iter: v.definitions.iter(),
field_iter: None,
}
}
}

impl<'a, T: 'a> Iterator for DocumentFieldIter<'a, T>
where T: Text<'a>,
{
type Item = &'a Field<'a, T>;
fn next(&mut self) -> Option<&'a Field<'a, T>> {
use crate::query::Definition::*;
use crate::query::OperationDefinition::*;
loop {
if let Some(field_iter) = &mut self.field_iter {
if let Some(result) = field_iter.next() {
return Some(result);
}
}
self.field_iter.take();
let ss = match self.doc_iter.next() {
Some(Operation(SelectionSet(def))) => &def,
Some(Operation(Query(def))) => &def.selection_set,
Some(Operation(Mutation(def))) => &def.selection_set,
Some(Operation(Subscription(def))) => &def.selection_set,
Some(Fragment(def)) => &def.selection_set,
None => return None,
};
self.field_iter = Some(ss.visit::<Field<'a, T>>());
}
}
}

#[test]
fn test_field_iter() {
use crate::parse_query;

let doc = parse_query::<&str>(r#"
query TestQuery {
users {
id
country {
id
}
}
}
"#).expect("Failed to parse query");
let mut fields = 0;
let mut field_names = Vec::new();
for f in doc.visit::<Field<_>>() {
fields += 1;
field_names.push(f.name);
}
assert_eq!(fields, 4);
assert_eq!(field_names, vec!["users", "id", "country", "id"]);
}