@@ -20,10 +20,7 @@ register_module!(test_module, init);
20
20
21
21
static JIEBA : OnceCell < Jieba > = OnceCell :: new ( ) ;
22
22
23
- fn init < ' env > (
24
- env : & ' env Env ,
25
- exports : & ' env mut Value < ' env , Object > ,
26
- ) -> Result < Option < Value < ' env , Object > > > {
23
+ fn init ( env : & Env , exports : & mut Value < Object > ) -> Result < ( ) > {
27
24
exports. set_property (
28
25
env. create_string ( "load" ) ?,
29
26
env. create_function ( "load" , load) ?,
@@ -53,19 +50,19 @@ fn init<'env>(
53
50
env. create_function ( "extract" , extract) ?,
54
51
) ?;
55
52
56
- Ok ( None )
53
+ Ok ( ( ) )
57
54
}
58
55
59
56
#[ js_function]
60
57
fn load ( ctx : CallContext ) -> Result < Value < Undefined > > {
61
- assert_not_init ( ctx. env ) ?;
58
+ assert_not_init ( & ctx. env ) ?;
62
59
let _ = JIEBA . get_or_init ( || Jieba :: new ( ) ) ;
63
60
ctx. env . get_undefined ( )
64
61
}
65
62
66
63
#[ js_function( 1 ) ]
67
64
fn load_dict ( ctx : CallContext ) -> Result < Value < Undefined > > {
68
- assert_not_init ( ctx. env ) ?;
65
+ assert_not_init ( & ctx. env ) ?;
69
66
let dict = ctx. get :: < Buffer > ( 0 ) ?;
70
67
let mut readable_dict: & [ u8 ] = & dict;
71
68
JIEBA . get_or_init ( || {
@@ -98,7 +95,7 @@ fn cut(ctx: CallContext) -> Result<Value<JsString>> {
98
95
. or_else ( |_| ctx. env . get_boolean ( false ) ) ?;
99
96
let jieba = JIEBA . get_or_init ( || Jieba :: new ( ) ) ;
100
97
let cutted = jieba. cut (
101
- str:: from_utf8 ( & sentence) . map_err ( |_| Error :: new ( Status :: InvalidArg ) ) ?,
98
+ str:: from_utf8 ( & sentence) . map_err ( |_| Error :: from_status ( Status :: InvalidArg ) ) ?,
102
99
hmm. get_value ( ) ,
103
100
) ;
104
101
@@ -112,7 +109,7 @@ fn cut_all(ctx: CallContext) -> Result<Value<JsString>> {
112
109
let sentence = ctx. get :: < Buffer > ( 0 ) ?;
113
110
let jieba = JIEBA . get_or_init ( || Jieba :: new ( ) ) ;
114
111
let cutted =
115
- jieba. cut_all ( str:: from_utf8 ( & sentence) . map_err ( |_| Error :: new ( Status :: InvalidArg ) ) ?) ;
112
+ jieba. cut_all ( str:: from_utf8 ( & sentence) . map_err ( |_| Error :: from_status ( Status :: InvalidArg ) ) ?) ;
116
113
117
114
let output = cutted. join ( "," ) ;
118
115
@@ -127,7 +124,7 @@ fn cut_for_search(ctx: CallContext) -> Result<Value<JsString>> {
127
124
. or_else ( |_| ctx. env . get_boolean ( false ) ) ?;
128
125
let jieba = JIEBA . get_or_init ( || Jieba :: new ( ) ) ;
129
126
let cutted = jieba. cut_for_search (
130
- str:: from_utf8 ( & sentence) . map_err ( |_| Error :: new ( Status :: InvalidArg ) ) ?,
127
+ str:: from_utf8 ( & sentence) . map_err ( |_| Error :: from_status ( Status :: InvalidArg ) ) ?,
131
128
hmm. get_value ( ) ,
132
129
) ;
133
130
@@ -144,7 +141,7 @@ fn tag(ctx: CallContext) -> Result<Value<JsString>> {
144
141
. or_else ( |_| ctx. env . get_boolean ( false ) ) ?;
145
142
let jieba = JIEBA . get_or_init ( || Jieba :: new ( ) ) ;
146
143
let tagged = jieba. tag (
147
- str:: from_utf8 ( & sentence) . map_err ( |_| Error :: new ( Status :: InvalidArg ) ) ?,
144
+ str:: from_utf8 ( & sentence) . map_err ( |_| Error :: from_status ( Status :: InvalidArg ) ) ?,
148
145
hmm. get_value ( ) ,
149
146
) ;
150
147
@@ -180,7 +177,7 @@ fn extract(ctx: CallContext) -> Result<Value<JsString>> {
180
177
let topn: usize = topn. try_into ( ) ?;
181
178
182
179
let tags = keyword_extractor. extract_tags (
183
- str:: from_utf8 ( & sentence) . map_err ( |_| Error :: new ( Status :: InvalidArg ) ) ?,
180
+ str:: from_utf8 ( & sentence) . map_err ( |_| Error :: from_status ( Status :: InvalidArg ) ) ?,
184
181
topn,
185
182
allowed_pos,
186
183
) ;
0 commit comments