@@ -5,26 +5,28 @@ extern crate global_alloc;
5
5
6
6
use crc32c:: crc32c_append;
7
7
use crc32fast:: Hasher ;
8
- use napi:: bindgen_prelude:: { Buffer , Either } ;
8
+ use napi:: { bindgen_prelude:: Either , JsBuffer , Result } ;
9
9
use napi_derive:: * ;
10
10
11
11
#[ napi( js_name = "crc32c" ) ]
12
- pub fn crc32c ( input : Either < String , Buffer > , initial_state : Option < u32 > ) -> u32 {
13
- crc32c_append (
14
- initial_state. unwrap_or ( 0 ) ,
15
- match & input {
16
- Either :: A ( s) => s. as_bytes ( ) ,
17
- Either :: B ( b) => b. as_ref ( ) ,
18
- } ,
19
- )
12
+ pub fn crc32c ( input : Either < String , JsBuffer > , initial_state : Option < u32 > ) -> Result < u32 > {
13
+ Ok ( match input {
14
+ Either :: A ( s) => crc32c_append ( initial_state. unwrap_or ( 0 ) , s. as_bytes ( ) ) ,
15
+ Either :: B ( b) => crc32c_append ( initial_state. unwrap_or ( 0 ) , & b. into_value ( ) ?) ,
16
+ } )
20
17
}
21
18
22
19
#[ napi]
23
- pub fn crc32 ( input : Either < String , Buffer > , initial_state : Option < u32 > ) -> u32 {
20
+ pub fn crc32 ( input : Either < String , JsBuffer > , initial_state : Option < u32 > ) -> Result < u32 > {
24
21
let mut hasher = Hasher :: new_with_initial ( initial_state. unwrap_or ( 0 ) ) ;
25
- hasher. update ( match & input {
26
- Either :: A ( s) => s. as_bytes ( ) ,
27
- Either :: B ( b) => b. as_ref ( ) ,
28
- } ) ;
29
- hasher. finalize ( )
22
+ match input {
23
+ Either :: A ( s) => {
24
+ hasher. update ( s. as_bytes ( ) ) ;
25
+ }
26
+ Either :: B ( b) => {
27
+ let b = b. into_value ( ) ?;
28
+ hasher. update ( & b) ;
29
+ }
30
+ } ;
31
+ Ok ( hasher. finalize ( ) )
30
32
}
0 commit comments