@@ -4,67 +4,47 @@ use bzip2::Compression;
4
4
use serde:: { Deserialize , Serialize } ;
5
5
use std:: {
6
6
collections:: HashSet ,
7
- fmt,
8
7
io:: { self , Read } ,
9
8
} ;
9
+ use strum:: { Display , EnumIter , EnumString , FromRepr } ;
10
10
11
11
pub type CompressionAlgorithms = HashSet < CompressionAlgorithm > ;
12
12
13
- macro_rules! enum_id {
14
- ( $vis: vis enum $name: ident { $( $variant: ident = $discriminant: expr, ) * } ) => {
15
- #[ derive( Copy , Clone , Debug , PartialEq , Eq , Hash , Serialize , Deserialize ) ]
16
- $vis enum $name {
17
- $( $variant = $discriminant, ) *
18
- }
19
-
20
- impl $name {
21
- #[ cfg( test) ]
22
- const AVAILABLE : & ' static [ Self ] = & [ $( Self :: $variant, ) * ] ;
23
- }
24
-
25
- impl fmt:: Display for CompressionAlgorithm {
26
- fn fmt( & self , f: & mut fmt:: Formatter <' _>) -> fmt:: Result {
27
- match self {
28
- $( Self :: $variant => write!( f, stringify!( $variant) ) , ) *
29
- }
30
- }
31
- }
32
-
33
- impl std:: str :: FromStr for CompressionAlgorithm {
34
- type Err = ( ) ;
35
- fn from_str( s: & str ) -> Result <Self , Self :: Err > {
36
- match s {
37
- $( stringify!( $variant) => Ok ( Self :: $variant) , ) *
38
- _ => Err ( ( ) ) ,
39
- }
40
- }
41
- }
13
+ #[ derive(
14
+ Copy ,
15
+ Clone ,
16
+ Debug ,
17
+ PartialEq ,
18
+ Eq ,
19
+ Hash ,
20
+ Serialize ,
21
+ Deserialize ,
22
+ Default ,
23
+ EnumString ,
24
+ Display ,
25
+ FromRepr ,
26
+ EnumIter ,
27
+ ) ]
28
+ pub enum CompressionAlgorithm {
29
+ #[ default]
30
+ Zstd = 0 ,
31
+ Bzip2 = 1 ,
32
+ }
42
33
43
- impl std:: convert:: TryFrom <i32 > for CompressionAlgorithm {
44
- type Error = i32 ;
45
- fn try_from( i: i32 ) -> Result <Self , Self :: Error > {
46
- match i {
47
- $ ( $discriminant => Ok ( Self :: $variant ) , ) *
48
- _ => Err ( i ) ,
49
- }
34
+ impl std:: convert:: TryFrom < i32 > for CompressionAlgorithm {
35
+ type Error = i32 ;
36
+ fn try_from ( i : i32 ) -> Result < Self , Self :: Error > {
37
+ if i >= 0 {
38
+ match Self :: from_repr ( i as usize ) {
39
+ Some ( alg ) => Ok ( alg ) ,
40
+ None => Err ( i ) ,
50
41
}
42
+ } else {
43
+ Err ( i)
51
44
}
52
45
}
53
46
}
54
47
55
- enum_id ! {
56
- pub enum CompressionAlgorithm {
57
- Zstd = 0 ,
58
- Bzip2 = 1 ,
59
- }
60
- }
61
-
62
- impl Default for CompressionAlgorithm {
63
- fn default ( ) -> Self {
64
- CompressionAlgorithm :: Zstd
65
- }
66
- }
67
-
68
48
// public for benchmarking
69
49
pub fn compress ( content : impl Read , algorithm : CompressionAlgorithm ) -> Result < Vec < u8 > , Error > {
70
50
match algorithm {
@@ -100,16 +80,17 @@ pub fn decompress(
100
80
#[ cfg( test) ]
101
81
mod tests {
102
82
use super :: * ;
83
+ use strum:: IntoEnumIterator ;
103
84
104
85
#[ test]
105
86
fn test_compression ( ) {
106
87
let orig = "fn main() {}" ;
107
- for alg in CompressionAlgorithm :: AVAILABLE {
88
+ for alg in CompressionAlgorithm :: iter ( ) {
108
89
println ! ( "testing algorithm {alg}" ) ;
109
90
110
- let data = compress ( orig. as_bytes ( ) , * alg) . unwrap ( ) ;
91
+ let data = compress ( orig. as_bytes ( ) , alg) . unwrap ( ) ;
111
92
assert_eq ! (
112
- decompress( data. as_slice( ) , * alg, std:: usize :: MAX ) . unwrap( ) ,
93
+ decompress( data. as_slice( ) , alg, std:: usize :: MAX ) . unwrap( ) ,
113
94
orig. as_bytes( )
114
95
) ;
115
96
}
@@ -123,7 +104,7 @@ mod tests {
123
104
let exact = & [ b'A' ; MAX_SIZE ] as & [ u8 ] ;
124
105
let big = & [ b'A' ; MAX_SIZE * 2 ] as & [ u8 ] ;
125
106
126
- for & alg in CompressionAlgorithm :: AVAILABLE {
107
+ for alg in CompressionAlgorithm :: iter ( ) {
127
108
let compressed_small = compress ( small, alg) . unwrap ( ) ;
128
109
let compressed_exact = compress ( exact, alg) . unwrap ( ) ;
129
110
let compressed_big = compress ( big, alg) . unwrap ( ) ;
@@ -151,4 +132,10 @@ mod tests {
151
132
. is_some( ) ) ;
152
133
}
153
134
}
135
+
136
+ #[ test]
137
+ fn test_enum_display ( ) {
138
+ assert_eq ! ( CompressionAlgorithm :: Zstd . to_string( ) , "Zstd" ) ;
139
+ assert_eq ! ( CompressionAlgorithm :: Bzip2 . to_string( ) , "Bzip2" ) ;
140
+ }
154
141
}
0 commit comments