@@ -67,6 +67,10 @@ impl From<c_uint> for HostType {
67
67
}
68
68
}
69
69
70
+ /// Components are a serialization-free representation of a URL.
71
+ /// For usages where string serialization has a high cost, you can
72
+ /// use url components with `href` attribute.
73
+ ///
70
74
/// By using 32-bit integers, we implicitly assume that the URL string
71
75
/// cannot exceed 4 GB.
72
76
///
@@ -234,6 +238,16 @@ impl Url {
234
238
unsafe { ffi:: ada_get_href ( self . 0 ) } . as_str ( )
235
239
}
236
240
241
+ /// Updates the href of the URL, and triggers the URL parser.
242
+ /// Returns true if operation is successful.
243
+ ///
244
+ /// ```
245
+ /// use ada_url::Url;
246
+ ///
247
+ /// let mut url = Url::parse("https://yagiz.co", None).expect("Invalid URL");
248
+ /// assert!(url.set_href("https://lemire.me"));
249
+ /// assert_eq!(url.href(), "https://lemire.me/");
250
+ /// ```
237
251
pub fn set_href ( & mut self , input : & str ) -> bool {
238
252
unsafe { ffi:: ada_set_href ( self . 0 , input. as_ptr ( ) . cast ( ) , input. len ( ) ) }
239
253
}
@@ -252,6 +266,16 @@ impl Url {
252
266
unsafe { ffi:: ada_get_username ( self . 0 ) } . as_str ( )
253
267
}
254
268
269
+ /// Updates the `username` of the URL.
270
+ /// Returns true if operation is successful.
271
+ ///
272
+ /// ```
273
+ /// use ada_url::Url;
274
+ ///
275
+ /// let mut url = Url::parse("https://yagiz.co", None).expect("Invalid URL");
276
+ /// assert!(url.set_username("username"));
277
+ /// assert_eq!(url.href(), "https://[email protected] /");
278
+ /// ```
255
279
pub fn set_username ( & mut self , input : & str ) -> bool {
256
280
unsafe { ffi:: ada_set_username ( self . 0 , input. as_ptr ( ) . cast ( ) , input. len ( ) ) }
257
281
}
@@ -270,6 +294,16 @@ impl Url {
270
294
unsafe { ffi:: ada_get_password ( self . 0 ) } . as_str ( )
271
295
}
272
296
297
+ /// Updates the `password` of the URL.
298
+ /// Returns true if operation is successful.
299
+ ///
300
+ /// ```
301
+ /// use ada_url::Url;
302
+ ///
303
+ /// let mut url = Url::parse("https://yagiz.co", None).expect("Invalid URL");
304
+ /// assert!(url.set_password("password"));
305
+ /// assert_eq!(url.href(), "https://:[email protected] /");
306
+ /// ```
273
307
pub fn set_password ( & mut self , input : & str ) -> bool {
274
308
unsafe { ffi:: ada_set_password ( self . 0 , input. as_ptr ( ) . cast ( ) , input. len ( ) ) }
275
309
}
@@ -291,6 +325,16 @@ impl Url {
291
325
unsafe { ffi:: ada_get_port ( self . 0 ) } . as_str ( )
292
326
}
293
327
328
+ /// Updates the `port` of the URL.
329
+ /// Returns true if operation is successful.
330
+ ///
331
+ /// ```
332
+ /// use ada_url::Url;
333
+ ///
334
+ /// let mut url = Url::parse("https://yagiz.co", None).expect("Invalid URL");
335
+ /// assert!(url.set_port("8080"));
336
+ /// assert_eq!(url.href(), "https://yagiz.co:8080/");
337
+ /// ```
294
338
pub fn set_port ( & mut self , input : & str ) -> bool {
295
339
unsafe { ffi:: ada_set_port ( self . 0 , input. as_ptr ( ) . cast ( ) , input. len ( ) ) }
296
340
}
@@ -316,6 +360,15 @@ impl Url {
316
360
unsafe { ffi:: ada_get_hash ( self . 0 ) } . as_str ( )
317
361
}
318
362
363
+ /// Updates the `hash` of the URL.
364
+ ///
365
+ /// ```
366
+ /// use ada_url::Url;
367
+ ///
368
+ /// let mut url = Url::parse("https://yagiz.co", None).expect("Invalid URL");
369
+ /// url.set_hash("this-is-my-hash");
370
+ /// assert_eq!(url.href(), "https://yagiz.co/#this-is-my-hash");
371
+ /// ```
319
372
pub fn set_hash ( & mut self , input : & str ) {
320
373
unsafe { ffi:: ada_set_hash ( self . 0 , input. as_ptr ( ) . cast ( ) , input. len ( ) ) }
321
374
}
@@ -334,6 +387,16 @@ impl Url {
334
387
unsafe { ffi:: ada_get_host ( self . 0 ) } . as_str ( )
335
388
}
336
389
390
+ /// Updates the `host` of the URL.
391
+ /// Returns true if operation is successful.
392
+ ///
393
+ /// ```
394
+ /// use ada_url::Url;
395
+ ///
396
+ /// let mut url = Url::parse("https://yagiz.co", None).expect("Invalid URL");
397
+ /// assert!(url.set_host("localhost:3000"));
398
+ /// assert_eq!(url.href(), "https://localhost:3000/");
399
+ /// ```
337
400
pub fn set_host ( & mut self , input : & str ) -> bool {
338
401
unsafe { ffi:: ada_set_host ( self . 0 , input. as_ptr ( ) . cast ( ) , input. len ( ) ) }
339
402
}
@@ -356,6 +419,16 @@ impl Url {
356
419
unsafe { ffi:: ada_get_hostname ( self . 0 ) } . as_str ( )
357
420
}
358
421
422
+ /// Updates the `hostname` of the URL.
423
+ /// Returns true if operation is successful.
424
+ ///
425
+ /// ```
426
+ /// use ada_url::Url;
427
+ ///
428
+ /// let mut url = Url::parse("https://yagiz.co", None).expect("Invalid URL");
429
+ /// assert!(url.set_hostname("localhost"));
430
+ /// assert_eq!(url.href(), "https://localhost/");
431
+ /// ```
359
432
pub fn set_hostname ( & mut self , input : & str ) -> bool {
360
433
unsafe { ffi:: ada_set_hostname ( self . 0 , input. as_ptr ( ) . cast ( ) , input. len ( ) ) }
361
434
}
@@ -374,6 +447,16 @@ impl Url {
374
447
unsafe { ffi:: ada_get_pathname ( self . 0 ) } . as_str ( )
375
448
}
376
449
450
+ /// Updates the `pathname` of the URL.
451
+ /// Returns true if operation is successful.
452
+ ///
453
+ /// ```
454
+ /// use ada_url::Url;
455
+ ///
456
+ /// let mut url = Url::parse("https://yagiz.co", None).expect("Invalid URL");
457
+ /// assert!(url.set_pathname("/contact"));
458
+ /// assert_eq!(url.href(), "https://yagiz.co/contact");
459
+ /// ```
377
460
pub fn set_pathname ( & mut self , input : & str ) -> bool {
378
461
unsafe { ffi:: ada_set_pathname ( self . 0 , input. as_ptr ( ) . cast ( ) , input. len ( ) ) }
379
462
}
@@ -395,6 +478,15 @@ impl Url {
395
478
unsafe { ffi:: ada_get_search ( self . 0 ) } . as_str ( )
396
479
}
397
480
481
+ /// Updates the `search` of the URL.
482
+ ///
483
+ /// ```
484
+ /// use ada_url::Url;
485
+ ///
486
+ /// let mut url = Url::parse("https://yagiz.co", None).expect("Invalid URL");
487
+ /// url.set_search("?page=1");
488
+ /// assert_eq!(url.href(), "https://yagiz.co/?page=1");
489
+ /// ```
398
490
pub fn set_search ( & mut self , input : & str ) {
399
491
unsafe { ffi:: ada_set_search ( self . 0 , input. as_ptr ( ) . cast ( ) , input. len ( ) ) }
400
492
}
@@ -413,6 +505,16 @@ impl Url {
413
505
unsafe { ffi:: ada_get_protocol ( self . 0 ) } . as_str ( )
414
506
}
415
507
508
+ /// Updates the `protocol` of the URL.
509
+ /// Returns true if operation is successful.
510
+ ///
511
+ /// ```
512
+ /// use ada_url::Url;
513
+ ///
514
+ /// let mut url = Url::parse("http://yagiz.co", None).expect("Invalid URL");
515
+ /// assert!(url.set_protocol("http"));
516
+ /// assert_eq!(url.href(), "http://yagiz.co/");
517
+ /// ```
416
518
pub fn set_protocol ( & mut self , input : & str ) -> bool {
417
519
unsafe { ffi:: ada_set_protocol ( self . 0 , input. as_ptr ( ) . cast ( ) , input. len ( ) ) }
418
520
}
@@ -432,26 +534,32 @@ impl Url {
432
534
unsafe { ffi:: ada_has_hostname ( self . 0 ) }
433
535
}
434
536
537
+ /// Returns true if URL has a non-empty username.
435
538
pub fn has_non_empty_username ( & self ) -> bool {
436
539
unsafe { ffi:: ada_has_non_empty_username ( self . 0 ) }
437
540
}
438
541
542
+ /// Returns true if URL has a non-empty pasword.
439
543
pub fn has_non_empty_password ( & self ) -> bool {
440
544
unsafe { ffi:: ada_has_non_empty_password ( self . 0 ) }
441
545
}
442
546
547
+ /// Returns true if URL has a port.
443
548
pub fn has_port ( & self ) -> bool {
444
549
unsafe { ffi:: ada_has_port ( self . 0 ) }
445
550
}
446
551
552
+ /// Returns true if URL has password.
447
553
pub fn has_password ( & self ) -> bool {
448
554
unsafe { ffi:: ada_has_password ( self . 0 ) }
449
555
}
450
556
557
+ /// Returns true if URL has a hash/fragment.
451
558
pub fn has_hash ( & self ) -> bool {
452
559
unsafe { ffi:: ada_has_hash ( self . 0 ) }
453
560
}
454
561
562
+ /// Returns true if URL has search/query.
455
563
pub fn has_search ( & self ) -> bool {
456
564
unsafe { ffi:: ada_has_search ( self . 0 ) }
457
565
}
0 commit comments