@@ -35,8 +35,7 @@ pub mod ffi;
35
35
mod idna;
36
36
pub use idna:: Idna ;
37
37
38
- use std:: os:: raw:: c_uint;
39
- use std:: { borrow, fmt, hash, ops} ;
38
+ use std:: { borrow, fmt, hash, ops, os:: raw:: c_uint} ;
40
39
use thiserror:: Error ;
41
40
42
41
extern crate alloc;
@@ -70,23 +69,25 @@ impl From<c_uint> for HostType {
70
69
71
70
/// A parsed URL struct according to WHATWG URL specification.
72
71
#[ derive( Eq ) ]
73
- pub struct Url {
74
- url : * mut ffi:: ada_url ,
75
- }
72
+ pub struct Url ( * mut ffi:: ada_url ) ;
76
73
77
74
/// Clone trait by default uses bit-wise copy.
78
75
/// In Rust, FFI requires deep copy, which requires an additional/inexpensive FFI call.
79
76
impl Clone for Url {
80
77
fn clone ( & self ) -> Self {
81
- Url {
82
- url : unsafe { ffi:: ada_copy ( self . url ) } ,
83
- }
78
+ unsafe { ffi:: ada_copy ( self . 0 ) . into ( ) }
84
79
}
85
80
}
86
81
87
82
impl Drop for Url {
88
83
fn drop ( & mut self ) {
89
- unsafe { ffi:: ada_free ( self . url ) }
84
+ unsafe { ffi:: ada_free ( self . 0 ) }
85
+ }
86
+ }
87
+
88
+ impl From < * mut ffi:: ada_url > for Url {
89
+ fn from ( value : * mut ffi:: ada_url ) -> Self {
90
+ Self { 0 : value }
90
91
}
91
92
}
92
93
@@ -113,9 +114,7 @@ impl Url {
113
114
} ;
114
115
115
116
if unsafe { ffi:: ada_is_valid ( url_aggregator) } {
116
- Ok ( Url {
117
- url : url_aggregator,
118
- } )
117
+ Ok ( url_aggregator. into ( ) )
119
118
} else {
120
119
Err ( Error :: ParseUrl ( input. to_owned ( ) ) )
121
120
}
@@ -147,7 +146,7 @@ impl Url {
147
146
148
147
/// Returns the type of the host such as default, ipv4 or ipv6.
149
148
pub fn host_type ( & self ) -> HostType {
150
- HostType :: from ( unsafe { ffi:: ada_get_url_host_type ( self . url ) } )
149
+ HostType :: from ( unsafe { ffi:: ada_get_url_host_type ( self . 0 ) } )
151
150
}
152
151
153
152
/// Return the origin of this URL
@@ -162,7 +161,7 @@ impl Url {
162
161
/// ```
163
162
pub fn origin ( & self ) -> & str {
164
163
unsafe {
165
- let out = ffi:: ada_get_origin ( self . url ) ;
164
+ let out = ffi:: ada_get_origin ( self . 0 ) ;
166
165
let slice = std:: slice:: from_raw_parts ( out. data . cast ( ) , out. length ) ;
167
166
std:: str:: from_utf8_unchecked ( slice)
168
167
}
@@ -172,11 +171,11 @@ impl Url {
172
171
///
173
172
/// For more information, read [WHATWG URL spec](https://url.spec.whatwg.org/#dom-url-href)
174
173
pub fn href ( & self ) -> & str {
175
- unsafe { ffi:: ada_get_href ( self . url ) } . as_str ( )
174
+ unsafe { ffi:: ada_get_href ( self . 0 ) } . as_str ( )
176
175
}
177
176
178
177
pub fn set_href ( & mut self , input : & str ) -> bool {
179
- unsafe { ffi:: ada_set_href ( self . url , input. as_ptr ( ) . cast ( ) , input. len ( ) ) }
178
+ unsafe { ffi:: ada_set_href ( self . 0 , input. as_ptr ( ) . cast ( ) , input. len ( ) ) }
180
179
}
181
180
182
181
/// Return the username for this URL as a percent-encoded ASCII string.
@@ -190,11 +189,11 @@ impl Url {
190
189
/// assert_eq!(url.username(), "rms");
191
190
/// ```
192
191
pub fn username ( & self ) -> & str {
193
- unsafe { ffi:: ada_get_username ( self . url ) } . as_str ( )
192
+ unsafe { ffi:: ada_get_username ( self . 0 ) } . as_str ( )
194
193
}
195
194
196
195
pub fn set_username ( & mut self , input : & str ) -> bool {
197
- unsafe { ffi:: ada_set_username ( self . url , input. as_ptr ( ) . cast ( ) , input. len ( ) ) }
196
+ unsafe { ffi:: ada_set_username ( self . 0 , input. as_ptr ( ) . cast ( ) , input. len ( ) ) }
198
197
}
199
198
200
199
/// Return the password for this URL, if any, as a percent-encoded ASCII string.
@@ -208,11 +207,11 @@ impl Url {
208
207
/// assert_eq!(url.password(), "secret123");
209
208
/// ```
210
209
pub fn password ( & self ) -> & str {
211
- unsafe { ffi:: ada_get_password ( self . url ) } . as_str ( )
210
+ unsafe { ffi:: ada_get_password ( self . 0 ) } . as_str ( )
212
211
}
213
212
214
213
pub fn set_password ( & mut self , input : & str ) -> bool {
215
- unsafe { ffi:: ada_set_password ( self . url , input. as_ptr ( ) . cast ( ) , input. len ( ) ) }
214
+ unsafe { ffi:: ada_set_password ( self . 0 , input. as_ptr ( ) . cast ( ) , input. len ( ) ) }
216
215
}
217
216
218
217
/// Return the port number for this URL, or an empty string.
@@ -229,11 +228,11 @@ impl Url {
229
228
/// assert_eq!(url.port(), "8080");
230
229
/// ```
231
230
pub fn port ( & self ) -> & str {
232
- unsafe { ffi:: ada_get_port ( self . url ) } . as_str ( )
231
+ unsafe { ffi:: ada_get_port ( self . 0 ) } . as_str ( )
233
232
}
234
233
235
234
pub fn set_port ( & mut self , input : & str ) -> bool {
236
- unsafe { ffi:: ada_set_port ( self . url , input. as_ptr ( ) . cast ( ) , input. len ( ) ) }
235
+ unsafe { ffi:: ada_set_port ( self . 0 , input. as_ptr ( ) . cast ( ) , input. len ( ) ) }
237
236
}
238
237
239
238
/// Return this URL’s fragment identifier, or an empty string.
@@ -254,11 +253,11 @@ impl Url {
254
253
/// assert!(url.has_hash());
255
254
/// ```
256
255
pub fn hash ( & self ) -> & str {
257
- unsafe { ffi:: ada_get_hash ( self . url ) } . as_str ( )
256
+ unsafe { ffi:: ada_get_hash ( self . 0 ) } . as_str ( )
258
257
}
259
258
260
259
pub fn set_hash ( & mut self , input : & str ) {
261
- unsafe { ffi:: ada_set_hash ( self . url , input. as_ptr ( ) . cast ( ) , input. len ( ) ) }
260
+ unsafe { ffi:: ada_set_hash ( self . 0 , input. as_ptr ( ) . cast ( ) , input. len ( ) ) }
262
261
}
263
262
264
263
/// Return the parsed representation of the host for this URL with an optional port number.
@@ -272,11 +271,11 @@ impl Url {
272
271
/// assert_eq!(url.host(), "127.0.0.1:8080");
273
272
/// ```
274
273
pub fn host ( & self ) -> & str {
275
- unsafe { ffi:: ada_get_host ( self . url ) } . as_str ( )
274
+ unsafe { ffi:: ada_get_host ( self . 0 ) } . as_str ( )
276
275
}
277
276
278
277
pub fn set_host ( & mut self , input : & str ) -> bool {
279
- unsafe { ffi:: ada_set_host ( self . url , input. as_ptr ( ) . cast ( ) , input. len ( ) ) }
278
+ unsafe { ffi:: ada_set_host ( self . 0 , input. as_ptr ( ) . cast ( ) , input. len ( ) ) }
280
279
}
281
280
282
281
/// Return the parsed representation of the host for this URL. Non-ASCII domain labels are
@@ -294,11 +293,11 @@ impl Url {
294
293
/// assert_eq!(url.hostname(), "127.0.0.1");
295
294
/// ```
296
295
pub fn hostname ( & self ) -> & str {
297
- unsafe { ffi:: ada_get_hostname ( self . url ) } . as_str ( )
296
+ unsafe { ffi:: ada_get_hostname ( self . 0 ) } . as_str ( )
298
297
}
299
298
300
299
pub fn set_hostname ( & mut self , input : & str ) -> bool {
301
- unsafe { ffi:: ada_set_hostname ( self . url , input. as_ptr ( ) . cast ( ) , input. len ( ) ) }
300
+ unsafe { ffi:: ada_set_hostname ( self . 0 , input. as_ptr ( ) . cast ( ) , input. len ( ) ) }
302
301
}
303
302
304
303
/// Return the path for this URL, as a percent-encoded ASCII string.
@@ -312,11 +311,11 @@ impl Url {
312
311
/// assert_eq!(url.pathname(), "/api/versions");
313
312
/// ```
314
313
pub fn pathname ( & self ) -> & str {
315
- unsafe { ffi:: ada_get_pathname ( self . url ) } . as_str ( )
314
+ unsafe { ffi:: ada_get_pathname ( self . 0 ) } . as_str ( )
316
315
}
317
316
318
317
pub fn set_pathname ( & mut self , input : & str ) -> bool {
319
- unsafe { ffi:: ada_set_pathname ( self . url , input. as_ptr ( ) . cast ( ) , input. len ( ) ) }
318
+ unsafe { ffi:: ada_set_pathname ( self . 0 , input. as_ptr ( ) . cast ( ) , input. len ( ) ) }
320
319
}
321
320
322
321
/// Return this URL’s query string, if any, as a percent-encoded ASCII string.
@@ -333,11 +332,11 @@ impl Url {
333
332
/// assert_eq!(url.search(), "");
334
333
/// ```
335
334
pub fn search ( & self ) -> & str {
336
- unsafe { ffi:: ada_get_search ( self . url ) } . as_str ( )
335
+ unsafe { ffi:: ada_get_search ( self . 0 ) } . as_str ( )
337
336
}
338
337
339
338
pub fn set_search ( & mut self , input : & str ) {
340
- unsafe { ffi:: ada_set_search ( self . url , input. as_ptr ( ) . cast ( ) , input. len ( ) ) }
339
+ unsafe { ffi:: ada_set_search ( self . 0 , input. as_ptr ( ) . cast ( ) , input. len ( ) ) }
341
340
}
342
341
343
342
/// Return the scheme of this URL, lower-cased, as an ASCII string with the ‘:’ delimiter.
@@ -351,50 +350,50 @@ impl Url {
351
350
/// assert_eq!(url.protocol(), "file:");
352
351
/// ```
353
352
pub fn protocol ( & self ) -> & str {
354
- unsafe { ffi:: ada_get_protocol ( self . url ) } . as_str ( )
353
+ unsafe { ffi:: ada_get_protocol ( self . 0 ) } . as_str ( )
355
354
}
356
355
357
356
pub fn set_protocol ( & mut self , input : & str ) -> bool {
358
- unsafe { ffi:: ada_set_protocol ( self . url , input. as_ptr ( ) . cast ( ) , input. len ( ) ) }
357
+ unsafe { ffi:: ada_set_protocol ( self . 0 , input. as_ptr ( ) . cast ( ) , input. len ( ) ) }
359
358
}
360
359
361
360
/// A URL includes credentials if its username or password is not the empty string.
362
361
pub fn has_credentials ( & self ) -> bool {
363
- unsafe { ffi:: ada_has_credentials ( self . url ) }
362
+ unsafe { ffi:: ada_has_credentials ( self . 0 ) }
364
363
}
365
364
366
365
/// Returns true if it has an host but it is the empty string.
367
366
pub fn has_empty_hostname ( & self ) -> bool {
368
- unsafe { ffi:: ada_has_empty_hostname ( self . url ) }
367
+ unsafe { ffi:: ada_has_empty_hostname ( self . 0 ) }
369
368
}
370
369
371
370
/// Returns true if it has a host (included an empty host)
372
371
pub fn has_hostname ( & self ) -> bool {
373
- unsafe { ffi:: ada_has_hostname ( self . url ) }
372
+ unsafe { ffi:: ada_has_hostname ( self . 0 ) }
374
373
}
375
374
376
375
pub fn has_non_empty_username ( & self ) -> bool {
377
- unsafe { ffi:: ada_has_non_empty_username ( self . url ) }
376
+ unsafe { ffi:: ada_has_non_empty_username ( self . 0 ) }
378
377
}
379
378
380
379
pub fn has_non_empty_password ( & self ) -> bool {
381
- unsafe { ffi:: ada_has_non_empty_password ( self . url ) }
380
+ unsafe { ffi:: ada_has_non_empty_password ( self . 0 ) }
382
381
}
383
382
384
383
pub fn has_port ( & self ) -> bool {
385
- unsafe { ffi:: ada_has_port ( self . url ) }
384
+ unsafe { ffi:: ada_has_port ( self . 0 ) }
386
385
}
387
386
388
387
pub fn has_password ( & self ) -> bool {
389
- unsafe { ffi:: ada_has_password ( self . url ) }
388
+ unsafe { ffi:: ada_has_password ( self . 0 ) }
390
389
}
391
390
392
391
pub fn has_hash ( & self ) -> bool {
393
- unsafe { ffi:: ada_has_hash ( self . url ) }
392
+ unsafe { ffi:: ada_has_hash ( self . 0 ) }
394
393
}
395
394
396
395
pub fn has_search ( & self ) -> bool {
397
- unsafe { ffi:: ada_has_search ( self . url ) }
396
+ unsafe { ffi:: ada_has_search ( self . 0 ) }
398
397
}
399
398
/// Returns the parsed version of the URL with all components.
400
399
///
@@ -510,7 +509,7 @@ impl From<Url> for String {
510
509
impl fmt:: Debug for Url {
511
510
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
512
511
unsafe {
513
- let components = ffi:: ada_get_components ( self . url ) . as_ref ( ) . unwrap ( ) ;
512
+ let components = ffi:: ada_get_components ( self . 0 ) . as_ref ( ) . unwrap ( ) ;
514
513
let mut debug = f. debug_struct ( "Url" ) ;
515
514
debug
516
515
. field ( "href" , & self . href ( ) )
0 commit comments