@@ -26,12 +26,18 @@ use jstz_core::{
26
26
} ;
27
27
use urlpattern:: quirks:: StringOrInit as InnerStringOrQuirksInit ;
28
28
use urlpattern:: quirks:: UrlPatternInit as InnerUrlPatternQuirksInit ;
29
- use urlpattern:: Error ;
30
29
use urlpattern:: UrlPattern as InnerUrlPattern ;
31
30
use urlpattern:: UrlPatternComponentResult as InnerUrlPatternComponentResult ;
32
31
use urlpattern:: UrlPatternResult as InnerUrlPatternResult ;
33
32
34
33
pub struct UrlPatternInput ( InnerStringOrQuirksInit ) ;
34
+ impl Default for UrlPatternInput {
35
+ fn default ( ) -> Self {
36
+ Self ( InnerStringOrQuirksInit :: Init (
37
+ InnerUrlPatternQuirksInit :: default ( ) ,
38
+ ) )
39
+ }
40
+ }
35
41
#[ derive( Default ) ]
36
42
pub struct UrlPatternInit ( InnerUrlPatternQuirksInit ) ;
37
43
@@ -70,13 +76,13 @@ impl UrlPattern {
70
76
urlpattern:: UrlPatternMatchInput ,
71
77
( InnerStringOrQuirksInit , Option < String > ) ,
72
78
) > {
73
- match
74
- urlpattern:: quirks:: process_match_input ( string_or_init, base_url. as_deref ( ) )
79
+ match urlpattern:: quirks:: process_match_input ( string_or_init, base_url. as_deref ( ) )
75
80
{
76
- Err ( Error :: BaseUrlWithInit ) => Err ( JsError :: from_native ( JsNativeError :: typ ( )
77
- . with_message ( "Specifying both an init object, and a separate base URL is not valid" ) ) ) ,
81
+ Err ( e) => Err ( JsError :: from_native (
82
+ JsNativeError :: typ ( ) . with_message ( e. to_string ( ) ) ,
83
+ ) ) ,
78
84
Ok ( Some ( input) ) => Ok ( input) ,
79
- _ => Err ( JsError :: from_native ( JsNativeError :: error ( ) ) )
85
+ Ok ( None ) => Err ( JsError :: from_native ( JsNativeError :: error ( ) ) ) ,
80
86
}
81
87
}
82
88
@@ -148,11 +154,9 @@ impl UrlPattern {
148
154
let UrlPatternInput ( string_or_init) = input;
149
155
let ( url_pattern_match_input, _) = Self :: process_input ( string_or_init, base_url) ?;
150
156
151
- self . url_pattern . test ( url_pattern_match_input) . map_err ( |_| {
152
- JsNativeError :: typ ( )
153
- . with_message ( "Failed to run `test` on `UrlPattern`" )
154
- . into ( )
155
- } )
157
+ self . url_pattern
158
+ . test ( url_pattern_match_input)
159
+ . map_err ( |e| JsNativeError :: typ ( ) . with_message ( e. to_string ( ) ) . into ( ) )
156
160
}
157
161
158
162
pub fn exec (
@@ -177,11 +181,7 @@ impl UrlPattern {
177
181
url_pattern_result,
178
182
} )
179
183
} )
180
- . map_err ( |_| {
181
- JsNativeError :: typ ( )
182
- . with_message ( "Failed to run `exec` on `UrlPattern`" )
183
- . into ( )
184
- } )
184
+ . map_err ( |e| JsNativeError :: typ ( ) . with_message ( e. to_string ( ) ) . into ( ) )
185
185
}
186
186
}
187
187
@@ -281,7 +281,10 @@ impl UrlPatternClass {
281
281
context : & mut Context < ' _ > ,
282
282
) -> JsResult < JsValue > {
283
283
let url_pattern = UrlPattern :: try_from_js ( this) ?;
284
- let input: UrlPatternInput = args. get ( 0 ) . unwrap ( ) . try_js_into ( context) ?;
284
+ let input: UrlPatternInput = match args. get ( 0 ) {
285
+ Some ( value) => value. try_js_into ( context) ?,
286
+ None => UrlPatternInput :: default ( ) ,
287
+ } ;
285
288
let base_url: Option < String > = args. get_or_undefined ( 1 ) . try_js_into ( context) . ok ( ) ;
286
289
Ok ( url_pattern. test ( input, base_url) ?. into_js ( context) )
287
290
}
@@ -292,20 +295,19 @@ impl UrlPatternClass {
292
295
context : & mut Context < ' _ > ,
293
296
) -> JsResult < JsValue > {
294
297
let url_pattern = UrlPattern :: try_from_js ( this) ?;
295
- let input: UrlPatternInput = args. get ( 0 ) . unwrap ( ) . try_js_into ( context) ?;
298
+ let input: UrlPatternInput = match args. get ( 0 ) {
299
+ Some ( value) => value. try_js_into ( context) ?,
300
+ None => UrlPatternInput :: default ( ) ,
301
+ } ;
296
302
let base_url: Option < String > = args. get_or_undefined ( 1 ) . try_js_into ( context) . ok ( ) ;
297
303
url_pattern
298
304
. exec ( input, base_url) ?
299
- . map_or ( Ok ( JsValue :: Null ) , |e | Ok ( e . into_js ( context) ) )
305
+ . map_or ( Ok ( JsValue :: Null ) , |r | Ok ( r . into_js ( context) ) )
300
306
}
301
307
}
302
308
303
309
impl TryFromJs for UrlPatternInit {
304
310
fn try_from_js ( value : & JsValue , context : & mut Context < ' _ > ) -> JsResult < Self > {
305
- if value. is_undefined ( ) {
306
- return Ok ( UrlPatternInit :: default ( ) ) ;
307
- }
308
-
309
311
let obj = value. as_object ( ) . ok_or_else ( || {
310
312
JsError :: from_native ( JsNativeError :: typ ( ) . with_message ( "Expected `JsObject`" ) )
311
313
} ) ?;
@@ -469,7 +471,10 @@ impl NativeClass for UrlPatternClass {
469
471
args : & [ JsValue ] ,
470
472
context : & mut Context < ' _ > ,
471
473
) -> JsResult < UrlPattern > {
472
- let input: UrlPatternInput = args. get_or_undefined ( 0 ) . try_js_into ( context) ?;
474
+ let input: UrlPatternInput = match args. get ( 0 ) {
475
+ Some ( value) => value. try_js_into ( context) ?,
476
+ None => UrlPatternInput :: default ( ) ,
477
+ } ;
473
478
let base_url: Option < String > = args. get_or_undefined ( 1 ) . try_js_into ( context) ?;
474
479
475
480
UrlPattern :: new ( this, input, base_url, context)
@@ -496,12 +501,12 @@ impl NativeClass for UrlPatternClass {
496
501
. accessor ( js_string ! ( "username" ) , username, Attribute :: all ( ) )
497
502
. method (
498
503
js_string ! ( "test" ) ,
499
- 0 ,
504
+ 2 ,
500
505
NativeFunction :: from_fn_ptr ( UrlPatternClass :: test) ,
501
506
)
502
507
. method (
503
508
js_string ! ( "exec" ) ,
504
- 0 ,
509
+ 2 ,
505
510
NativeFunction :: from_fn_ptr ( UrlPatternClass :: exec) ,
506
511
) ;
507
512
Ok ( ( ) )
0 commit comments