1
1
#[ derive( Clone , Default , Eq , PartialEq ) ]
2
2
pub ( crate ) struct Filter {
3
+ active : Option < bool > ,
3
4
q : String ,
4
5
source : String ,
5
6
tag : String ,
@@ -14,6 +15,7 @@ impl Filter {
14
15
#[ must_use]
15
16
pub fn from ( location : & crate :: Location ) -> Self {
16
17
Self {
18
+ active : location. active ( ) ,
17
19
q : location. q ( ) ,
18
20
source : location. param ( "source" ) ,
19
21
tag : location. param ( "tag" ) ,
@@ -22,13 +24,17 @@ impl Filter {
22
24
23
25
#[ must_use]
24
26
pub fn is_empty ( & self ) -> bool {
25
- self . q . is_empty ( ) && self . tag . is_empty ( ) && self . source . is_empty ( )
27
+ self . active . is_none ( ) && self . q . is_empty ( ) && self . tag . is_empty ( ) && self . source . is_empty ( )
26
28
}
27
29
28
30
#[ must_use]
29
31
pub fn to_url_param ( & self ) -> String {
30
32
let mut params = Vec :: new ( ) ;
31
33
34
+ if let Some ( active) = self . active {
35
+ params. push ( format ! ( "active={active}" ) ) ;
36
+ }
37
+
32
38
if !self . q . is_empty ( ) {
33
39
params. push ( format ! ( "q={}" , urlencoding:: encode( & self . q) ) ) ;
34
40
}
@@ -47,15 +53,19 @@ impl Filter {
47
53
48
54
impl From < String > for Filter {
49
55
fn from ( query : String ) -> Self {
56
+ let mut active = None ;
50
57
let mut q = query. clone ( ) ;
51
58
let mut source = String :: new ( ) ;
52
59
let mut tag = String :: new ( ) ;
53
60
54
61
let regex =
55
- regex:: Regex :: new ( r#"(:?source=(?P<source>[^ ]+) )?(tag=(?P<tag>[^ ]+) )?(?P<q>.*)"# )
62
+ regex:: Regex :: new ( r#"(:?active=(?P<active>[^ ]+) )?(:? source=(?P<source>[^ ]+) )?(tag=(?P<tag>[^ ]+) )?(?P<q>.*)"# )
56
63
. unwrap ( ) ;
57
64
58
65
if let Some ( captures) = regex. captures ( & query) {
66
+ active = captures
67
+ . name ( "active" )
68
+ . and_then ( |x| x. as_str ( ) . parse ( ) . ok ( ) ) ;
59
69
q = captures
60
70
. name ( "q" )
61
71
. map_or_else ( || query. clone ( ) , |x| x. as_str ( ) . to_string ( ) ) ;
@@ -69,12 +79,21 @@ impl From<String> for Filter {
69
79
. unwrap_or_default ( ) ;
70
80
}
71
81
72
- Self { q, source, tag }
82
+ Self {
83
+ active,
84
+ q,
85
+ source,
86
+ tag,
87
+ }
73
88
}
74
89
}
75
90
76
91
impl std:: fmt:: Display for Filter {
77
92
fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
93
+ if let Some ( active) = self . active {
94
+ write ! ( f, "active={active} " ) ?;
95
+ }
96
+
78
97
if !self . source . is_empty ( ) {
79
98
write ! ( f, "source={} " , self . source) ?;
80
99
}
0 commit comments