-
Notifications
You must be signed in to change notification settings - Fork 87
/
scheme.rs
1409 lines (1278 loc) · 39.5 KB
/
scheme.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 crate::{
ast::FilterAst,
functions::FunctionDefinition,
lex::{complete, expect, span, take_while, Lex, LexErrorKind, LexResult, LexWith},
types::{GetType, RhsValue, Type},
};
use fnv::FnvBuildHasher;
use indexmap::map::{Entry, IndexMap};
use serde::ser::SerializeMap;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use std::{
cmp::{max, min},
collections::HashMap,
convert::TryFrom,
error::Error,
fmt::{self, Debug, Display, Formatter},
hash::{Hash, Hasher},
iter::Iterator,
ptr,
};
use thiserror::Error;
/// An error that occurs if two underlying [schemes](struct@Scheme)
/// don't match.
#[derive(Debug, PartialEq, Error)]
#[error("underlying schemes do not match")]
pub struct SchemeMismatchError;
#[derive(Debug, PartialEq, Eq, Clone, Hash, Serialize)]
#[serde(tag = "kind", content = "value")]
/// FieldIndex is an enum with variants [`ArrayIndex(usize)`],
/// representing an index into an Array, or `[MapKey(String)`],
/// representing a key into a Map.
///
/// ```
/// #[allow(dead_code)]
/// enum FieldIndex {
/// ArrayIndex(u32),
/// MapKey(String),
/// }
/// ```
pub enum FieldIndex {
/// Index into an Array
ArrayIndex(u32),
/// Key into a Map
MapKey(String),
/// Map each element by applying a function or a comparison
MapEach,
}
impl<'i> Lex<'i> for FieldIndex {
fn lex(input: &'i str) -> LexResult<'i, Self> {
if let Ok(input) = expect(input, "*") {
return Ok((FieldIndex::MapEach, input));
}
// The token inside an [] can be either an integer index into an Array
// or a string key into a Map. The token is a key into a Map if it
// starts and ends with "\"", otherwise an integer index or an error.
let (rhs, rest) = match expect(input, "\"") {
Ok(_) => RhsValue::lex_with(input, Type::Bytes),
Err(_) => RhsValue::lex_with(input, Type::Int),
}?;
match rhs {
RhsValue::Int(i) => match u32::try_from(i) {
Ok(u) => Ok((FieldIndex::ArrayIndex(u), rest)),
Err(_) => Err((
LexErrorKind::ExpectedLiteral("expected positive integer as index"),
input,
)),
},
RhsValue::Bytes(b) => match String::from_utf8(b.to_vec()) {
Ok(s) => Ok((FieldIndex::MapKey(s), rest)),
Err(_) => Err((LexErrorKind::ExpectedLiteral("expected utf8 string"), input)),
},
_ => unreachable!(),
}
}
}
#[derive(Debug, PartialEq, Error)]
#[error("cannot access index {index:?} for type {actual:?}")]
pub struct IndexAccessError {
pub index: FieldIndex,
pub actual: Type,
}
#[derive(PartialEq, Eq, Clone, Copy, Hash)]
/// A structure to represent a field inside a [`Scheme`](struct@Scheme).
pub struct Field<'s> {
scheme: &'s Scheme,
index: usize,
}
impl<'s> Serialize for Field<'s> {
fn serialize<S: Serializer>(&self, ser: S) -> Result<S::Ok, S::Error> {
self.name().serialize(ser)
}
}
impl<'s> Debug for Field<'s> {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.name())
}
}
impl<'i, 's> LexWith<'i, &'s Scheme> for Field<'s> {
fn lex_with(input: &'i str, scheme: &'s Scheme) -> LexResult<'i, Self> {
match Identifier::lex_with(input, scheme) {
Ok((Identifier::Field(f), rest)) => Ok((f, rest)),
Ok((Identifier::Function(_), rest)) => Err((
LexErrorKind::UnknownField(UnknownFieldError),
span(input, rest),
)),
Err((LexErrorKind::UnknownIdentifier, s)) => {
Err((LexErrorKind::UnknownField(UnknownFieldError), s))
}
Err(err) => Err(err),
}
}
}
impl<'s> Field<'s> {
/// Returns the field's name as recorded in the [`Scheme`](struct@Scheme).
pub fn name(&self) -> &'s str {
self.scheme.items.get_index(self.index).unwrap().0
}
pub(crate) fn index(&self) -> usize {
self.index
}
/// Returns the [`Scheme`](struct@Scheme) to which this field belongs to.
pub fn scheme(&self) -> &'s Scheme {
self.scheme
}
}
impl<'s> GetType for Field<'s> {
fn get_type(&self) -> Type {
match self.scheme.items.get_index(self.index).unwrap().1 {
SchemeItem::Field(ty) => ty.clone(),
SchemeItem::Function(_) => unreachable!(),
}
}
}
#[derive(PartialEq, Eq, Clone, Copy, Hash)]
/// A structure to represent a function inside a [`Scheme`](struct@Scheme).
pub struct Function<'s> {
scheme: &'s Scheme,
index: usize,
}
impl<'s> Serialize for Function<'s> {
fn serialize<S: Serializer>(&self, ser: S) -> Result<S::Ok, S::Error> {
self.name().serialize(ser)
}
}
impl<'s> Debug for Function<'s> {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.name())
}
}
impl<'i, 's> LexWith<'i, &'s Scheme> for Function<'s> {
fn lex_with(input: &'i str, scheme: &'s Scheme) -> LexResult<'i, Self> {
match Identifier::lex_with(input, scheme) {
Ok((Identifier::Function(f), rest)) => Ok((f, rest)),
Ok((Identifier::Field(_), rest)) => Err((
LexErrorKind::UnknownFunction(UnknownFunctionError),
span(input, rest),
)),
Err((LexErrorKind::UnknownIdentifier, s)) => {
Err((LexErrorKind::UnknownFunction(UnknownFunctionError), s))
}
Err(err) => Err(err),
}
}
}
impl<'s> Function<'s> {
/// Returns the function's name as recorded in the [`Scheme`](struct@Scheme).
pub fn name(&self) -> &'s str {
self.scheme.items.get_index(self.index).unwrap().0
}
/// Returns the [`Scheme`](struct@Scheme) to which this function belongs to.
pub fn scheme(&self) -> &'s Scheme {
self.scheme
}
pub(crate) fn as_definition(&self) -> &'s dyn FunctionDefinition {
match self.scheme.items.get_index(self.index).unwrap().1 {
SchemeItem::Field(_) => unreachable!(),
SchemeItem::Function(func) => &**func,
}
}
}
/// An enum to represent an entry inside a [`Scheme`](struct@Scheme).
/// It can be either a [`Field`](struct@Field) or a [`Function`](struct@Function).
#[derive(Debug)]
pub enum Identifier<'s> {
/// Identifier is a [`Field`](struct@Field)
Field(Field<'s>),
/// Identifier is a [`Function`](struct@Function)
Function(Function<'s>),
}
impl<'s> Identifier<'s> {
/// Converts the identifier into a [`Field`](struct@Field) if possible.
pub fn into_field(self) -> Option<Field<'s>> {
match self {
Self::Field(f) => Some(f),
_ => None,
}
}
/// Converts the identifier into a [`Function`](struct@Function) if possible.
pub fn into_function(self) -> Option<Function<'s>> {
match self {
Self::Function(f) => Some(f),
_ => None,
}
}
}
impl<'i, 's> LexWith<'i, &'s Scheme> for Identifier<'s> {
fn lex_with(mut input: &'i str, scheme: &'s Scheme) -> LexResult<'i, Self> {
let initial_input = input;
loop {
input = take_while(input, "identifier character", |c| {
c.is_ascii_alphanumeric() || c == '_'
})?
.1;
match expect(input, ".") {
Ok(rest) => input = rest,
Err(_) => break,
};
}
let name = span(initial_input, input);
let field = scheme
.get(name)
.ok_or((LexErrorKind::UnknownIdentifier, name))?;
Ok((field, input))
}
}
/// An error that occurs if an unregistered field name was queried from a
/// [`Scheme`](struct@Scheme).
#[derive(Debug, PartialEq, Error)]
#[error("unknown field")]
pub struct UnknownFieldError;
/// An error that occurs if an unregistered function name was queried from a
/// [`Scheme`](struct@Scheme).
#[derive(Debug, PartialEq, Error)]
#[error("unknown function")]
pub struct UnknownFunctionError;
/// An error that occurs when previously defined field gets redefined.
#[derive(Debug, PartialEq, Error)]
#[error("attempt to redefine field {0}")]
pub struct FieldRedefinitionError(String);
/// An error that occurs when previously defined function gets redefined.
#[derive(Debug, PartialEq, Error)]
#[error("attempt to redefine function {0}")]
pub struct FunctionRedefinitionError(String);
/// An error that occurs when trying to redefine a field or function.
#[derive(Debug, PartialEq, Error)]
pub enum IdentifierRedefinitionError {
/// An error that occurs when previously defined field gets redefined.
#[error("{0}")]
Field(#[source] FieldRedefinitionError),
/// An error that occurs when previously defined function gets redefined.
#[error("{0}")]
Function(#[source] FunctionRedefinitionError),
}
/// An opaque filter parsing error associated with the original input.
///
/// For now, you can just print it in a debug or a human-readable fashion.
#[derive(Debug, PartialEq)]
pub struct ParseError<'i> {
/// The error that occurred when parsing the input
pub(crate) kind: LexErrorKind,
/// The input that caused the parse error
pub(crate) input: &'i str,
/// The line number on the input where the error occurred
pub(crate) line_number: usize,
/// The start of the bad input
pub(crate) span_start: usize,
/// The number of characters that span the bad input
pub(crate) span_len: usize,
}
impl<'i> Error for ParseError<'i> {}
impl<'i> ParseError<'i> {
/// Create a new ParseError for the input, LexErrorKind and span in the
/// input.
pub fn new(mut input: &'i str, (kind, span): (LexErrorKind, &'i str)) -> Self {
let input_range = input.as_ptr() as usize..=input.as_ptr() as usize + input.len();
assert!(
input_range.contains(&(span.as_ptr() as usize))
&& input_range.contains(&(span.as_ptr() as usize + span.len()))
);
let mut span_start = span.as_ptr() as usize - input.as_ptr() as usize;
let (line_number, line_start) = input[..span_start]
.match_indices('\n')
.map(|(pos, _)| pos + 1)
.scan(0, |line_number, line_start| {
*line_number += 1;
Some((*line_number, line_start))
})
.last()
.unwrap_or_default();
input = &input[line_start..];
span_start -= line_start;
let mut span_len = span.len();
if let Some(line_end) = input.find('\n') {
input = &input[..line_end];
span_len = min(span_len, line_end - span_start);
}
ParseError {
kind,
input,
line_number,
span_start,
span_len,
}
}
}
impl<'i> Display for ParseError<'i> {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
writeln!(
f,
"Filter parsing error ({}:{}):",
self.line_number + 1,
self.span_start + 1
)?;
writeln!(f, "{}", self.input)?;
for _ in 0..self.span_start {
write!(f, " ")?;
}
for _ in 0..max(1, self.span_len) {
write!(f, "^")?;
}
writeln!(f, " {}", self.kind)?;
Ok(())
}
}
#[derive(Debug)]
enum SchemeItem {
Field(Type),
Function(Box<dyn FunctionDefinition>),
}
impl<T: FunctionDefinition + 'static> From<T> for Box<dyn FunctionDefinition> {
fn from(func: T) -> Box<dyn FunctionDefinition> {
Box::new(func)
}
}
/// A structure to represent a list inside a [`scheme`](struct.Scheme.html).
///
/// See [`Scheme::get_list`](struct.Scheme.html#method.get_list).
#[derive(PartialEq, Eq, Clone, Copy, Hash)]
pub struct List<'s> {
scheme: &'s Scheme,
index: usize,
}
impl<'s> List<'s> {
pub(crate) fn index(&self) -> usize {
self.index
}
pub(crate) fn scheme(&self) -> &'s Scheme {
self.scheme
}
pub(crate) fn definition(&self) -> &'s dyn ListDefinition {
&**self.scheme.lists.get_index(self.index).unwrap().1
}
}
impl<'s> Debug for List<'s> {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(
f,
"{:?}",
self.scheme.lists.get_index(self.index).unwrap().1
)
}
}
/// An error that occurs when previously defined list gets redefined.
#[derive(Debug, PartialEq, Error)]
#[error("attempt to redefine list for type {0:?}")]
pub struct ListRedefinitionError(Type);
use crate::list_matcher::ListDefinition;
/// The main registry for fields and their associated types.
///
/// This is necessary to provide typechecking for runtime values provided
/// to the [execution context](::ExecutionContext) and also to aid parser
/// in ambiguous contexts.
#[derive(Default, Debug)]
pub struct Scheme {
items: IndexMap<String, SchemeItem, FnvBuildHasher>,
lists: IndexMap<Type, Box<dyn ListDefinition>>,
}
impl PartialEq for Scheme {
fn eq(&self, other: &Self) -> bool {
ptr::eq(self, other)
}
}
impl Eq for Scheme {}
impl Hash for Scheme {
fn hash<H: Hasher>(&self, state: &mut H) {
(self as *const Scheme).hash(state);
}
}
impl Serialize for Scheme {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let mut map = serializer.serialize_map(Some(self.len().0))?;
for (k, v) in self.iter().filter_map(|(key, val)| match val {
Identifier::Field(field) => Some((key, field)),
Identifier::Function(_) => None,
}) {
map.serialize_entry(k, &v.get_type())?;
}
map.end()
}
}
impl<'de> Deserialize<'de> for Scheme {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let map: HashMap<String, Type> = HashMap::<String, Type>::deserialize(deserializer)?;
Ok(Self::try_from_iter(map.into_iter()).unwrap())
}
}
impl<'s> Scheme {
/// Creates a new scheme.
pub fn new() -> Self {
Default::default()
}
/// Creates a new scheme with capacity for `n` fields.
pub fn with_capacity(n: usize) -> Self {
Scheme {
items: IndexMap::with_capacity_and_hasher(n, FnvBuildHasher::default()),
..Default::default()
}
}
/// Returns the [`identifier`](struct@Identifier) with name [`name`]
pub fn get(&'s self, name: &str) -> Option<Identifier<'s>> {
self.items
.get_full(name)
.map(move |(index, _, item)| match item {
SchemeItem::Field(_) => Identifier::Field(Field {
scheme: self,
index,
}),
SchemeItem::Function(_) => Identifier::Function(Function {
scheme: self,
index,
}),
})
}
/// Registers a field and its corresponding type.
pub fn add_field(&mut self, name: String, ty: Type) -> Result<(), IdentifierRedefinitionError> {
match self.items.entry(name) {
Entry::Occupied(entry) => match entry.get() {
SchemeItem::Field(_) => Err(IdentifierRedefinitionError::Field(
FieldRedefinitionError(entry.key().to_string()),
)),
SchemeItem::Function(_) => Err(IdentifierRedefinitionError::Function(
FunctionRedefinitionError(entry.key().to_string()),
)),
},
Entry::Vacant(entry) => {
entry.insert(SchemeItem::Field(ty));
Ok(())
}
}
}
/// Registers a series of fields from an iterable, reporting any conflicts.
pub fn try_from_iter(
iter: impl IntoIterator<Item = (String, Type)>,
) -> Result<Self, IdentifierRedefinitionError> {
let iter = iter.into_iter();
let (low, _) = iter.size_hint();
let mut scheme = Scheme::with_capacity(low);
for (name, value) in iter {
scheme.add_field(name, value)?;
}
Ok(scheme)
}
/// Returns the [`field`](struct@Field) with name [`name`]
pub fn get_field(&'s self, name: &str) -> Result<Field<'s>, UnknownFieldError> {
match self.get(name) {
Some(Identifier::Field(f)) => Ok(f),
_ => Err(UnknownFieldError),
}
}
/// Returns the number of element in the [`scheme`](struct@Scheme)
pub fn len(&self) -> (usize, usize) {
(self.items.len(), self.lists.len())
}
/// Returns true if the [`scheme`](struct@Scheme) is empty
pub fn is_empty(&self) -> bool {
self.items.is_empty()
}
/// Registers a function
pub fn add_function(
&mut self,
name: String,
function: impl Into<Box<dyn FunctionDefinition + 'static>>,
) -> Result<(), IdentifierRedefinitionError> {
match self.items.entry(name) {
Entry::Occupied(entry) => match entry.get() {
SchemeItem::Field(_) => Err(IdentifierRedefinitionError::Field(
FieldRedefinitionError(entry.key().to_string()),
)),
SchemeItem::Function(_) => Err(IdentifierRedefinitionError::Function(
FunctionRedefinitionError(entry.key().to_string()),
)),
},
Entry::Vacant(entry) => {
entry.insert(SchemeItem::Function(function.into()));
Ok(())
}
}
}
/// Registers a list of functions
pub fn add_functions(
&mut self,
functions: impl IntoIterator<Item = (String, impl Into<Box<dyn FunctionDefinition + 'static>>)>,
) -> Result<(), IdentifierRedefinitionError> {
for (name, func) in functions {
self.add_function(name, func)?;
}
Ok(())
}
/// Returns the [`function`](struct@Function) with name [`name`]
pub fn get_function(&'s self, name: &str) -> Result<Function<'s>, UnknownFunctionError> {
match self.get(name) {
Some(Identifier::Function(f)) => Ok(f),
_ => Err(UnknownFunctionError),
}
}
/// Parses a filter into an AST form.
pub fn parse<'i>(&'s self, input: &'i str) -> Result<FilterAst<'s>, ParseError<'i>> {
complete(FilterAst::lex_with(input.trim(), self)).map_err(|err| ParseError::new(input, err))
}
/// Iterates over all items.
pub fn iter(&'s self) -> impl ExactSizeIterator<Item = (&'s str, Identifier<'s>)> {
self.items
.iter()
.enumerate()
.map(move |(index, (name, item))| match item {
SchemeItem::Field(_) => (
name.as_str(),
Identifier::Field(Field {
scheme: self,
index,
}),
),
SchemeItem::Function(_) => (
name.as_str(),
Identifier::Function(Function {
scheme: self,
index,
}),
),
})
}
/// Registers a new [`list`](trait.ListDefinition.html) for a given [`type`](enum.Type.html).
pub fn add_list(
&mut self,
ty: Type,
list: Box<dyn ListDefinition>,
) -> Result<(), ListRedefinitionError> {
match self.lists.entry(ty) {
Entry::Occupied(entry) => Err(ListRedefinitionError(entry.key().clone())),
Entry::Vacant(entry) => {
entry.insert(list);
Ok(())
}
}
}
/// Returns the [`list`](struct.List.html) for a given [`type`](enum.Type.html).
pub fn get_list(&self, ty: &Type) -> Option<List<'_>> {
self.lists.get_index_of(ty).map(move |index| List {
scheme: self,
index,
})
}
/// Iterates over all registered [`lists`](trait.ListDefinition.html).
pub fn lists(&self) -> impl ExactSizeIterator<Item = (&Type, List<'_>)> {
self.lists.keys().enumerate().map(move |(index, key)| {
(
key,
List {
scheme: self,
index,
},
)
})
}
}
/// A convenience macro for constructing a [`Scheme`](struct@Scheme) with static
/// contents.
#[macro_export]
macro_rules! Scheme {
($($ns:ident $(. $field:ident)*: $ty:ident $(($subty:tt $($rest:tt)*))?),* $(,)*) => {
$crate::Scheme::try_from_iter(
[$(
(
concat!(stringify!($ns) $(, ".", stringify!($field))*),
Scheme!($ty $(($subty $($rest)*))?),
)
),*]
.iter()
.map(|&(k, ref v)| (k.to_owned(), v.clone())),
)
// Treat duplciations in static schemes as a developer's mistake.
.unwrap_or_else(|err| panic!("{}", err))
};
($ty:ident $(($subty:tt $($rest:tt)*))?) => {crate::Type::$ty$((Box::new(Scheme!($subty $($rest)*))))?};
}
#[test]
fn test_parse_error() {
use crate::types::TypeMismatchError;
use indoc::indoc;
let scheme = &Scheme! {
num: Int,
arr: Array(Bool),
};
{
let err = scheme.parse("xyz").unwrap_err();
assert_eq!(
err,
ParseError {
kind: LexErrorKind::UnknownIdentifier,
input: "xyz",
line_number: 0,
span_start: 0,
span_len: 3
}
);
assert_eq!(
err.to_string(),
indoc!(
r#"
Filter parsing error (1:1):
xyz
^^^ unknown identifier
"#
)
);
}
{
let err = scheme.parse("xyz\n").unwrap_err();
assert_eq!(
err,
ParseError {
kind: LexErrorKind::UnknownIdentifier,
input: "xyz",
line_number: 0,
span_start: 0,
span_len: 3
}
);
assert_eq!(
err.to_string(),
indoc!(
r#"
Filter parsing error (1:1):
xyz
^^^ unknown identifier
"#
)
);
}
{
let err = scheme.parse("\n\n xyz").unwrap_err();
assert_eq!(
err,
ParseError {
kind: LexErrorKind::UnknownIdentifier,
input: " xyz",
line_number: 2,
span_start: 4,
span_len: 3
}
);
assert_eq!(
err.to_string(),
indoc!(
r#"
Filter parsing error (3:5):
xyz
^^^ unknown identifier
"#
)
);
}
{
let err = scheme
.parse(indoc!(
r#"
num == 10 or
num == true or
num == 20
"#
))
.unwrap_err();
assert_eq!(
err,
ParseError {
kind: LexErrorKind::ExpectedName("digit"),
input: "num == true or",
line_number: 1,
span_start: 7,
span_len: 7
}
);
assert_eq!(
err.to_string(),
indoc!(
r#"
Filter parsing error (2:8):
num == true or
^^^^^^^ expected digit
"#
)
);
}
{
let err = scheme
.parse(indoc!(
r#"
arr and arr
"#
))
.unwrap_err();
assert_eq!(
err,
ParseError {
kind: LexErrorKind::TypeMismatch(TypeMismatchError {
expected: Type::Bool.into(),
actual: Type::Array(Box::new(Type::Bool)),
}),
input: "arr and arr",
line_number: 0,
span_start: 11,
span_len: 0,
}
);
assert_eq!(
err.to_string(),
indoc!(
r#"
Filter parsing error (1:12):
arr and arr
^ expected value of type {Type(Bool)}, but got Array(Bool)
"#
)
);
}
}
#[test]
fn test_parse_error_in_op() {
use cidr::NetworkParseError;
use indoc::indoc;
use std::{net::IpAddr, str::FromStr};
let scheme = &Scheme! {
num: Int,
bool: Bool,
str: Bytes,
ip: Ip,
str_arr: Array(Bytes),
str_map: Map(Bytes),
};
{
let err = scheme.parse("bool in {0}").unwrap_err();
assert_eq!(
err,
ParseError {
kind: LexErrorKind::EOF,
input: "bool in {0}",
line_number: 0,
span_start: 4,
span_len: 7
}
);
assert_eq!(
err.to_string(),
indoc!(
r#"
Filter parsing error (1:5):
bool in {0}
^^^^^^^ unrecognised input
"#
)
);
}
{
let err = scheme.parse("bool in {127.0.0.1}").unwrap_err();
assert_eq!(
err,
ParseError {
kind: LexErrorKind::EOF,
input: "bool in {127.0.0.1}",
line_number: 0,
span_start: 4,
span_len: 15
}
);
assert_eq!(
err.to_string(),
indoc!(
r#"
Filter parsing error (1:5):
bool in {127.0.0.1}
^^^^^^^^^^^^^^^ unrecognised input
"#
)
);
}
{
let err = scheme.parse("bool in {\"test\"}").unwrap_err();
assert_eq!(
err,
ParseError {
kind: LexErrorKind::EOF,
input: "bool in {\"test\"}",
line_number: 0,
span_start: 4,
span_len: 12
}
);
assert_eq!(
err.to_string(),
indoc!(
r#"
Filter parsing error (1:5):
bool in {"test"}
^^^^^^^^^^^^ unrecognised input
"#
)
);
}
{
let err = scheme.parse("num in {127.0.0.1}").unwrap_err();
assert_eq!(
err,
ParseError {
kind: LexErrorKind::ExpectedName("digit"),
input: "num in {127.0.0.1}",
line_number: 0,
span_start: 11,
span_len: 7
}
);
assert_eq!(
err.to_string(),
indoc!(
r#"
Filter parsing error (1:12):
num in {127.0.0.1}
^^^^^^^ expected digit
"#
)
);
}
{
let err = scheme.parse("num in {\"test\"}").unwrap_err();
assert_eq!(
err,
ParseError {
kind: LexErrorKind::ExpectedName("digit"),
input: "num in {\"test\"}",
line_number: 0,
span_start: 8,
span_len: 7
}
);
assert_eq!(
err.to_string(),
indoc!(
r#"
Filter parsing error (1:9):
num in {"test"}
^^^^^^^ expected digit
"#
)
);
}
{
let err = scheme.parse("ip in {666}").unwrap_err();
assert_eq!(
err,
ParseError {
kind: LexErrorKind::ParseNetwork(
IpAddr::from_str("666")
.map_err(NetworkParseError::AddrParseError)
.unwrap_err()
),
input: "ip in {666}",
line_number: 0,
span_start: 7,
span_len: 3
}
);
assert_eq!(
err.to_string(),
indoc!(
r#"
Filter parsing error (1:8):
ip in {666}
^^^ couldn't parse address in network: invalid IP address syntax
"#
)
);
}
{
let err = scheme.parse("ip in {\"test\"}").unwrap_err();