11use crate :: set_panic_hook;
22use brotli:: enc:: encode:: {
3- BrotliEncoderCompressStream , BrotliEncoderCreateInstance , BrotliEncoderDestroyInstance ,
4- BrotliEncoderIsFinished , BrotliEncoderOperation , BrotliEncoderParameter ,
5- BrotliEncoderSetParameter , BrotliEncoderStateStruct ,
3+ BrotliEncoderDestroyInstance , BrotliEncoderOperation , BrotliEncoderParameter ,
4+ BrotliEncoderStateStruct ,
65} ;
76use brotli:: enc:: StandardAlloc ; // Re-exported from alloc_stdlib::StandardAlloc
87use brotli:: { self , BrotliDecompressStream , BrotliResult , BrotliState } ;
@@ -58,15 +57,11 @@ impl CompressStream {
5857 pub fn new ( quality : Option < u32 > ) -> CompressStream {
5958 set_panic_hook ( ) ;
6059 let alloc = StandardAlloc :: default ( ) ;
61- let mut state = BrotliEncoderCreateInstance ( alloc) ;
60+ let mut state = BrotliEncoderStateStruct :: new ( alloc) ;
6261 match quality {
6362 None => ( ) ,
6463 Some ( quality) => {
65- BrotliEncoderSetParameter (
66- & mut state,
67- BrotliEncoderParameter :: BROTLI_PARAM_QUALITY ,
68- quality,
69- ) ;
64+ state. set_parameter ( BrotliEncoderParameter :: BROTLI_PARAM_QUALITY , quality) ;
7065 }
7166 }
7267 Self {
@@ -97,8 +92,7 @@ impl CompressStream {
9792 // `BrotliEncoderCompressStream` does not return a `BrotliResult` but returns a boolean,
9893 // which is different from `BrotliDecompressStream`.
9994 // But the requirement for input/output buf is common so we reused `BrotliStreamResult` to report it.
100- let ret = BrotliEncoderCompressStream (
101- & mut self . state ,
95+ if self . state . compress_stream (
10296 op,
10397 & mut available_in,
10498 & input,
@@ -108,8 +102,7 @@ impl CompressStream {
108102 & mut output_offset,
109103 & mut Some ( self . total_out ) ,
110104 & mut nop_callback,
111- ) ;
112- if ret != 0 {
105+ ) {
113106 if available_in == 0 {
114107 output. truncate ( output_offset) ;
115108 Ok ( BrotliStreamResult {
@@ -136,9 +129,8 @@ impl CompressStream {
136129 let op = BrotliEncoderOperation :: BROTLI_OPERATION_FINISH ;
137130 let input = Vec :: new ( ) . into_boxed_slice ( ) ;
138131 let mut available_in = 0 ;
139- while BrotliEncoderIsFinished ( & mut self . state ) == 0 && available_out > 0 {
140- let ret = BrotliEncoderCompressStream (
141- & mut self . state ,
132+ while !self . state . is_finished ( ) && available_out > 0 {
133+ if !self . state . compress_stream (
142134 op,
143135 & mut available_in,
144136 & input,
@@ -148,8 +140,7 @@ impl CompressStream {
148140 & mut output_offset,
149141 & mut Some ( self . total_out ) ,
150142 & mut nop_callback,
151- ) ;
152- if ret == 0 {
143+ ) {
153144 return Err ( JsError :: new (
154145 "Brotli streaming compress failed: When finishing" ,
155146 ) ) ;
0 commit comments