1
- use std:: { borrow:: Cow , collections:: HashMap , path:: Path } ;
1
+ use std:: { borrow:: Cow , cmp :: Ordering , collections:: HashMap , path:: Path } ;
2
2
3
3
use ahash:: AHashMap ;
4
4
use aho_corasick:: AhoCorasick ;
@@ -9,6 +9,88 @@ use tracing::debug;
9
9
10
10
use crate :: { db:: RefreshError , inrelease:: ChecksumItem } ;
11
11
12
+ #[ derive( Debug , Eq , PartialEq ) ]
13
+ struct CompressFileWrapper {
14
+ compress_file : CompressFile ,
15
+ }
16
+
17
+ impl From < & str > for CompressFileWrapper {
18
+ fn from ( value : & str ) -> Self {
19
+ match value {
20
+ "xz" => CompressFileWrapper {
21
+ compress_file : CompressFile :: Xz ,
22
+ } ,
23
+ "bz2" => CompressFileWrapper {
24
+ compress_file : CompressFile :: Bz2 ,
25
+ } ,
26
+ "lzma" => CompressFileWrapper {
27
+ compress_file : CompressFile :: Lzma ,
28
+ } ,
29
+ "gz" => CompressFileWrapper {
30
+ compress_file : CompressFile :: Gzip ,
31
+ } ,
32
+ "lz4" => CompressFileWrapper {
33
+ compress_file : CompressFile :: Lz4 ,
34
+ } ,
35
+ "zst" => CompressFileWrapper {
36
+ compress_file : CompressFile :: Zstd ,
37
+ } ,
38
+ x => {
39
+ debug ! ( "{x} format is not compress format" ) ;
40
+ CompressFileWrapper {
41
+ compress_file : CompressFile :: Nothing ,
42
+ }
43
+ }
44
+ }
45
+ }
46
+ }
47
+
48
+ impl PartialOrd for CompressFileWrapper {
49
+ fn partial_cmp ( & self , other : & Self ) -> Option < Ordering > {
50
+ Some ( self . cmp ( other) )
51
+ }
52
+ }
53
+
54
+ #[ cfg( feature = "apt" ) ]
55
+ impl Ord for CompressFileWrapper {
56
+ fn cmp ( & self , other : & Self ) -> Ordering {
57
+ let config = Config :: new ( ) ;
58
+ let t = config
59
+ . get_compression_types ( )
60
+ . iter ( )
61
+ . map ( |t| CompressFileWrapper :: from ( t. as_str ( ) ) )
62
+ . collect :: < Vec < _ > > ( ) ;
63
+
64
+ let self_pos = t. iter ( ) . position ( |x| x == self ) . unwrap ( ) ;
65
+ let other_pos = t. iter ( ) . position ( |x| x == other) . unwrap ( ) ;
66
+
67
+ other_pos. cmp ( & self_pos)
68
+ }
69
+ }
70
+
71
+ #[ cfg( not( feature = "apt" ) ) ]
72
+ impl Ord for CompressFileWrapper {
73
+ fn cmp ( & self , other : & Self ) -> Ordering {
74
+ let t = vec ! [ "zst" , "xz" , "bz2" , "lzma" , "gz" , "lz4" ]
75
+ . into_iter ( )
76
+ . map ( CompressFileWrapper :: from)
77
+ . collect :: < Vec < _ > > ( ) ;
78
+
79
+ let self_pos = t. iter ( ) . position ( |x| x == self ) . unwrap ( ) ;
80
+ let other_pos = t. iter ( ) . position ( |x| x == other) . unwrap ( ) ;
81
+
82
+ other_pos. cmp ( & self_pos)
83
+ }
84
+ }
85
+
86
+ impl From < CompressFile > for CompressFileWrapper {
87
+ fn from ( value : CompressFile ) -> Self {
88
+ Self {
89
+ compress_file : value,
90
+ }
91
+ }
92
+ }
93
+
12
94
#[ cfg( feature = "apt" ) ]
13
95
fn modify_result (
14
96
tree : ConfigTree ,
@@ -232,7 +314,7 @@ fn flat_repo_template_match(
232
314
}
233
315
234
316
fn uncompress_file_name ( target : & str ) -> Cow < ' _ , str > {
235
- if compress_file ( target) == CompressFile :: Nothing {
317
+ if compress_file ( target) == CompressFile :: Nothing . into ( ) {
236
318
Cow :: Borrowed ( target)
237
319
} else {
238
320
let compress_target_without_ext = Path :: new ( target) . with_extension ( "" ) ;
@@ -287,15 +369,31 @@ fn get_matches_language(locales: impl IntoIterator<Item = String>) -> Vec<String
287
369
langs
288
370
}
289
371
290
- fn compress_file ( name : & str ) -> CompressFile {
291
- CompressFile :: from (
292
- Path :: new ( name)
293
- . extension ( )
294
- . map ( |x| x. to_string_lossy ( ) )
295
- . unwrap_or_default ( )
296
- . to_string ( )
297
- . as_str ( ) ,
298
- )
372
+ fn compress_file ( name : & str ) -> CompressFileWrapper {
373
+ CompressFileWrapper {
374
+ compress_file : CompressFile :: from (
375
+ Path :: new ( name)
376
+ . extension ( )
377
+ . map ( |x| x. to_string_lossy ( ) )
378
+ . unwrap_or_default ( )
379
+ . to_string ( )
380
+ . as_str ( ) ,
381
+ ) ,
382
+ }
383
+ }
384
+
385
+ #[ cfg( feature = "apt" ) ]
386
+ #[ test]
387
+ fn test_compression_order ( ) {
388
+ let mut t = Config :: new ( )
389
+ . get_compression_types ( )
390
+ . iter ( )
391
+ . map ( |t| CompressFileWrapper :: from ( t. as_str ( ) ) )
392
+ . collect :: < Vec < _ > > ( ) ;
393
+
394
+ t. sort ( ) ;
395
+
396
+ dbg ! ( t) ;
299
397
}
300
398
301
399
#[ test]
0 commit comments