-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathvisitor.rs
More file actions
154 lines (136 loc) · 3.95 KB
/
visitor.rs
File metadata and controls
154 lines (136 loc) · 3.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
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"]);
}