Skip to content

Commit 7fa139a

Browse files
committed
refactor url struct
1 parent 9faa94c commit 7fa139a

File tree

2 files changed

+44
-45
lines changed

2 files changed

+44
-45
lines changed

src/ffi.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub struct ada_string {
1414
}
1515

1616
impl ada_string {
17-
pub fn as_str(self) -> &'static str {
17+
pub fn as_str(&self) -> &'static str {
1818
unsafe {
1919
let slice = std::slice::from_raw_parts(self.data.cast(), self.length);
2020
std::str::from_utf8_unchecked(slice)

src/lib.rs

Lines changed: 43 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@ pub mod ffi;
3535
mod idna;
3636
pub use idna::Idna;
3737

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};
4039
use thiserror::Error;
4140

4241
extern crate alloc;
@@ -70,23 +69,25 @@ impl From<c_uint> for HostType {
7069

7170
/// A parsed URL struct according to WHATWG URL specification.
7271
#[derive(Eq)]
73-
pub struct Url {
74-
url: *mut ffi::ada_url,
75-
}
72+
pub struct Url(*mut ffi::ada_url);
7673

7774
/// Clone trait by default uses bit-wise copy.
7875
/// In Rust, FFI requires deep copy, which requires an additional/inexpensive FFI call.
7976
impl Clone for Url {
8077
fn clone(&self) -> Self {
81-
Url {
82-
url: unsafe { ffi::ada_copy(self.url) },
83-
}
78+
unsafe { ffi::ada_copy(self.0).into() }
8479
}
8580
}
8681

8782
impl Drop for Url {
8883
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 }
9091
}
9192
}
9293

@@ -113,9 +114,7 @@ impl Url {
113114
};
114115

115116
if unsafe { ffi::ada_is_valid(url_aggregator) } {
116-
Ok(Url {
117-
url: url_aggregator,
118-
})
117+
Ok(url_aggregator.into())
119118
} else {
120119
Err(Error::ParseUrl(input.to_owned()))
121120
}
@@ -147,7 +146,7 @@ impl Url {
147146

148147
/// Returns the type of the host such as default, ipv4 or ipv6.
149148
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) })
151150
}
152151

153152
/// Return the origin of this URL
@@ -162,7 +161,7 @@ impl Url {
162161
/// ```
163162
pub fn origin(&self) -> &str {
164163
unsafe {
165-
let out = ffi::ada_get_origin(self.url);
164+
let out = ffi::ada_get_origin(self.0);
166165
let slice = std::slice::from_raw_parts(out.data.cast(), out.length);
167166
std::str::from_utf8_unchecked(slice)
168167
}
@@ -172,11 +171,11 @@ impl Url {
172171
///
173172
/// For more information, read [WHATWG URL spec](https://url.spec.whatwg.org/#dom-url-href)
174173
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()
176175
}
177176

178177
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()) }
180179
}
181180

182181
/// Return the username for this URL as a percent-encoded ASCII string.
@@ -190,11 +189,11 @@ impl Url {
190189
/// assert_eq!(url.username(), "rms");
191190
/// ```
192191
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()
194193
}
195194

196195
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()) }
198197
}
199198

200199
/// Return the password for this URL, if any, as a percent-encoded ASCII string.
@@ -208,11 +207,11 @@ impl Url {
208207
/// assert_eq!(url.password(), "secret123");
209208
/// ```
210209
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()
212211
}
213212

214213
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()) }
216215
}
217216

218217
/// Return the port number for this URL, or an empty string.
@@ -229,11 +228,11 @@ impl Url {
229228
/// assert_eq!(url.port(), "8080");
230229
/// ```
231230
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()
233232
}
234233

235234
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()) }
237236
}
238237

239238
/// Return this URL’s fragment identifier, or an empty string.
@@ -254,11 +253,11 @@ impl Url {
254253
/// assert!(url.has_hash());
255254
/// ```
256255
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()
258257
}
259258

260259
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()) }
262261
}
263262

264263
/// Return the parsed representation of the host for this URL with an optional port number.
@@ -272,11 +271,11 @@ impl Url {
272271
/// assert_eq!(url.host(), "127.0.0.1:8080");
273272
/// ```
274273
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()
276275
}
277276

278277
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()) }
280279
}
281280

282281
/// Return the parsed representation of the host for this URL. Non-ASCII domain labels are
@@ -294,11 +293,11 @@ impl Url {
294293
/// assert_eq!(url.hostname(), "127.0.0.1");
295294
/// ```
296295
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()
298297
}
299298

300299
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()) }
302301
}
303302

304303
/// Return the path for this URL, as a percent-encoded ASCII string.
@@ -312,11 +311,11 @@ impl Url {
312311
/// assert_eq!(url.pathname(), "/api/versions");
313312
/// ```
314313
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()
316315
}
317316

318317
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()) }
320319
}
321320

322321
/// Return this URL’s query string, if any, as a percent-encoded ASCII string.
@@ -333,11 +332,11 @@ impl Url {
333332
/// assert_eq!(url.search(), "");
334333
/// ```
335334
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()
337336
}
338337

339338
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()) }
341340
}
342341

343342
/// Return the scheme of this URL, lower-cased, as an ASCII string with the ‘:’ delimiter.
@@ -351,50 +350,50 @@ impl Url {
351350
/// assert_eq!(url.protocol(), "file:");
352351
/// ```
353352
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()
355354
}
356355

357356
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()) }
359358
}
360359

361360
/// A URL includes credentials if its username or password is not the empty string.
362361
pub fn has_credentials(&self) -> bool {
363-
unsafe { ffi::ada_has_credentials(self.url) }
362+
unsafe { ffi::ada_has_credentials(self.0) }
364363
}
365364

366365
/// Returns true if it has an host but it is the empty string.
367366
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) }
369368
}
370369

371370
/// Returns true if it has a host (included an empty host)
372371
pub fn has_hostname(&self) -> bool {
373-
unsafe { ffi::ada_has_hostname(self.url) }
372+
unsafe { ffi::ada_has_hostname(self.0) }
374373
}
375374

376375
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) }
378377
}
379378

380379
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) }
382381
}
383382

384383
pub fn has_port(&self) -> bool {
385-
unsafe { ffi::ada_has_port(self.url) }
384+
unsafe { ffi::ada_has_port(self.0) }
386385
}
387386

388387
pub fn has_password(&self) -> bool {
389-
unsafe { ffi::ada_has_password(self.url) }
388+
unsafe { ffi::ada_has_password(self.0) }
390389
}
391390

392391
pub fn has_hash(&self) -> bool {
393-
unsafe { ffi::ada_has_hash(self.url) }
392+
unsafe { ffi::ada_has_hash(self.0) }
394393
}
395394

396395
pub fn has_search(&self) -> bool {
397-
unsafe { ffi::ada_has_search(self.url) }
396+
unsafe { ffi::ada_has_search(self.0) }
398397
}
399398
/// Returns the parsed version of the URL with all components.
400399
///
@@ -510,7 +509,7 @@ impl From<Url> for String {
510509
impl fmt::Debug for Url {
511510
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
512511
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();
514513
let mut debug = f.debug_struct("Url");
515514
debug
516515
.field("href", &self.href())

0 commit comments

Comments
 (0)