@@ -2,11 +2,11 @@ use crate::otp::otp_element::OTPDatabase;
22use crate :: { clipboard, otp:: otp_element:: OTPElement } ;
33use clap:: Args ;
44use color_eyre:: eyre:: eyre;
5- use globset:: { Glob , GlobMatcher } ;
5+ use globset:: { GlobBuilder , GlobMatcher } ;
66
77use super :: SubcommandExecutor ;
88
9- #[ derive( Args ) ]
9+ #[ derive( Args , Default ) ]
1010pub struct ExtractArgs {
1111 /// Code Index
1212 #[ arg( short, long, required_unless_present_any = [ "issuer" , "label" ] ) ]
@@ -36,14 +36,14 @@ impl TryFrom<ExtractArgs> for ExtractFilterGlob {
3636 type Error = color_eyre:: eyre:: ErrReport ;
3737
3838 fn try_from ( value : ExtractArgs ) -> Result < Self , Self :: Error > {
39- let issuer_glob = if value. issuer . is_some ( ) {
40- Some ( Glob :: new ( & value . issuer . unwrap ( ) ) ? . compile_matcher ( ) )
39+ let issuer_glob = if let Some ( issuer ) = value. issuer {
40+ Some ( create_matcher ( & issuer) ? )
4141 } else {
4242 None
4343 } ;
4444
45- let label_glob = if value. label . is_some ( ) {
46- Some ( Glob :: new ( & value . label . unwrap ( ) ) ? . compile_matcher ( ) )
45+ let label_glob = if let Some ( label ) = value. label {
46+ Some ( create_matcher ( & label) ? )
4747 } else {
4848 None
4949 } ;
@@ -56,6 +56,15 @@ impl TryFrom<ExtractArgs> for ExtractFilterGlob {
5656 }
5757}
5858
59+ fn create_matcher (
60+ glob : & str ,
61+ ) -> Result < GlobMatcher , <ExtractFilterGlob as TryFrom < ExtractArgs > >:: Error > {
62+ Ok ( GlobBuilder :: new ( glob)
63+ . case_insensitive ( true )
64+ . build ( ) ?
65+ . compile_matcher ( ) )
66+ }
67+
5968impl SubcommandExecutor for ExtractArgs {
6069 fn run_command ( self , otp_database : OTPDatabase ) -> color_eyre:: Result < OTPDatabase > {
6170 let copy_to_clipboard = self . copy_to_clipboard ;
@@ -104,11 +113,13 @@ fn filter_extract(args: &ExtractFilterGlob, index: usize, candidate: &OTPElement
104113
105114#[ cfg( test) ]
106115mod tests {
107- use globset:: Glob ;
108116
109- use crate :: otp:: otp_element:: { OTPDatabase , OTPElementBuilder } ;
117+ use crate :: {
118+ arguments:: extract:: ExtractArgs ,
119+ otp:: otp_element:: { OTPDatabase , OTPElementBuilder } ,
120+ } ;
110121
111- use super :: { find_match, ExtractFilterGlob } ;
122+ use super :: find_match;
112123
113124 #[ test]
114125 fn test_glob_filtering_good_issuer ( ) {
@@ -132,14 +143,13 @@ mod tests {
132143 . unwrap ( ) ,
133144 ) ;
134145
135- let filter = ExtractFilterGlob {
136- issuer_glob : Some ( Glob :: new ( "test-iss*" ) . unwrap ( ) . compile_matcher ( ) ) ,
137- label_glob : None ,
138- index : None ,
146+ let filter = ExtractArgs {
147+ issuer : Some ( "test-iss*" . to_string ( ) ) ,
148+ ..Default :: default ( )
139149 } ;
140150
141151 // Act
142- let found_match = find_match ( & otp_database, filter) ;
152+ let found_match = find_match ( & otp_database, filter. try_into ( ) . unwrap ( ) ) ;
143153
144154 // Assert
145155 assert ! ( found_match. is_some( ) ) ;
@@ -167,14 +177,13 @@ mod tests {
167177 . unwrap ( ) ,
168178 ) ;
169179
170- let filter = ExtractFilterGlob {
171- issuer_glob : None ,
172- label_glob : Some ( Glob :: new ( "test-la*" ) . unwrap ( ) . compile_matcher ( ) ) ,
173- index : None ,
180+ let filter = ExtractArgs {
181+ label : Some ( "test-la*" . to_string ( ) ) ,
182+ ..Default :: default ( )
174183 } ;
175184
176185 // Act
177- let found_match = find_match ( & otp_database, filter) ;
186+ let found_match = find_match ( & otp_database, filter. try_into ( ) . unwrap ( ) ) ;
178187
179188 // Assert
180189 assert ! ( found_match. is_some( ) ) ;
@@ -202,14 +211,13 @@ mod tests {
202211 . unwrap ( ) ,
203212 ) ;
204213
205- let filter = ExtractFilterGlob {
206- issuer_glob : None ,
207- label_glob : Some ( Glob :: new ( "test-lala*" ) . unwrap ( ) . compile_matcher ( ) ) ,
208- index : None ,
214+ let filter = ExtractArgs {
215+ label : Some ( "test-lala*" . to_string ( ) ) ,
216+ ..Default :: default ( )
209217 } ;
210218
211219 // Act
212- let found_match = find_match ( & otp_database, filter) ;
220+ let found_match = find_match ( & otp_database, filter. try_into ( ) . unwrap ( ) ) ;
213221
214222 // Assert
215223 assert ! ( found_match. is_none( ) ) ;
@@ -237,14 +245,14 @@ mod tests {
237245 . unwrap ( ) ,
238246 ) ;
239247
240- let filter = ExtractFilterGlob {
241- issuer_glob : Some ( Glob :: new ( "test*" ) . unwrap ( ) . compile_matcher ( ) ) ,
242- label_glob : Some ( Glob :: new ( "test-la*" ) . unwrap ( ) . compile_matcher ( ) ) ,
243- index : None ,
248+ let filter = ExtractArgs {
249+ issuer : Some ( "test*" . to_string ( ) ) ,
250+ label : Some ( "test-la*" . to_string ( ) ) ,
251+ .. Default :: default ( )
244252 } ;
245253
246254 // Act
247- let found_match = find_match ( & otp_database, filter) ;
255+ let found_match = find_match ( & otp_database, filter. try_into ( ) . unwrap ( ) ) ;
248256
249257 // Assert
250258 assert ! ( found_match. is_some( ) ) ;
@@ -272,16 +280,50 @@ mod tests {
272280 . unwrap ( ) ,
273281 ) ;
274282
275- let filter = ExtractFilterGlob {
276- issuer_glob : Some ( Glob :: new ( "test-no*" ) . unwrap ( ) . compile_matcher ( ) ) ,
277- label_glob : Some ( Glob :: new ( "test-la*" ) . unwrap ( ) . compile_matcher ( ) ) ,
278- index : None ,
283+ let filter = ExtractArgs {
284+ issuer : Some ( "test-no*" . to_string ( ) ) ,
285+ label : Some ( "test-la*" . to_string ( ) ) ,
286+ .. Default :: default ( )
279287 } ;
280288
281289 // Act
282- let found_match = find_match ( & otp_database, filter) ;
290+ let found_match = find_match ( & otp_database, filter. try_into ( ) . unwrap ( ) ) ;
283291
284292 // Assert
285293 assert ! ( found_match. is_none( ) ) ;
286294 }
295+
296+ #[ test]
297+ fn test_glob_filtering_case_insensitive ( ) {
298+ // Arrange
299+ let mut otp_database = OTPDatabase :: default ( ) ;
300+ otp_database. add_element (
301+ OTPElementBuilder :: default ( )
302+ . issuer ( "test-issuer" )
303+ . label ( "test-label" )
304+ . secret ( "AA" )
305+ . build ( )
306+ . unwrap ( ) ,
307+ ) ;
308+
309+ otp_database. add_element (
310+ OTPElementBuilder :: default ( )
311+ . issuer ( "test-issuer2" )
312+ . label ( "test-label2" )
313+ . secret ( "AA" )
314+ . build ( )
315+ . unwrap ( ) ,
316+ ) ;
317+
318+ let filter = ExtractArgs {
319+ issuer : Some ( "TeSt-iSS*" . to_string ( ) ) ,
320+ ..Default :: default ( )
321+ } ;
322+
323+ // Act
324+ let found_match = find_match ( & otp_database, filter. try_into ( ) . unwrap ( ) ) ;
325+
326+ // Assert
327+ assert ! ( found_match. is_some( ) ) ;
328+ }
287329}
0 commit comments