@@ -29,7 +29,13 @@ pub enum Algorithm {
29
29
30
30
impl fmt:: Display for Algorithm {
31
31
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
32
- write ! ( f, "{:?}" , self )
32
+ let output = match self {
33
+ Algorithm :: None => "none" ,
34
+ Algorithm :: Lz4Block => "lz4_block" ,
35
+ Algorithm :: GZip => "gzip" ,
36
+ Algorithm :: Zstd => "zstd" ,
37
+ } ;
38
+ write ! ( f, "{}" , output)
33
39
}
34
40
}
35
41
@@ -250,6 +256,7 @@ mod tests {
250
256
use std:: fs:: OpenOptions ;
251
257
use std:: io:: { Seek , SeekFrom } ;
252
258
use std:: path:: Path ;
259
+ use std:: str:: FromStr ;
253
260
use vmm_sys_util:: tempfile:: TempFile ;
254
261
255
262
#[ test]
@@ -593,4 +600,46 @@ mod tests {
593
600
assert ! ( !Algorithm :: GZip . is_none( ) ) ;
594
601
assert ! ( !Algorithm :: Zstd . is_none( ) ) ;
595
602
}
603
+
604
+ #[ test]
605
+ fn test_algorithm_to_string ( ) {
606
+ assert_eq ! ( Algorithm :: None . to_string( ) , "none" ) ;
607
+ assert_eq ! ( Algorithm :: Lz4Block . to_string( ) , "lz4_block" ) ;
608
+ assert_eq ! ( Algorithm :: GZip . to_string( ) , "gzip" ) ;
609
+ assert_eq ! ( Algorithm :: Zstd . to_string( ) , "zstd" ) ;
610
+ }
611
+
612
+ #[ test]
613
+ fn test_algorithm_from_str ( ) {
614
+ assert_eq ! ( Algorithm :: from_str( "none" ) . unwrap( ) , Algorithm :: None ) ;
615
+ assert_eq ! (
616
+ Algorithm :: from_str( "lz4_block" ) . unwrap( ) ,
617
+ Algorithm :: Lz4Block
618
+ ) ;
619
+ assert_eq ! ( Algorithm :: from_str( "gzip" ) . unwrap( ) , Algorithm :: GZip ) ;
620
+ assert_eq ! ( Algorithm :: from_str( "zstd" ) . unwrap( ) , Algorithm :: Zstd ) ;
621
+ }
622
+
623
+ #[ test]
624
+ fn test_algorithm_to_string_and_from_str_consistency ( ) {
625
+ let algorithms = vec ! [
626
+ Algorithm :: None ,
627
+ Algorithm :: Lz4Block ,
628
+ Algorithm :: GZip ,
629
+ Algorithm :: Zstd ,
630
+ ] ;
631
+
632
+ for algo in algorithms {
633
+ let stringified = algo. to_string ( ) ;
634
+ let parsed = Algorithm :: from_str ( & stringified) . unwrap ( ) ;
635
+ assert_eq ! ( algo, parsed, "Mismatch for algorithm: {:?}" , algo) ;
636
+ }
637
+ }
638
+
639
+ #[ test]
640
+ fn test_algorithm_from_str_invalid_input ( ) {
641
+ assert ! ( Algorithm :: from_str( "invalid_algorithm" ) . is_err( ) ) ;
642
+ assert ! ( Algorithm :: from_str( "GZIP" ) . is_err( ) ) ;
643
+ assert ! ( Algorithm :: from_str( "LZ4_BLOCK" ) . is_err( ) ) ;
644
+ }
596
645
}
0 commit comments