Skip to content

Commit a2cd9cf

Browse files
committed
doc: add comments for setter functions
1 parent 61297c5 commit a2cd9cf

File tree

1 file changed

+108
-0
lines changed

1 file changed

+108
-0
lines changed

src/lib.rs

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ impl From<c_uint> for HostType {
6767
}
6868
}
6969

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+
///
7074
/// By using 32-bit integers, we implicitly assume that the URL string
7175
/// cannot exceed 4 GB.
7276
///
@@ -234,6 +238,16 @@ impl Url {
234238
unsafe { ffi::ada_get_href(self.0) }.as_str()
235239
}
236240

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+
/// ```
237251
pub fn set_href(&mut self, input: &str) -> bool {
238252
unsafe { ffi::ada_set_href(self.0, input.as_ptr().cast(), input.len()) }
239253
}
@@ -252,6 +266,16 @@ impl Url {
252266
unsafe { ffi::ada_get_username(self.0) }.as_str()
253267
}
254268

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+
/// ```
255279
pub fn set_username(&mut self, input: &str) -> bool {
256280
unsafe { ffi::ada_set_username(self.0, input.as_ptr().cast(), input.len()) }
257281
}
@@ -270,6 +294,16 @@ impl Url {
270294
unsafe { ffi::ada_get_password(self.0) }.as_str()
271295
}
272296

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+
/// ```
273307
pub fn set_password(&mut self, input: &str) -> bool {
274308
unsafe { ffi::ada_set_password(self.0, input.as_ptr().cast(), input.len()) }
275309
}
@@ -291,6 +325,16 @@ impl Url {
291325
unsafe { ffi::ada_get_port(self.0) }.as_str()
292326
}
293327

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+
/// ```
294338
pub fn set_port(&mut self, input: &str) -> bool {
295339
unsafe { ffi::ada_set_port(self.0, input.as_ptr().cast(), input.len()) }
296340
}
@@ -316,6 +360,15 @@ impl Url {
316360
unsafe { ffi::ada_get_hash(self.0) }.as_str()
317361
}
318362

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+
/// ```
319372
pub fn set_hash(&mut self, input: &str) {
320373
unsafe { ffi::ada_set_hash(self.0, input.as_ptr().cast(), input.len()) }
321374
}
@@ -334,6 +387,16 @@ impl Url {
334387
unsafe { ffi::ada_get_host(self.0) }.as_str()
335388
}
336389

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+
/// ```
337400
pub fn set_host(&mut self, input: &str) -> bool {
338401
unsafe { ffi::ada_set_host(self.0, input.as_ptr().cast(), input.len()) }
339402
}
@@ -356,6 +419,16 @@ impl Url {
356419
unsafe { ffi::ada_get_hostname(self.0) }.as_str()
357420
}
358421

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+
/// ```
359432
pub fn set_hostname(&mut self, input: &str) -> bool {
360433
unsafe { ffi::ada_set_hostname(self.0, input.as_ptr().cast(), input.len()) }
361434
}
@@ -374,6 +447,16 @@ impl Url {
374447
unsafe { ffi::ada_get_pathname(self.0) }.as_str()
375448
}
376449

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+
/// ```
377460
pub fn set_pathname(&mut self, input: &str) -> bool {
378461
unsafe { ffi::ada_set_pathname(self.0, input.as_ptr().cast(), input.len()) }
379462
}
@@ -395,6 +478,15 @@ impl Url {
395478
unsafe { ffi::ada_get_search(self.0) }.as_str()
396479
}
397480

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+
/// ```
398490
pub fn set_search(&mut self, input: &str) {
399491
unsafe { ffi::ada_set_search(self.0, input.as_ptr().cast(), input.len()) }
400492
}
@@ -413,6 +505,16 @@ impl Url {
413505
unsafe { ffi::ada_get_protocol(self.0) }.as_str()
414506
}
415507

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+
/// ```
416518
pub fn set_protocol(&mut self, input: &str) -> bool {
417519
unsafe { ffi::ada_set_protocol(self.0, input.as_ptr().cast(), input.len()) }
418520
}
@@ -432,26 +534,32 @@ impl Url {
432534
unsafe { ffi::ada_has_hostname(self.0) }
433535
}
434536

537+
/// Returns true if URL has a non-empty username.
435538
pub fn has_non_empty_username(&self) -> bool {
436539
unsafe { ffi::ada_has_non_empty_username(self.0) }
437540
}
438541

542+
/// Returns true if URL has a non-empty pasword.
439543
pub fn has_non_empty_password(&self) -> bool {
440544
unsafe { ffi::ada_has_non_empty_password(self.0) }
441545
}
442546

547+
/// Returns true if URL has a port.
443548
pub fn has_port(&self) -> bool {
444549
unsafe { ffi::ada_has_port(self.0) }
445550
}
446551

552+
/// Returns true if URL has password.
447553
pub fn has_password(&self) -> bool {
448554
unsafe { ffi::ada_has_password(self.0) }
449555
}
450556

557+
/// Returns true if URL has a hash/fragment.
451558
pub fn has_hash(&self) -> bool {
452559
unsafe { ffi::ada_has_hash(self.0) }
453560
}
454561

562+
/// Returns true if URL has search/query.
455563
pub fn has_search(&self) -> bool {
456564
unsafe { ffi::ada_has_search(self.0) }
457565
}

0 commit comments

Comments
 (0)