@@ -4,67 +4,47 @@ use bzip2::Compression;
44use serde:: { Deserialize , Serialize } ;
55use std:: {
66 collections:: HashSet ,
7- fmt,
87 io:: { self , Read } ,
98} ;
9+ use strum:: { Display , EnumIter , EnumString , FromRepr } ;
1010
1111pub type CompressionAlgorithms = HashSet < CompressionAlgorithm > ;
1212
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+ }
4233
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 ) ,
5041 }
42+ } else {
43+ Err ( i)
5144 }
5245 }
5346}
5447
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-
6848// public for benchmarking
6949pub fn compress ( content : impl Read , algorithm : CompressionAlgorithm ) -> Result < Vec < u8 > , Error > {
7050 match algorithm {
@@ -100,16 +80,17 @@ pub fn decompress(
10080#[ cfg( test) ]
10181mod tests {
10282 use super :: * ;
83+ use strum:: IntoEnumIterator ;
10384
10485 #[ test]
10586 fn test_compression ( ) {
10687 let orig = "fn main() {}" ;
107- for alg in CompressionAlgorithm :: AVAILABLE {
88+ for alg in CompressionAlgorithm :: iter ( ) {
10889 println ! ( "testing algorithm {alg}" ) ;
10990
110- let data = compress ( orig. as_bytes ( ) , * alg) . unwrap ( ) ;
91+ let data = compress ( orig. as_bytes ( ) , alg) . unwrap ( ) ;
11192 assert_eq ! (
112- decompress( data. as_slice( ) , * alg, std:: usize :: MAX ) . unwrap( ) ,
93+ decompress( data. as_slice( ) , alg, std:: usize :: MAX ) . unwrap( ) ,
11394 orig. as_bytes( )
11495 ) ;
11596 }
@@ -123,7 +104,7 @@ mod tests {
123104 let exact = & [ b'A' ; MAX_SIZE ] as & [ u8 ] ;
124105 let big = & [ b'A' ; MAX_SIZE * 2 ] as & [ u8 ] ;
125106
126- for & alg in CompressionAlgorithm :: AVAILABLE {
107+ for alg in CompressionAlgorithm :: iter ( ) {
127108 let compressed_small = compress ( small, alg) . unwrap ( ) ;
128109 let compressed_exact = compress ( exact, alg) . unwrap ( ) ;
129110 let compressed_big = compress ( big, alg) . unwrap ( ) ;
@@ -151,4 +132,10 @@ mod tests {
151132 . is_some( ) ) ;
152133 }
153134 }
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+ }
154141}
0 commit comments