Skip to content

Commit 2574d7b

Browse files
alexpovelanonrig
authored andcommitted
feat!: Error for SetterResult
This implements `std::error::Error` for a newly introduced error type. We cannot implement that trait on `()` in this crate, so cannot offer consumers convenient error handling via `?` etc. (`anyhow`, ...) without a dedicated error type we own.
1 parent 032b8f4 commit 2574d7b

File tree

2 files changed

+8
-11
lines changed

2 files changed

+8
-11
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Here is an example illustrating a common usage:
1212

1313
```Rust
1414
use ada_url::Url;
15-
fn main() -> Result<(), ()> {
15+
fn main() -> Result<(), Box<dyn std::error::Error>> {
1616
let mut u = Url::parse("http://www.google:8080/love#drug", None).expect("bad url");
1717
println!("port: {:?}", u.port());
1818
println!("hash: {:?}", u.hash());

src/lib.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -179,14 +179,19 @@ impl From<*mut ffi::ada_url> for Url {
179179
}
180180
}
181181

182-
type SetterResult = Result<(), ()>;
182+
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Default, Display)]
183+
pub struct SetterError;
184+
185+
impl std::error::Error for SetterError {}
186+
187+
type SetterResult = Result<(), SetterError>;
183188

184189
#[inline]
185190
fn setter_result(successful: bool) -> SetterResult {
186191
if successful {
187192
Ok(())
188193
} else {
189-
Err(())
194+
Err(SetterError)
190195
}
191196
}
192197

@@ -290,7 +295,6 @@ impl Url {
290295
/// url.set_href("https://lemire.me").unwrap();
291296
/// assert_eq!(url.href(), "https://lemire.me/");
292297
/// ```
293-
#[allow(clippy::result_unit_err)]
294298
pub fn set_href(&mut self, input: &str) -> SetterResult {
295299
setter_result(unsafe { ffi::ada_set_href(self.0, input.as_ptr().cast(), input.len()) })
296300
}
@@ -318,7 +322,6 @@ impl Url {
318322
/// url.set_username(Some("username")).unwrap();
319323
/// assert_eq!(url.href(), "https://[email protected]/");
320324
/// ```
321-
#[allow(clippy::result_unit_err)]
322325
pub fn set_username(&mut self, input: Option<&str>) -> SetterResult {
323326
setter_result(unsafe {
324327
ffi::ada_set_username(
@@ -352,7 +355,6 @@ impl Url {
352355
/// url.set_password(Some("password")).unwrap();
353356
/// assert_eq!(url.href(), "https://:[email protected]/");
354357
/// ```
355-
#[allow(clippy::result_unit_err)]
356358
pub fn set_password(&mut self, input: Option<&str>) -> SetterResult {
357359
setter_result(unsafe {
358360
ffi::ada_set_password(
@@ -389,7 +391,6 @@ impl Url {
389391
/// url.set_port(Some("8080")).unwrap();
390392
/// assert_eq!(url.href(), "https://yagiz.co:8080/");
391393
/// ```
392-
#[allow(clippy::result_unit_err)]
393394
pub fn set_port(&mut self, input: Option<&str>) -> SetterResult {
394395
match input {
395396
Some(value) => setter_result(unsafe {
@@ -462,7 +463,6 @@ impl Url {
462463
/// url.set_host(Some("localhost:3000")).unwrap();
463464
/// assert_eq!(url.href(), "https://localhost:3000/");
464465
/// ```
465-
#[allow(clippy::result_unit_err)]
466466
pub fn set_host(&mut self, input: Option<&str>) -> SetterResult {
467467
setter_result(unsafe {
468468
ffi::ada_set_host(
@@ -500,7 +500,6 @@ impl Url {
500500
/// url.set_hostname(Some("localhost")).unwrap();
501501
/// assert_eq!(url.href(), "https://localhost/");
502502
/// ```
503-
#[allow(clippy::result_unit_err)]
504503
pub fn set_hostname(&mut self, input: Option<&str>) -> SetterResult {
505504
setter_result(unsafe {
506505
ffi::ada_set_hostname(
@@ -534,7 +533,6 @@ impl Url {
534533
/// url.set_pathname(Some("/contact")).unwrap();
535534
/// assert_eq!(url.href(), "https://yagiz.co/contact");
536535
/// ```
537-
#[allow(clippy::result_unit_err)]
538536
pub fn set_pathname(&mut self, input: Option<&str>) -> SetterResult {
539537
setter_result(unsafe {
540538
ffi::ada_set_pathname(
@@ -603,7 +601,6 @@ impl Url {
603601
/// url.set_protocol("http").unwrap();
604602
/// assert_eq!(url.href(), "http://yagiz.co/");
605603
/// ```
606-
#[allow(clippy::result_unit_err)]
607604
pub fn set_protocol(&mut self, input: &str) -> SetterResult {
608605
setter_result(unsafe { ffi::ada_set_protocol(self.0, input.as_ptr().cast(), input.len()) })
609606
}

0 commit comments

Comments
 (0)