22
33use std:: io:: Read ;
44
5- #[ cfg( feature = "zstd" ) ]
6- use ruzstd as _;
5+ #[ cfg( not( any( feature = "zstd" , feature = "ruzstd" ) ) ) ]
6+ compile_error ! ( "You must enable exactly one of the `zstd` or `ruzstd` features" ) ;
7+ #[ cfg( all( feature = "zstd" , feature = "ruzstd" ) ) ]
8+ compile_error ! ( "Features `zstd` and `ruzstd` are mutually exclusive" ) ;
79
810/// The ZSTD magic number for zstd compressed data header.
911const ZSTD_MAGIC_NUMBER : [ u8 ; 4 ] = [ 0x28 , 0xb5 , 0x2f , 0xfd ] ;
1012
13+ /// Result type for Zstd operations.
14+ type Result < T > = std:: result:: Result < T , ZstdError > ;
15+
16+ /// Zstd error type.
17+ #[ derive( Debug ) ]
18+ pub struct ZstdError ( Box < dyn std:: error:: Error + Send + Sync > ) ;
19+
20+ impl std:: fmt:: Display for ZstdError {
21+ fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
22+ write ! ( f, "ZstdError: {}" , self . 0 )
23+ }
24+ }
25+
26+ impl std:: error:: Error for ZstdError { }
27+
28+ impl ZstdError {
29+ /// Consumes the error and returns the inner error.
30+ pub fn into_inner ( self ) -> Box < dyn std:: error:: Error + Send + Sync > {
31+ self . 0
32+ }
33+ }
34+
1135/// Uncompress the provided data.
1236#[ cfg( feature = "zstd" ) ]
13- pub fn decompress_blob_data ( data : & [ u8 ] ) -> Vec < u8 > {
37+ pub fn decompress_blob_data ( data : & [ u8 ] ) -> Result < Vec < u8 > > {
1438 use zstd:: Decoder ;
1539 let mut header_data = ZSTD_MAGIC_NUMBER . to_vec ( ) ;
1640
1741 header_data. extend_from_slice ( data) ;
1842
1943 // init decoder and owned output data.
20- let mut decoder = Decoder :: new ( header_data. as_slice ( ) ) . unwrap ( ) ;
44+ let mut decoder = Decoder :: new ( header_data. as_slice ( ) ) . map_err ( |e| ZstdError ( Box :: new ( e ) ) ) ? ;
2145 // heuristic: use data length as the allocated output capacity.
2246 let mut output = Vec :: with_capacity ( header_data. len ( ) ) ;
2347
@@ -33,22 +57,23 @@ pub fn decompress_blob_data(data: &[u8]) -> Vec<u8> {
3357 output. extend_from_slice ( & dst[ ..size] ) ;
3458 }
3559
36- output
60+ Ok ( output)
3761}
3862
3963/// Uncompress the provided data.
40- #[ cfg( not ( feature = "zstd" ) ) ]
41- pub fn decompress_blob_data ( data : & [ u8 ] ) -> Vec < u8 > {
64+ #[ cfg( feature = "ruzstd" ) ]
65+ pub fn decompress_blob_data ( data : & [ u8 ] ) -> Result < Vec < u8 > > {
4266 use ruzstd:: decoding:: StreamingDecoder ;
4367
4468 let mut header_data = ZSTD_MAGIC_NUMBER . to_vec ( ) ;
4569 header_data. extend_from_slice ( data) ;
4670
4771 // init decoder and owned output data.
48- let mut decoder = StreamingDecoder :: new ( header_data. as_slice ( ) ) . unwrap ( ) ;
72+ let mut decoder =
73+ StreamingDecoder :: new ( header_data. as_slice ( ) ) . map_err ( |e| ZstdError ( Box :: new ( e) ) ) ?;
4974 // heuristic: use data length as the allocated output capacity.
5075 let mut output = Vec :: with_capacity ( header_data. len ( ) ) ;
51- decoder. read_to_end ( & mut output) . unwrap ( ) ;
76+ decoder. read_to_end ( & mut output) . map_err ( |e| ZstdError ( Box :: new ( e ) ) ) ? ;
5277
53- output
78+ Ok ( output)
5479}
0 commit comments