Skip to content

Commit ff65fde

Browse files
committed
docs: add examples to functions
1 parent 11737bd commit ff65fde

File tree

1 file changed

+121
-0
lines changed

1 file changed

+121
-0
lines changed

src/lib.rs

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,8 @@ impl Url {
160160

161161
/// Returns whether or not the URL can be parsed or not.
162162
///
163+
/// For more information, read [WHATWG URL spec](https://url.spec.whatwg.org/#dom-url-canparse)
164+
///
163165
/// ```
164166
/// use ada_url::Url;
165167
/// assert!(Url::can_parse("https://ada-url.github.io/ada", None));
@@ -180,6 +182,16 @@ impl Url {
180182
}
181183
}
182184

185+
/// Return the origin of this URL
186+
///
187+
/// For more information, read [WHATWG URL spec](https://url.spec.whatwg.org/#dom-url-origin)
188+
///
189+
/// ```
190+
/// use ada_url::Url;
191+
///
192+
/// let mut url = Url::parse("blob:https://example.com/foo", None).expect("Invalid URL");
193+
/// assert_eq!(url.origin(), "https://example.com");
194+
/// ```
183195
pub fn origin(&mut self) -> &str {
184196
unsafe {
185197
let out = ffi::ada_get_origin(self.url);
@@ -188,6 +200,8 @@ impl Url {
188200
}
189201
}
190202

203+
/// Return the parsed version of the URL with all components.
204+
/// For more information, read [WHATWG URL spec](https://url.spec.whatwg.org/#dom-url-href)
191205
pub fn href(&self) -> &str {
192206
unsafe { ffi::ada_get_href(self.url) }.as_str()
193207
}
@@ -196,6 +210,16 @@ impl Url {
196210
unsafe { ffi::ada_set_href(self.url, input.as_ptr().cast(), input.len()) }
197211
}
198212

213+
/// Return the username for this URL as a percent-encoded ASCII string.
214+
///
215+
/// For more information, read [WHATWG URL spec](https://url.spec.whatwg.org/#dom-url-username)
216+
///
217+
/// ```
218+
/// use ada_url::Url;
219+
///
220+
/// let url = Url::parse("ftp://rms:[email protected]", None).expect("Invalid URL");
221+
/// assert_eq!(url.username(), "rms");
222+
/// ```
199223
pub fn username(&self) -> &str {
200224
unsafe { ffi::ada_get_username(self.url) }.as_str()
201225
}
@@ -204,6 +228,16 @@ impl Url {
204228
unsafe { ffi::ada_set_username(self.url, input.as_ptr().cast(), input.len()) }
205229
}
206230

231+
/// Return the password for this URL, if any, as a percent-encoded ASCII string.
232+
///
233+
/// For more information, read [WHATWG URL spec](https://url.spec.whatwg.org/#dom-url-password)
234+
///
235+
/// ```
236+
/// use ada_url::Url;
237+
///
238+
/// let url = Url::parse("ftp://rms:[email protected]", None).expect("Invalid URL");
239+
/// assert_eq!(url.password(), "secret123");
240+
/// ```
207241
pub fn password(&self) -> &str {
208242
unsafe { ffi::ada_get_password(self.url) }.as_str()
209243
}
@@ -212,6 +246,19 @@ impl Url {
212246
unsafe { ffi::ada_set_password(self.url, input.as_ptr().cast(), input.len()) }
213247
}
214248

249+
/// Return the port number for this URL, or an empty string.
250+
///
251+
/// For more information, read [WHATWG URL spec](https://url.spec.whatwg.org/#dom-url-port)
252+
///
253+
/// ```
254+
/// use ada_url::Url;
255+
///
256+
/// let url = Url::parse("https://example.com", None).expect("Invalid URL");
257+
/// assert_eq!(url.port(), "");
258+
///
259+
/// let url = Url::parse("https://example.com:8080", None).expect("Invalid URL");
260+
/// assert_eq!(url.port(), "8080");
261+
/// ```
215262
pub fn port(&self) -> &str {
216263
unsafe { ffi::ada_get_port(self.url) }.as_str()
217264
}
@@ -220,6 +267,23 @@ impl Url {
220267
unsafe { ffi::ada_set_port(self.url, input.as_ptr().cast(), input.len()) }
221268
}
222269

270+
/// Return this URL’s fragment identifier, or an empty string.
271+
/// A fragment is the part of the URL with the # symbol.
272+
/// The fragment is optional and, if present, contains a fragment identifier that identifies
273+
/// a secondary resource, such as a section heading of a document.
274+
/// In HTML, the fragment identifier is usually the id attribute of a an element that is
275+
/// scrolled to on load. Browsers typically will not send the fragment portion of a URL to the
276+
/// server.
277+
///
278+
/// For more information, read [WHATWG URL spec](https://url.spec.whatwg.org/#dom-url-hash)
279+
///
280+
/// ```
281+
/// use ada_url::Url;
282+
///
283+
/// let url = Url::parse("https://example.com/data.csv#row=4", None).expect("Invalid URL");
284+
/// assert_eq!(url.hash(), "#row=4");
285+
/// assert!(url.has_hash());
286+
/// ```
223287
pub fn hash(&self) -> &str {
224288
unsafe { ffi::ada_get_hash(self.url) }.as_str()
225289
}
@@ -228,6 +292,16 @@ impl Url {
228292
unsafe { ffi::ada_set_hash(self.url, input.as_ptr().cast(), input.len()) }
229293
}
230294

295+
/// Return the parsed representation of the host for this URL with an optional port number.
296+
///
297+
/// For more information, read [WHATWG URL spec](https://url.spec.whatwg.org/#dom-url-host)
298+
///
299+
/// ```
300+
/// use ada_url::Url;
301+
///
302+
/// let url = Url::parse("https://127.0.0.1:8080/index.html", None).expect("Invalid URL");
303+
/// assert_eq!(url.host(), "127.0.0.1:8080");
304+
/// ```
231305
pub fn host(&self) -> &str {
232306
unsafe { ffi::ada_get_host(self.url) }.as_str()
233307
}
@@ -236,6 +310,20 @@ impl Url {
236310
unsafe { ffi::ada_set_host(self.url, input.as_ptr().cast(), input.len()) }
237311
}
238312

313+
/// Return the parsed representation of the host for this URL. Non-ASCII domain labels are
314+
/// punycode-encoded per IDNA if this is the host of a special URL, or percent encoded for
315+
/// non-special URLs.
316+
///
317+
/// Hostname does not contain port number.
318+
///
319+
/// For more information, read [WHATWG URL spec](https://url.spec.whatwg.org/#dom-url-hostname)
320+
///
321+
/// ```
322+
/// use ada_url::Url;
323+
///
324+
/// let url = Url::parse("https://127.0.0.1:8080/index.html", None).expect("Invalid URL");
325+
/// assert_eq!(url.hostname(), "127.0.0.1");
326+
/// ```
239327
pub fn hostname(&self) -> &str {
240328
unsafe { ffi::ada_get_hostname(self.url) }.as_str()
241329
}
@@ -244,6 +332,16 @@ impl Url {
244332
unsafe { ffi::ada_set_hostname(self.url, input.as_ptr().cast(), input.len()) }
245333
}
246334

335+
/// Return the path for this URL, as a percent-encoded ASCII string.
336+
///
337+
/// For more information, read [WHATWG URL spec](https://url.spec.whatwg.org/#dom-url-pathname)
338+
///
339+
/// ```
340+
/// use ada_url::Url;
341+
///
342+
/// let url = Url::parse("https://example.com/api/versions?page=2", None).expect("Invalid URL");
343+
/// assert_eq!(url.pathname(), "/api/versions");
344+
/// ```
247345
pub fn pathname(&self) -> &str {
248346
unsafe { ffi::ada_get_pathname(self.url) }.as_str()
249347
}
@@ -252,6 +350,19 @@ impl Url {
252350
unsafe { ffi::ada_set_pathname(self.url, input.as_ptr().cast(), input.len()) }
253351
}
254352

353+
/// Return this URL’s query string, if any, as a percent-encoded ASCII string.
354+
///
355+
/// For more information, read [WHATWG URL spec](https://url.spec.whatwg.org/#dom-url-search)
356+
///
357+
/// ```
358+
/// use ada_url::Url;
359+
///
360+
/// let url = Url::parse("https://example.com/products?page=2", None).expect("Invalid URL");
361+
/// assert_eq!(url.search(), "?page=2");
362+
///
363+
/// let url = Url::parse("https://example.com/products", None).expect("Invalid URL");
364+
/// assert_eq!(url.search(), "");
365+
/// ```
255366
pub fn search(&self) -> &str {
256367
unsafe { ffi::ada_get_search(self.url) }.as_str()
257368
}
@@ -260,6 +371,16 @@ impl Url {
260371
unsafe { ffi::ada_set_search(self.url, input.as_ptr().cast(), input.len()) }
261372
}
262373

374+
/// Return the scheme of this URL, lower-cased, as an ASCII string with the ‘:’ delimiter.
375+
///
376+
/// For more information, read [WHATWG URL spec](https://url.spec.whatwg.org/#dom-url-protocol)
377+
///
378+
/// ```
379+
/// use ada_url::Url;
380+
///
381+
/// let url = Url::parse("file:///tmp/foo", None).expect("Invalid URL");
382+
/// assert_eq!(url.protocol(), "file:");
383+
/// ```
263384
pub fn protocol(&self) -> &str {
264385
unsafe { ffi::ada_get_protocol(self.url) }.as_str()
265386
}

0 commit comments

Comments
 (0)