@@ -5,8 +5,9 @@ mod zlib {
5
5
use crate :: vm:: {
6
6
builtins:: { PyBaseExceptionRef , PyBytes , PyBytesRef , PyIntRef , PyTypeRef } ,
7
7
common:: lock:: PyMutex ,
8
+ convert:: TryFromBorrowedObject ,
8
9
function:: { ArgBytesLike , ArgPrimitiveIndex , ArgSize , OptionalArg } ,
9
- PyPayload , PyResult , VirtualMachine ,
10
+ PyObject , PyPayload , PyResult , VirtualMachine ,
10
11
} ;
11
12
use adler32:: RollingAdler32 as Adler32 ;
12
13
use crossbeam_utils:: atomic:: AtomicCell ;
@@ -78,23 +79,12 @@ mod zlib {
78
79
crate :: binascii:: crc32 ( data, begin_state)
79
80
}
80
81
81
- // TODO: rewrite with TryFromBorrowedObject
82
- fn compression_from_int ( level : i32 ) -> Option < Compression > {
83
- match level {
84
- Z_DEFAULT_COMPRESSION => Some ( Compression :: default ( ) ) ,
85
- valid_level @ Z_NO_COMPRESSION ..=Z_BEST_COMPRESSION => {
86
- Some ( Compression :: new ( valid_level as u32 ) )
87
- }
88
- _ => None ,
89
- }
90
- }
91
-
92
82
#[ derive( FromArgs ) ]
93
83
struct PyFuncCompressArgs {
94
84
#[ pyarg( positional) ]
95
85
data : ArgBytesLike ,
96
- #[ pyarg( any, default = "Z_DEFAULT_COMPRESSION" ) ]
97
- level : i32 ,
86
+ #[ pyarg( any, default = "Level::new( Z_DEFAULT_COMPRESSION) " ) ]
87
+ level : Level ,
98
88
#[ pyarg( any, default = "ArgPrimitiveIndex { value: MAX_WBITS }" ) ]
99
89
wbits : ArgPrimitiveIndex < i8 > ,
100
90
}
@@ -107,9 +97,7 @@ mod zlib {
107
97
level,
108
98
ref wbits,
109
99
} = args;
110
-
111
- let level = compression_from_int ( level)
112
- . ok_or_else ( || new_zlib_error ( "Bad compression level" , vm) ) ?;
100
+ let level = level. ok_or_else ( || new_zlib_error ( "Bad compression level" , vm) ) ?;
113
101
114
102
let encoded_bytes = if args. wbits . value == MAX_WBITS {
115
103
let mut encoder = ZlibEncoder :: new ( Vec :: new ( ) , level) ;
@@ -431,8 +419,8 @@ mod zlib {
431
419
#[ derive( FromArgs ) ]
432
420
#[ allow( dead_code) ] // FIXME: use args
433
421
struct CompressobjArgs {
434
- #[ pyarg( any, default = "Z_DEFAULT_COMPRESSION" ) ]
435
- level : i32 ,
422
+ #[ pyarg( any, default = "Level::new( Z_DEFAULT_COMPRESSION) " ) ]
423
+ level : Level ,
436
424
// only DEFLATED is valid right now, it's w/e
437
425
#[ pyarg( any, default = "DEFLATED" ) ]
438
426
_method : i32 ,
@@ -457,8 +445,8 @@ mod zlib {
457
445
zdict,
458
446
..
459
447
} = args;
460
- let level = compression_from_int ( level )
461
- . ok_or_else ( || vm. new_value_error ( "invalid initialization option" . to_owned ( ) ) ) ?;
448
+ let level =
449
+ level . ok_or_else ( || vm. new_value_error ( "invalid initialization option" . to_owned ( ) ) ) ?;
462
450
#[ allow( unused_mut) ]
463
451
let mut compress = InitOptions :: new ( wbits. value , vm) ?. compress ( level) ;
464
452
#[ cfg( feature = "zlib" ) ]
@@ -574,4 +562,32 @@ mod zlib {
574
562
fn new_zlib_error ( message : & str , vm : & VirtualMachine ) -> PyBaseExceptionRef {
575
563
vm. new_exception_msg ( vm. class ( "zlib" , "error" ) , message. to_owned ( ) )
576
564
}
565
+
566
+ struct Level ( Option < flate2:: Compression > ) ;
567
+
568
+ impl Level {
569
+ fn new ( level : i32 ) -> Self {
570
+ let compression = match level {
571
+ Z_DEFAULT_COMPRESSION => Compression :: default ( ) ,
572
+ valid_level @ Z_NO_COMPRESSION ..=Z_BEST_COMPRESSION => {
573
+ Compression :: new ( valid_level as u32 )
574
+ }
575
+ _ => return Self ( None ) ,
576
+ } ;
577
+ Self ( Some ( compression) )
578
+ }
579
+ fn ok_or_else (
580
+ self ,
581
+ f : impl FnOnce ( ) -> PyBaseExceptionRef ,
582
+ ) -> PyResult < flate2:: Compression > {
583
+ self . 0 . ok_or_else ( f)
584
+ }
585
+ }
586
+
587
+ impl TryFromBorrowedObject for Level {
588
+ fn try_from_borrowed_object ( vm : & VirtualMachine , obj : & PyObject ) -> PyResult < Self > {
589
+ let int: i32 = obj. try_index ( vm) ?. try_to_primitive ( vm) ?;
590
+ Ok ( Self :: new ( int) )
591
+ }
592
+ }
577
593
}
0 commit comments