@@ -2,13 +2,23 @@ use regex::Regex;
2
2
use serde:: de;
3
3
use std:: fmt;
4
4
5
- type MyError = Box < dyn std :: error :: Error > ;
6
-
5
+ // this lint is falsely triggering on this, which is *not* interior mutable
6
+ # [ allow ( clippy :: declare_interior_mutable_const ) ]
7
7
pub const TRUE_FILTER : Filter = Filter {
8
8
invert : false ,
9
9
operator : FilterOperator :: True ,
10
10
} ;
11
11
12
+ #[ derive( Debug ) ]
13
+ pub enum FilterErrorKind {
14
+ MissingColon ,
15
+ OperatorParse ,
16
+ RegexError ( regex:: Error ) ,
17
+ }
18
+
19
+ #[ derive( Debug ) ]
20
+ pub struct FilterError ( String , FilterErrorKind ) ;
21
+
12
22
#[ derive( Debug ) ]
13
23
enum FilterOperator {
14
24
Equals ( String ) ,
@@ -27,15 +37,15 @@ pub struct Filter {
27
37
28
38
impl FilterOperator {
29
39
/// Parses a stringified filter operator into a [`FilterOperator`](enum.FilterOperator.html)
30
- fn parse ( s : & str , content : String ) -> Result < FilterOperator , MyError > {
40
+ fn parse ( s : & str , content : String ) -> Result < FilterOperator , FilterError > {
31
41
match s {
32
42
"equals" => Ok ( FilterOperator :: Equals ( content) ) ,
33
43
"startsWith" => Ok ( FilterOperator :: StartsWith ( content) ) ,
34
44
"endsWith" => Ok ( FilterOperator :: EndsWith ( content) ) ,
35
45
"contains" => Ok ( FilterOperator :: Contains ( content) ) ,
36
46
"true" => Ok ( FilterOperator :: True ) ,
37
- "regex" => Ok ( FilterOperator :: Regex ( Regex :: new ( & content) ?) ) ,
38
- _ => Err ( "unknown filter operator; options are equals, startsWith, endsWith, contains, true, regex" . into ( ) ) ,
47
+ "regex" => Ok ( FilterOperator :: Regex ( Regex :: new ( & content) . map_err ( |e| FilterError ( content , FilterErrorKind :: RegexError ( e ) ) ) ?) ) ,
48
+ _ => Err ( FilterError ( "unknown filter operator; options are equals, startsWith, endsWith, contains, true, regex" . to_owned ( ) , FilterErrorKind :: OperatorParse ) ) ,
39
49
}
40
50
}
41
51
@@ -55,11 +65,13 @@ impl FilterOperator {
55
65
56
66
impl Filter {
57
67
/// Parses a filter into a Filter struct.
58
- fn parse ( s : & str ) -> Result < Filter , MyError > {
68
+ fn parse ( s : & str ) -> Result < Filter , FilterError > {
59
69
let invert = s. starts_with ( '!' ) ;
60
70
let s = if invert { & s[ 1 ..] } else { s } ;
61
71
62
- let colon = s. find ( ":" ) . ok_or_else ( || "missing colon in filter" ) ?;
72
+ let colon = s
73
+ . find ( ':' )
74
+ . ok_or_else ( || FilterError ( s. to_owned ( ) , FilterErrorKind :: MissingColon ) ) ?;
63
75
let ( operator, text) = s. split_at ( colon) ;
64
76
// chop off colon
65
77
let text = & text[ 1 ..] ;
98
110
E : de:: Error ,
99
111
{
100
112
let mut filters = Vec :: new ( ) ;
101
- for filt in v. split ( "~" ) {
102
- let parsed = Filter :: parse ( filt) . map_err ( E :: custom) ?;
113
+ for filt in v. split ( '~' ) {
114
+ let parsed = Filter :: parse ( filt) . map_err ( |e| E :: custom ( format ! ( "{:?}" , e ) ) ) ?;
103
115
filters. push ( parsed) ;
104
116
}
105
117
0 commit comments