@@ -1341,8 +1341,7 @@ mod tests {
13411341 }
13421342
13431343 #[ test]
1344- fn test_algo_based_regex ( ) {
1345- let algo_based_regex = Regex :: new ( ALGO_BASED_REGEX ) . unwrap ( ) ;
1344+ fn test_algo_based_parser ( ) {
13461345 #[ allow( clippy:: type_complexity) ]
13471346 let test_cases: & [ ( & [ u8 ] , Option < ( & [ u8 ] , Option < & [ u8 ] > , & [ u8 ] , & [ u8 ] ) > ) ] = & [
13481347 ( b"SHA256 (example.txt) = d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2" , Some ( ( b"SHA256" , None , b"example.txt" , b"d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2" ) ) ) ,
@@ -1353,27 +1352,30 @@ mod tests {
13531352 ] ;
13541353
13551354 for ( input, expected) in test_cases {
1356- let captures = algo_based_regex . captures ( input) ;
1355+ let line_info = LineFormat :: parse_algo_based ( input) ;
13571356 match expected {
13581357 Some ( ( algo, bits, filename, checksum) ) => {
1359- assert ! ( captures. is_some( ) ) ;
1360- let captures = captures. unwrap ( ) ;
1361- assert_eq ! ( & captures. name( "algo" ) . unwrap( ) . as_bytes( ) , algo) ;
1362- assert_eq ! ( & captures. name( "bits" ) . map( |m| m. as_bytes( ) ) , bits) ;
1363- assert_eq ! ( & captures. name( "filename" ) . unwrap( ) . as_bytes( ) , filename) ;
1364- assert_eq ! ( & captures. name( "checksum" ) . unwrap( ) . as_bytes( ) , checksum) ;
1358+ assert ! ( line_info. is_some( ) ) ;
1359+ let line_info = line_info. unwrap ( ) ;
1360+ assert_eq ! ( & line_info. algo_name. unwrap( ) . as_bytes( ) , algo) ;
1361+ assert_eq ! (
1362+ line_info
1363+ . algo_bit_len
1364+ . map( |m| m. to_string( ) . as_bytes( ) . to_owned( ) ) ,
1365+ bits. map( |b| b. to_owned( ) )
1366+ ) ;
1367+ assert_eq ! ( & line_info. filename, filename) ;
1368+ assert_eq ! ( & line_info. checksum. as_bytes( ) , checksum) ;
13651369 }
13661370 None => {
1367- assert ! ( captures . is_none( ) ) ;
1371+ assert ! ( line_info . is_none( ) ) ;
13681372 }
13691373 }
13701374 }
13711375 }
13721376
13731377 #[ test]
1374- fn test_double_space_regex ( ) {
1375- let double_space_regex = Regex :: new ( DOUBLE_SPACE_REGEX ) . unwrap ( ) ;
1376-
1378+ fn test_double_space_parser ( ) {
13771379 #[ allow( clippy:: type_complexity) ]
13781380 let test_cases: & [ ( & [ u8 ] , Option < ( & [ u8 ] , & [ u8 ] ) > ) ] = & [
13791381 (
@@ -1400,24 +1402,23 @@ mod tests {
14001402 ] ;
14011403
14021404 for ( input, expected) in test_cases {
1403- let captures = double_space_regex . captures ( input) ;
1405+ let line_info = LineFormat :: parse_untagged ( input) ;
14041406 match expected {
14051407 Some ( ( checksum, filename) ) => {
1406- assert ! ( captures . is_some( ) ) ;
1407- let captures = captures . unwrap ( ) ;
1408- assert_eq ! ( & captures . name ( "checksum" ) . unwrap ( ) . as_bytes ( ) , checksum ) ;
1409- assert_eq ! ( & captures . name ( "filename" ) . unwrap ( ) . as_bytes( ) , filename ) ;
1408+ assert ! ( line_info . is_some( ) ) ;
1409+ let line_info = line_info . unwrap ( ) ;
1410+ assert_eq ! ( & line_info . filename , filename ) ;
1411+ assert_eq ! ( & line_info . checksum . as_bytes( ) , checksum ) ;
14101412 }
14111413 None => {
1412- assert ! ( captures . is_none( ) ) ;
1414+ assert ! ( line_info . is_none( ) ) ;
14131415 }
14141416 }
14151417 }
14161418 }
14171419
14181420 #[ test]
1419- fn test_single_space_regex ( ) {
1420- let single_space_regex = Regex :: new ( SINGLE_SPACE_REGEX ) . unwrap ( ) ;
1421+ fn test_single_space_parser ( ) {
14211422 #[ allow( clippy:: type_complexity) ]
14221423 let test_cases: & [ ( & [ u8 ] , Option < ( & [ u8 ] , & [ u8 ] ) > ) ] = & [
14231424 (
@@ -1440,85 +1441,86 @@ mod tests {
14401441 ] ;
14411442
14421443 for ( input, expected) in test_cases {
1443- let captures = single_space_regex . captures ( input) ;
1444+ let line_info = LineFormat :: parse_single_space ( input) ;
14441445 match expected {
14451446 Some ( ( checksum, filename) ) => {
1446- assert ! ( captures . is_some( ) ) ;
1447- let captures = captures . unwrap ( ) ;
1448- assert_eq ! ( & captures . name ( "checksum" ) . unwrap ( ) . as_bytes ( ) , checksum ) ;
1449- assert_eq ! ( & captures . name ( "filename" ) . unwrap ( ) . as_bytes( ) , filename ) ;
1447+ assert ! ( line_info . is_some( ) ) ;
1448+ let line_info = line_info . unwrap ( ) ;
1449+ assert_eq ! ( & line_info . filename , filename ) ;
1450+ assert_eq ! ( & line_info . checksum . as_bytes( ) , checksum ) ;
14501451 }
14511452 None => {
1452- assert ! ( captures . is_none( ) ) ;
1453+ assert ! ( line_info . is_none( ) ) ;
14531454 }
14541455 }
14551456 }
14561457 }
14571458
14581459 #[ test]
14591460 fn test_line_info ( ) {
1460- let mut cached_regex = None ;
1461+ let mut cached_line_format = None ;
14611462
1462- // Test algo-based regex
1463+ // Test algo-based parser
14631464 let line_algo_based =
14641465 OsString :: from ( "MD5 (example.txt) = d41d8cd98f00b204e9800998ecf8427e" ) ;
1465- let line_info = LineInfo :: parse ( & line_algo_based, & mut cached_regex ) . unwrap ( ) ;
1466+ let line_info = LineInfo :: parse ( & line_algo_based, & mut cached_line_format ) . unwrap ( ) ;
14661467 assert_eq ! ( line_info. algo_name. as_deref( ) , Some ( "MD5" ) ) ;
14671468 assert ! ( line_info. algo_bit_len. is_none( ) ) ;
14681469 assert_eq ! ( line_info. filename, b"example.txt" ) ;
14691470 assert_eq ! ( line_info. checksum, "d41d8cd98f00b204e9800998ecf8427e" ) ;
14701471 assert_eq ! ( line_info. format, LineFormat :: AlgoBased ) ;
1471- assert ! ( cached_regex . is_none( ) ) ;
1472+ assert ! ( cached_line_format . is_none( ) ) ;
14721473
1473- // Test double-space regex
1474+ // Test double-space parser
14741475 let line_double_space = OsString :: from ( "d41d8cd98f00b204e9800998ecf8427e example.txt" ) ;
1475- let line_info = LineInfo :: parse ( & line_double_space, & mut cached_regex ) . unwrap ( ) ;
1476+ let line_info = LineInfo :: parse ( & line_double_space, & mut cached_line_format ) . unwrap ( ) ;
14761477 assert ! ( line_info. algo_name. is_none( ) ) ;
14771478 assert ! ( line_info. algo_bit_len. is_none( ) ) ;
14781479 assert_eq ! ( line_info. filename, b"example.txt" ) ;
14791480 assert_eq ! ( line_info. checksum, "d41d8cd98f00b204e9800998ecf8427e" ) ;
14801481 assert_eq ! ( line_info. format, LineFormat :: Untagged ) ;
1481- assert ! ( cached_regex . is_some( ) ) ;
1482+ assert ! ( cached_line_format . is_some( ) ) ;
14821483
1483- cached_regex = None ;
1484+ cached_line_format = None ;
14841485
1485- // Test single-space regex
1486+ // Test single-space parser
14861487 let line_single_space = OsString :: from ( "d41d8cd98f00b204e9800998ecf8427e example.txt" ) ;
1487- let line_info = LineInfo :: parse ( & line_single_space, & mut cached_regex ) . unwrap ( ) ;
1488+ let line_info = LineInfo :: parse ( & line_single_space, & mut cached_line_format ) . unwrap ( ) ;
14881489 assert ! ( line_info. algo_name. is_none( ) ) ;
14891490 assert ! ( line_info. algo_bit_len. is_none( ) ) ;
14901491 assert_eq ! ( line_info. filename, b"example.txt" ) ;
14911492 assert_eq ! ( line_info. checksum, "d41d8cd98f00b204e9800998ecf8427e" ) ;
14921493 assert_eq ! ( line_info. format, LineFormat :: SingleSpace ) ;
1493- assert ! ( cached_regex . is_some( ) ) ;
1494+ assert ! ( cached_line_format . is_some( ) ) ;
14941495
1495- cached_regex = None ;
1496+ cached_line_format = None ;
14961497
14971498 // Test invalid checksum line
14981499 let line_invalid = OsString :: from ( "invalid checksum line" ) ;
1499- assert ! ( LineInfo :: parse( & line_invalid, & mut cached_regex ) . is_none( ) ) ;
1500- assert ! ( cached_regex . is_none( ) ) ;
1500+ assert ! ( LineInfo :: parse( & line_invalid, & mut cached_line_format ) . is_none( ) ) ;
1501+ assert ! ( cached_line_format . is_none( ) ) ;
15011502
15021503 // Test leading space before checksum line
15031504 let line_algo_based_leading_space =
15041505 OsString :: from ( " MD5 (example.txt) = d41d8cd98f00b204e9800998ecf8427e" ) ;
1505- let line_info = LineInfo :: parse ( & line_algo_based_leading_space, & mut cached_regex) . unwrap ( ) ;
1506+ let line_info =
1507+ LineInfo :: parse ( & line_algo_based_leading_space, & mut cached_line_format) . unwrap ( ) ;
15061508 assert_eq ! ( line_info. format, LineFormat :: AlgoBased ) ;
1507- assert ! ( cached_regex . is_none( ) ) ;
1509+ assert ! ( cached_line_format . is_none( ) ) ;
15081510
15091511 // Test trailing space after checksum line (should fail)
15101512 let line_algo_based_leading_space =
15111513 OsString :: from ( "MD5 (example.txt) = d41d8cd98f00b204e9800998ecf8427e " ) ;
1512- let res = LineInfo :: parse ( & line_algo_based_leading_space, & mut cached_regex ) ;
1514+ let res = LineInfo :: parse ( & line_algo_based_leading_space, & mut cached_line_format ) ;
15131515 assert ! ( res. is_none( ) ) ;
1514- assert ! ( cached_regex . is_none( ) ) ;
1516+ assert ! ( cached_line_format . is_none( ) ) ;
15151517 }
15161518
15171519 #[ test]
15181520 fn test_get_expected_digest ( ) {
15191521 let line = OsString :: from ( "SHA256 (empty) = 47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=" ) ;
1520- let mut cached_regex = None ;
1521- let line_info = LineInfo :: parse ( & line, & mut cached_regex ) . unwrap ( ) ;
1522+ let mut cached_line_format = None ;
1523+ let line_info = LineInfo :: parse ( & line, & mut cached_line_format ) . unwrap ( ) ;
15221524
15231525 let result = get_expected_digest_as_hex_string ( & line_info, None ) ;
15241526
@@ -1532,8 +1534,8 @@ mod tests {
15321534 fn test_get_expected_checksum_invalid ( ) {
15331535 // The line misses a '=' at the end to be valid base64
15341536 let line = OsString :: from ( "SHA256 (empty) = 47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU" ) ;
1535- let mut cached_regex = None ;
1536- let line_info = LineInfo :: parse ( & line, & mut cached_regex ) . unwrap ( ) ;
1537+ let mut cached_line_format = None ;
1538+ let line_info = LineInfo :: parse ( & line, & mut cached_line_format ) . unwrap ( ) ;
15371539
15381540 let result = get_expected_digest_as_hex_string ( & line_info, None ) ;
15391541
0 commit comments