-
Notifications
You must be signed in to change notification settings - Fork 0
/
ops.rs
1754 lines (1462 loc) · 47.8 KB
/
ops.rs
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
use std::{vec, iter};
use entry::{self,Entry,IterPosition};
use db::{Document, Doctype, ItemIterator, Input, Pipeline, Repeater};
use prop::Property;
pub struct Identity<'a, D> where D: Document {
input: Input<'a, D>,
}
impl<'a, D> Identity<'a, D> where D: Document {
pub fn new(input: Input<D>) -> Identity<D> {
Identity{input}
}
}
impl<'a, D> Repeater<'a,D> for Identity<'a, D> where D: 'a + Document {
fn repeat(&self) -> Input<'a,D> {
Box::new(Identity{input: self.input.repeat()})
}
}
impl<'a, D> Iterator for Identity<'a, D> where D: Document {
type Item=Entry<D>;
fn next(&mut self) -> Option<Entry<D>> {
Some(self.input.next()?)
}
}
impl<'a, D> Pipeline<'a,D> for Identity<'a, D> where D: 'a + Document {
}
pub struct Recurse<'a, D> where D: Document {
input: Input<'a,D>,
iter: Option<vec::IntoIter<Entry<D>>>,
}
impl<'a,D> Recurse<'a,D> where D: Document {
pub fn new(input: Input<D>) -> Recurse<D> {
Recurse{input, iter: None}
}
fn values(&mut self) -> Option<Vec<Entry<D>>> {
let d_entry = self.input.next()?;
if d_entry.has_error() { return Some(vec![d_entry]) }
let mut entries = Vec::new();
for value in d_entry.doc.clone().recurse() {
let mut entry = d_entry.clone();
entry.doc = value;
entries.push(entry)
}
Some(entry::fixpositions(entries))
}
fn next_value(&mut self) -> Option<Entry<D>> {
let entry = loop {
match self.iter.as_mut().unwrap().next() {
Some(entry) => break entry,
None => { self.iter = Some(self.values()?.into_iter()); },
}
};
Some(entry)
}
}
impl<'a,D> Repeater<'a,D> for Recurse<'a,D> where D: 'a + Document {
fn repeat(&self) -> Input<'a,D> {
if let Some(_) = self.iter {
panic!("cannot repeat Recurse after the statement is prepared");
}
let x = Recurse{ input: self.input.repeat(), iter: None };
Box::new(x)
}
}
impl<'a,D> Iterator for Recurse<'a,D> where D: Document {
type Item=Entry<D>;
fn next(&mut self) -> Option<Entry<D>> {
if self.iter.is_none() {
self.iter = Some(self.values()?.into_iter())
}
self.next_value().map(|entry| entry)
}
}
impl<'a,D> Pipeline<'a,D> for Recurse<'a,D> where D: 'a + Document {
}
pub struct Null<'a,D> where D: Document {
input: Input<'a,D>,
}
impl<'a,D> Null<'a,D> where D: Document {
pub fn new(input: Input<D>) -> Null<D> {
Null{input}
}
}
impl<'a,D> Repeater<'a,D> for Null<'a,D> where D: 'a + Document {
fn repeat(&self) -> Input<'a,D> {
let x = Null{input: self.input.repeat()};
Box::new(x)
}
}
impl<'a,D> Iterator for Null<'a,D> where D: Document {
type Item=Entry<D>;
fn next(&mut self) -> Option<Entry<D>> {
self.input.next()?; // ignore any meta information, errors from input.
Some(Entry::new(D::null()))
}
}
impl<'a,D> Pipeline<'a,D> for Null<'a,D> where D: 'a + Document {
}
pub struct Bool<'a,D> where D: Document {
input: Input<'a,D>,
value: D,
}
impl<'a,D> Bool<'a,D> where D: Document {
pub fn new(input: Input<D>, value: bool) -> Bool<D> {
Bool{input, value: From::from(value)}
}
}
impl<'a,D> Repeater<'a,D> for Bool<'a,D> where D: 'a + Document {
fn repeat(&self) -> Input<'a,D> {
let x = Bool{input: self.input.repeat(), value: self.value.clone()};
Box::new(x)
}
}
impl<'a,D> Iterator for Bool<'a,D> where D: Document {
type Item=Entry<D>;
fn next(&mut self) -> Option<Entry<D>> {
self.input.next()?; // ignore any meta information, errors from input.
Some(Entry::new(self.value.clone()))
}
}
impl<'a,D> Pipeline<'a,D> for Bool<'a,D> where D: 'a + Document {
}
pub struct Integer<'a,D> where D: Document {
input: Input<'a,D>,
value: D,
}
impl<'a,D> Integer<'a,D> where D: Document {
pub fn new(input: Input<D>, value: i128) -> Integer<D> {
Integer{input, value: From::from(value)}
}
}
impl<'a,D> Repeater<'a,D> for Integer<'a,D> where D: 'a + Document {
fn repeat(&self) -> Input<'a,D> {
let x = Integer{input: self.input.repeat(), value: self.value.clone()};
Box::new(x)
}
}
impl<'a,D> Iterator for Integer<'a,D> where D: Document {
type Item=Entry<D>;
fn next(&mut self) -> Option<Entry<D>> {
self.input.next()?; // ignore any meta information, errors from input.
Some(Entry::new(self.value.clone()))
}
}
impl<'a,D> Pipeline<'a,D> for Integer<'a,D> where D: 'a + Document {
}
pub struct Float<'a,D> where D: Document {
input: Input<'a,D>,
value: D,
}
impl<'a,D> Float<'a,D> where D: Document {
pub fn new(input: Input<D>, value: f64) -> Float<D> {
Float{input, value: From::from(value)}
}
}
impl<'a,D> Repeater<'a,D> for Float<'a,D> where D: 'a + Document {
fn repeat(&self) -> Input<'a,D> {
let x = Float{input: self.input.repeat(), value: self.value.clone()};
Box::new(x)
}
}
impl<'a,D> Iterator for Float<'a,D> where D: Document {
type Item=Entry<D>;
fn next(&mut self) -> Option<Entry<D>> {
self.input.next()?; // ignore any meta information, errors from input.
Some(Entry::new(self.value.clone()))
}
}
impl<'a,D> Pipeline<'a,D> for Float<'a,D> where D: 'a + Document {
}
pub struct StringLit<'a,D> where D: Document {
input: Input<'a,D>,
value: D,
}
impl<'a,D> StringLit<'a,D> where D: Document {
pub fn new(input: Input<D>, value: String) -> StringLit<D> {
StringLit{input, value: From::from(value)}
}
}
impl<'a,D> Repeater<'a,D> for StringLit<'a,D> where D: 'a + Document {
fn repeat(&self) -> Input<'a,D> {
let x = StringLit{
input: self.input.repeat(), value: self.value.clone()
};
Box::new(x)
}
}
impl<'a,D> Iterator for StringLit<'a,D> where D: Document {
type Item=Entry<D>;
fn next(&mut self) -> Option<Entry<D>> {
self.input.next()?; // ignore any meta information, errors from input.
Some(Entry::new(self.value.clone()))
}
}
impl<'a,D> Pipeline<'a,D> for StringLit<'a,D> where D: 'a + Document {
}
pub struct Index<'a,D> where D: Document {
input: Input<'a,D>,
key: Option<String>,
off: Option<isize>,
}
impl<'a,D> Index<'a,D> where D: Document {
pub fn new(input: Input<D>, key: Option<String>, off: Option<isize>)
-> Index<D>
{
Index{input, key, off}
}
fn do_get_shortcut(&self, doc: D) -> Result<D,String> {
let (key, dt) = (self.key.as_ref().unwrap(), doc.doctype());
if let Some(val) = doc.get(key) {
Ok(val)
} else {
Err(format!("cannot index {} into {:?}", key, dt))
}
}
fn do_index_shortcut(&self, doc: D) -> Result<D,String> {
let (off, dt) = (self.off.unwrap(), doc.doctype());
if let Some(val) = doc.index(off) {
Ok(val)
} else {
Err(format!("cannot index {} into {:?}", off, dt))
}
}
}
impl<'a,D> Repeater<'a,D> for Index<'a,D> where D: 'a + Document {
fn repeat(&self) -> Input<'a,D> {
let (key, off) = (self.key.clone(), self.off.clone());
let x = Index{input: self.input.repeat(), key, off};
Box::new(x)
}
}
impl<'a,D> Iterator for Index<'a,D> where D: Document {
type Item=Entry<D>;
fn next(&mut self) -> Option<Entry<D>> {
let mut d_entry = self.input.next()?;
if d_entry.has_error() { return Some(d_entry) }
let res = if let Some(_) = self.key {
self.do_get_shortcut(d_entry.doc)
} else if let Some(_) = self.off {
self.do_index_shortcut(d_entry.doc)
} else {
unreachable!()
};
let d_entry = match res {
Ok(doc) => {d_entry.doc = doc; d_entry},
Err(s) => {d_entry.doc = D::null(); d_entry.set_error(s); d_entry},
};
Some(d_entry)
}
}
impl<'a,D> Pipeline<'a,D> for Index<'a,D> where D: 'a + Document {
}
pub struct Identifier<'a,D> where D: Document {
input: Input<'a,D>,
symbol: String,
index: Option<isize>,
}
impl<'a,D> Identifier<'a,D> where D: Document {
pub fn new(input: Input<D>, symbol: String) -> Identifier<D> {
let index = symbol.parse().ok();
Identifier{input, symbol, index}
}
fn do_lookup(&self, doc: D) -> Result<D,String> {
let dt = doc.doctype();
if let Some(val) = doc.get_ref(&self.symbol) {
return Ok(val.clone())
} else if let Some(off) = self.index {
if let Some(val) = doc.index_ref(off) { return Ok(val.clone()) }
}
Err(format!("cannot index {} into {:?}", self.symbol, dt))
}
}
impl<'a,D> Repeater<'a,D> for Identifier<'a,D> where D: 'a + Document {
fn repeat(&self) -> Input<'a,D> {
let input = self.input.repeat();
let (symbol, index) = (self.symbol.clone(), self.index.clone());
let x = Identifier{input, symbol, index};
Box::new(x)
}
}
impl<'a,D> Iterator for Identifier<'a,D> where D: Document {
type Item=Entry<D>;
fn next(&mut self) -> Option<Entry<D>> {
let mut d_entry = self.input.next()?;
if d_entry.has_error() { return Some(d_entry) }
match self.do_lookup(d_entry.doc) {
Ok(doc) => {d_entry.doc = doc},
Err(s) => {d_entry.doc = D::null(); d_entry.set_error(s)},
}
Some(d_entry)
}
}
impl<'a,D> Pipeline<'a,D> for Identifier<'a,D> where D: 'a + Document {
}
pub struct Slice<'a,D> where D: Document {
input: Input<'a,D>,
start: isize,
end: isize,
}
impl<'a,D> Slice<'a,D> where D: Document {
pub fn new(input: Input<D>, start: isize, end: isize) -> Slice<D> {
Slice{input, start, end}
}
}
impl<'a,D> Repeater<'a,D> for Slice<'a,D> where D: 'a + Document {
fn repeat(&self) -> Input<'a,D> {
let (start, end) = (self.start, self.end);
let x = Slice{ input: self.input.repeat(), start, end };
Box::new(x)
}
}
impl<'a,D> Iterator for Slice<'a,D> where D: Document {
type Item=Entry<D>;
fn next(&mut self) -> Option<Entry<D>> {
let mut d_entry = self.input.next()?;
if d_entry.has_error() { return Some(d_entry) }
let dt = d_entry.doc.doctype();
let d_entry = match d_entry.doc.clone().slice(self.start, self.end) {
Some(doc) => { d_entry.doc = doc; d_entry },
None => {
d_entry.set_error(format!("cannot slice {:?}", dt));
d_entry
},
};
Some(d_entry)
}
}
impl<'a,D> Pipeline<'a,D> for Slice<'a,D> where D: 'a + Document {
}
pub struct IterValues<'a,D> where D: Document {
input: Input<'a,D>,
iter: Option<vec::IntoIter<Entry<D>>>,
}
impl<'a,D> IterValues<'a,D> where D: Document {
pub fn new(input: Input<D>) -> IterValues<D> {
IterValues{input, iter: None}
}
fn iter_values(&mut self) -> Option<Vec<Entry<D>>> {
let mut d_entry = self.input.next()?;
if d_entry.has_error() { return Some(vec![d_entry]) }
let dt = d_entry.doc.doctype();
let entries = match docvalues(d_entry.doc.clone()) {
Some(values) => {
values.into_iter()
.map(|doc| Entry::new_with_meta(d_entry.meta.clone(), doc))
.collect()
},
None => {
d_entry.set_error(format!("cannot iterate {:?}", dt));
vec![d_entry]
},
};
Some(entry::fixpositions(entries))
}
}
impl<'a,D> Repeater<'a,D> for IterValues<'a,D> where D: 'a + Document {
fn repeat(&self) -> Input<'a,D> {
if let Some(_) = self.iter {
panic!("cannot repeat IterValues after the statement is prepared");
}
let x = IterValues{ input: self.input.repeat(), iter: None };
Box::new(x)
}
}
impl<'a,D> Iterator for IterValues<'a,D> where D: Document {
type Item=Entry<D>;
fn next(&mut self) -> Option<Entry<D>> {
if self.iter.is_none() {
let entries = self.iter_values()?;
self.iter = Some(entries.into_iter());
}
let d_entry = loop {
match self.iter.as_mut().unwrap().next() {
Some(entry) => break entry,
None => {
let entries = self.iter_values()?;
self.iter = Some(entries.into_iter());
},
}
};
Some(d_entry)
}
}
impl<'a,D> Pipeline<'a,D> for IterValues<'a,D> where D: 'a + Document {
}
pub struct Iter<'a,D> where D: Document {
inputs: Vec<Input<'a,D>>,
done: bool,
iter: Option<vec::IntoIter<Entry<D>>>,
}
impl<'a,D> Iter<'a,D> where D: Document {
pub fn new(inputs: Vec<Input<D>>) -> Iter<D> {
Iter{inputs, done: false, iter: None}
}
fn iter_entries(&mut self) -> Option<Vec<Entry<D>>> {
let mut entries = Vec::new();
let mut n = self.inputs.len();
for input in self.inputs.iter_mut() {
match input.next() {
Some(entry) => entries.push(entry),
None => { n -= 1; entries.push(Entry::new(D::null())) },
}
}
if n > 0 { Some(entry::fixpositions(entries)) } else { None }
}
}
impl<'a,D> Repeater<'a,D> for Iter<'a,D> where D: 'a + Document {
fn repeat(&self) -> Input<'a,D> {
if let Some(_) = self.iter {
panic!("cannot repeat Iter after the statement is prepared");
}
let (done, iter) = (false, None);
let inputs = self.inputs.iter().map(|input| input.repeat()).collect();
Box::new(Iter{ inputs, done, iter })
}
}
impl<'a,D> Iterator for Iter<'a,D> where D: Document {
type Item=Entry<D>;
fn next(&mut self) -> Option<Entry<D>> {
if self.done { return None }
if self.iter.is_none() {
let entries = self.iter_entries()?;
self.iter = Some(entries.into_iter());
}
let d_entry = loop {
match self.iter.as_mut().unwrap().next() {
Some(entry) => break entry,
None => {
let entries = self.iter_entries()?;
self.iter = Some(entries.into_iter());
},
}
};
Some(d_entry)
}
}
impl<'a,D> Pipeline<'a,D> for Iter<'a,D> where D: 'a + Document {
}
pub struct List<'a,D> where D: Document {
inputs: Vec<Input<'a,D>>,
}
impl<'a,D> List<'a,D> where D: Document {
pub fn new(inputs: Vec<Input<D>>) -> List<D> {
List{inputs}
}
fn next_entries(&mut self) -> Option<Vec<Entry<D>>> {
let mut entries: Vec<Entry<D>> = Vec::new();
let mut n = self.inputs.len();
for input in self.inputs.iter_mut() {
loop {
match input.next() {
Some(entry) => match entry.iter_position() {
IterPosition::Next => entries.push(entry),
IterPosition::Item | IterPosition::End => {
entries.push(entry);
break
},
},
None => {
n -= 1;
entries.push(Entry::new(D::null()));
break
},
}
}
}
if n > 0 { Some(entries) } else { None }
}
}
impl<'a,D> Repeater<'a,D> for List<'a,D> where D: 'a + Document {
fn repeat(&self) -> Input<'a,D> {
let inputs = self.inputs.iter().map(|input| input.repeat()).collect();
Box::new(List{ inputs })
}
}
impl<'a,D> Iterator for List<'a,D> where D: Document {
type Item=Entry<D>;
fn next(&mut self) -> Option<Entry<D>> {
let entries = self.next_entries()?;
let values: Vec<D> = entries.iter().map(|e| e.doc.clone()).collect();
let d_entry = Entry::new_merged(entries, From::from(values));
Some(d_entry)
}
}
impl<'a,D> Pipeline<'a,D> for List<'a,D> where D: 'a + Document {
}
pub struct Dict<'a,D> where D: Document {
inputs: Vec<(Input<'a,D>, Input<'a,D>)>,
iter: Option<vec::IntoIter<Entry<D>>>,
}
impl<'a,D> Dict<'a,D> where D: Document {
pub fn new(inputs: Vec<(Input<'a,D>,Input<'a,D>)>) -> Dict<'a,D> {
Dict{inputs, iter: None}
}
fn collect_prop(kinput: &mut Input<D>, vinput: &mut Input<D>)
-> Option<Vec<(Entry<D>, Vec<Entry<D>>)>>
{
// gather keys
let mut keys = Vec::new();
loop {
match kinput.next() {
Some(entry) => match entry.iter_position() {
IterPosition::Next => keys.push(entry),
IterPosition::Item | IterPosition::End => {
keys.push(entry);
break
},
},
None => break,
}
}
if keys.len() == 0 { return None }
// gather values
let mut values = Vec::new();
loop {
match vinput.next() {
Some(entry) => match entry.iter_position() {
IterPosition::Next => values.push(entry),
IterPosition::Item | IterPosition::End => {
values.push(entry);
break
},
},
None => break,
}
}
if values.len() == 0 { return None }
// final
let mut props = Vec::new();
for key in keys.into_iter() {
props.push((key, values.clone()))
}
Some(props)
}
fn collect_props(&mut self) -> Option<Vec<(Entry<D>, Vec<Entry<D>>)>> {
let mut props = Vec::new();
let mut n = self.inputs.len();
for (kinput, vinput) in self.inputs.iter_mut() {
match Self::collect_prop(kinput, vinput) {
Some(mut sub_props) => props.append(&mut sub_props),
None => { n -= 1; },
}
}
if n > 0 { Some(props) } else { None }
}
fn collect_dict(mut bp: Vec<(Entry<D>, Vec<Entry<D>>)>)
-> Vec<Vec<(Entry<D>, Entry<D>)>>
{
let (key, values) = bp.remove(0);
if bp.len() == 0 {
return values.into_iter().map(|v| vec![(key.clone(), v)]).collect();
}
let tail = Self::collect_dict(bp);
let mut dict_lists: Vec<Vec<Vec<(Entry<D>, Entry<D>)>>> =
iter::repeat(tail).take(values.len()).collect();
for (i, value) in values.into_iter().enumerate() {
for dict in dict_lists[i].iter_mut() {
dict.push((key.clone(), value.clone()))
}
}
dict_lists.into_iter().flatten().collect()
}
fn next_props(&mut self) -> Option<Vec<Entry<D>>> {
let bp = self.collect_props()?;
let mut full = Vec::new();
for dict in Self::collect_dict(bp).into_iter() {
let mut entries: Vec<Entry<D>> = Vec::new();
let mut keys: Vec<String> = Vec::new();
let mut values: Vec<D> = Vec::new();
for (kentry, ventry) in dict {
entries.push(kentry.clone());
entries.push(ventry.clone());
if kentry.has_error() || ventry.has_error() { continue }
match kentry.doc.string() {
Some(s) => { keys.push(s); values.push(ventry.doc) },
None => (),
}
}
let props: Vec<Property<D>> = keys.into_iter().zip(values)
.map(|(k,v)| Property::new(k,v)).collect();
full.push(Entry::new_merged(entries, From::from(props)));
}
Some(full)
}
}
impl<'a,D> Repeater<'a,D> for Dict<'a,D> where D: 'a + Document {
fn repeat(&self) -> Input<'a,D> {
if let Some(_) = self.iter {
panic!("cannot repeat Dict after the statement is prepared");
}
let inputs = self.inputs.iter().map(|(ki,vi)| {
(ki.repeat(), vi.repeat())
}).collect();
Box::new(Dict{ inputs, iter: None })
}
}
impl<'a,D> Iterator for Dict<'a,D> where D: Document {
type Item=Entry<D>;
fn next(&mut self) -> Option<Entry<D>> {
if let None = self.iter {
self.iter = Some(self.next_props()?.into_iter());
}
let entry = loop {
match self.iter.as_mut().unwrap().next() {
Some(entry) => break entry,
None => { self.iter = Some(self.next_props()?.into_iter()); },
}
};
Some(entry)
}
}
impl<'a,D> Pipeline<'a,D> for Dict<'a,D> where D: 'a + Document {
}
pub struct Neg<'a,D> where D: Document {
input: Input<'a,D>,
}
impl<'a,D> Neg<'a,D> where D: Document {
pub fn new(input: Input<D>) -> Neg<D> {
Neg{input}
}
}
impl<'a,D> Repeater<'a,D> for Neg<'a,D> where D: 'a + Document {
fn repeat(&self) -> Input<'a,D> {
let x = Neg{input: self.input.repeat()};
Box::new(x)
}
}
impl<'a,D> Iterator for Neg<'a,D> where D: Document {
type Item=Entry<D>;
fn next(&mut self) -> Option<Entry<D>> {
let mut d_entry = self.input.next()?;
let doc = d_entry.doc;
d_entry.doc = -doc;
Some(d_entry)
}
}
impl<'a,D> Pipeline<'a,D> for Neg<'a,D> where D: 'a + Document {
}
pub struct Not<'a,D> where D: Document {
input: Input<'a,D>,
}
impl<'a,D> Not<'a,D> where D: Document {
pub fn new(input: Input<D>) -> Not<D> {
Not{input}
}
}
impl<'a,D> Repeater<'a,D> for Not<'a,D> where D: 'a + Document {
fn repeat(&self) -> Input<'a,D> {
let x = Not{input: self.input.repeat()};
Box::new(x)
}
}
impl<'a,D> Iterator for Not<'a,D> where D: Document {
type Item=Entry<D>;
fn next(&mut self) -> Option<Entry<D>> {
let mut d_entry = self.input.next()?;
let doc = d_entry.doc;
d_entry.doc = !doc;
Some(d_entry)
}
}
impl<'a,D> Pipeline<'a,D> for Not<'a,D> where D: 'a + Document {
}
pub struct Mul<'a,D> where D: Document {
lhs: Input<'a,D>,
rhs: Input<'a,D>,
}
impl<'a,D> Mul<'a,D> where D: Document {
pub fn new(lhs: Input<'a,D>, rhs: Input<'a,D>) -> Mul<'a,D> {
Mul{lhs, rhs}
}
}
impl<'a,D> Repeater<'a,D> for Mul<'a,D> where D: 'a + Document {
fn repeat(&self) -> Input<'a,D> {
let x = Mul{ lhs: self.lhs.repeat(), rhs: self.rhs.repeat() };
Box::new(x)
}
}
impl<'a,D> Iterator for Mul<'a,D> where D: Document {
type Item=Entry<D>;
fn next(&mut self) -> Option<Entry<D>> {
let d_lhs = self.lhs.next()?;
let d_rhs = self.rhs.next()?;
let doc = d_lhs.doc.clone() * d_rhs.doc.clone();
let entries = vec![d_lhs, d_rhs];
Some(Entry::new_merged(entries, doc))
}
}
impl<'a,D> Pipeline<'a,D> for Mul<'a,D> where D: 'a + Document {
}
pub struct Div<'a,D> where D: Document {
lhs: Input<'a,D>,
rhs: Input<'a,D>,
}
impl<'a,D> Div<'a,D> where D: Document {
pub fn new(lhs: Input<'a,D>, rhs: Input<'a,D>) -> Div<'a,D> {
Div{lhs, rhs}
}
}
impl<'a,D> Repeater<'a,D> for Div<'a,D> where D: 'a + Document {
fn repeat(&self) -> Input<'a,D> {
let x = Div{ lhs: self.lhs.repeat(), rhs: self.rhs.repeat() };
Box::new(x)
}
}
impl<'a,D> Iterator for Div<'a,D> where D: Document {
type Item=Entry<D>;
fn next(&mut self) -> Option<Entry<D>> {
let d_lhs = self.lhs.next()?;
let d_rhs = self.rhs.next()?;
let doc = d_lhs.doc.clone() / d_rhs.doc.clone();
let entries = vec![d_lhs, d_rhs];
Some(Entry::new_merged(entries, doc))
}
}
impl<'a,D> Pipeline<'a,D> for Div<'a,D> where D: 'a + Document {
}
pub struct Rem<'a,D> where D: Document {
lhs: Input<'a,D>,
rhs: Input<'a,D>,
}
impl<'a,D> Rem<'a,D> where D: Document {
pub fn new(lhs: Input<'a,D>, rhs: Input<'a,D>) -> Rem<'a,D> {
Rem{lhs, rhs}
}
}
impl<'a,D> Repeater<'a,D> for Rem<'a,D> where D: 'a + Document {
fn repeat(&self) -> Input<'a,D> {
let x = Rem{ lhs: self.lhs.repeat(), rhs: self.rhs.repeat() };
Box::new(x)
}
}
impl<'a,D> Iterator for Rem<'a,D> where D: Document {
type Item=Entry<D>;
fn next(&mut self) -> Option<Entry<D>> {
let d_lhs = self.lhs.next()?;
let d_rhs = self.rhs.next()?;
let doc = d_lhs.doc.clone() % d_rhs.doc.clone();
let entries = vec![d_lhs, d_rhs];
Some(Entry::new_merged(entries, doc))
}
}
impl<'a,D> Pipeline<'a,D> for Rem<'a,D> where D: 'a + Document {
}
pub struct Add<'a,D> where D: Document {
lhs: Input<'a,D>,
rhs: Input<'a,D>,
}
impl<'a,D> Add<'a,D> where D: Document {
pub fn new(lhs: Input<'a,D>, rhs: Input<'a,D>) -> Add<'a,D> {
Add{lhs, rhs}
}
}
impl<'a,D> Repeater<'a,D> for Add<'a,D> where D: 'a + Document {
fn repeat(&self) -> Input<'a,D> {
let x = Add{ lhs: self.lhs.repeat(), rhs: self.rhs.repeat() };
Box::new(x)
}
}
impl<'a,D> Iterator for Add<'a,D> where D: Document {
type Item=Entry<D>;
fn next(&mut self) -> Option<Entry<D>> {
let d_lhs = self.lhs.next()?;
let d_rhs = self.rhs.next()?;
let doc = d_lhs.doc.clone() + d_rhs.doc.clone();
let entries = vec![d_lhs, d_rhs];
Some(Entry::new_merged(entries, doc))
}
}
impl<'a,D> Pipeline<'a,D> for Add<'a,D> where D: 'a + Document {
}
pub struct Sub<'a,D> where D: Document {
lhs: Input<'a,D>,
rhs: Input<'a,D>,
}
impl<'a,D> Sub<'a,D> where D: Document {
pub fn new(lhs: Input<'a,D>, rhs: Input<'a,D>) -> Sub<'a,D> {
Sub{lhs, rhs}
}
}
impl<'a,D> Repeater<'a,D> for Sub<'a,D> where D: 'a + Document {
fn repeat(&self) -> Input<'a,D> {
let x = Sub{ lhs: self.lhs.repeat(), rhs: self.rhs.repeat() };
Box::new(x)
}
}
impl<'a,D> Iterator for Sub<'a,D> where D: Document {
type Item=Entry<D>;
fn next(&mut self) -> Option<Entry<D>> {
let d_lhs = self.lhs.next()?;
let d_rhs = self.rhs.next()?;
let doc = d_lhs.doc.clone() - d_rhs.doc.clone();
let entries = vec![d_lhs, d_rhs];
Some(Entry::new_merged(entries, doc))
}
}
impl<'a,D> Pipeline<'a,D> for Sub<'a,D> where D: 'a + Document {
}
pub struct Shr<'a,D> where D: Document {
lhs: Input<'a,D>,
rhs: Input<'a,D>,
}
impl<'a,D> Shr<'a,D> where D: Document {
pub fn new(lhs: Input<'a,D>, rhs: Input<'a,D>) -> Shr<'a,D> {
Shr{lhs, rhs}
}
}
impl<'a,D> Repeater<'a,D> for Shr<'a,D> where D: 'a + Document {
fn repeat(&self) -> Input<'a,D> {
let x = Shr{ lhs: self.lhs.repeat(), rhs: self.rhs.repeat() };
Box::new(x)
}
}
impl<'a,D> Iterator for Shr<'a,D> where D: Document {
type Item=Entry<D>;
fn next(&mut self) -> Option<Entry<D>> {
let d_lhs = self.lhs.next()?;
let d_rhs = self.rhs.next()?;
let doc = d_lhs.doc.clone() >> d_rhs.doc.clone();