Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

clippy: Map to an error type instead of using allowing result_unit_err in components/url #31834

Merged
merged 9 commits into from
Mar 26, 2024
37 changes: 26 additions & 11 deletions components/url/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */

#![deny(unsafe_code)]
#![allow(clippy::result_unit_err)]
#![crate_name = "servo_url"]
#![crate_type = "rlib"]

Expand All @@ -24,6 +23,14 @@ pub use url::Host;
use url::{Position, Url};

pub use crate::origin::{ImmutableOrigin, MutableOrigin, OpaqueOrigin};
#[derive(Debug)]
pub enum UrlError {
SetUsername,
SetIpHost,
SetPassword,
ToFilePath,
FromFilePath,
}

#[derive(Clone, Deserialize, Eq, Hash, MallocSizeOf, Ord, PartialEq, PartialOrd, Serialize)]
pub struct ServoUrl(#[ignore_malloc_size_of = "Arc"] Arc<Url>);
Expand Down Expand Up @@ -109,16 +116,22 @@ impl ServoUrl {
Arc::make_mut(&mut self.0)
}

pub fn set_username(&mut self, user: &str) -> Result<(), ()> {
self.as_mut_url().set_username(user)
pub fn set_username(&mut self, user: &str) -> Result<(), UrlError> {
self.as_mut_url()
.set_username(user)
.map_err(|_| UrlError::SetUsername)
}

pub fn set_ip_host(&mut self, addr: IpAddr) -> Result<(), ()> {
self.as_mut_url().set_ip_host(addr)
pub fn set_ip_host(&mut self, addr: IpAddr) -> Result<(), UrlError> {
self.as_mut_url()
.set_ip_host(addr)
.map_err(|_| UrlError::SetIpHost)
}

pub fn set_password(&mut self, pass: Option<&str>) -> Result<(), ()> {
self.as_mut_url().set_password(pass)
pub fn set_password(&mut self, pass: Option<&str>) -> Result<(), UrlError> {
self.as_mut_url()
.set_password(pass)
.map_err(|_| UrlError::SetPassword)
}

pub fn set_fragment(&mut self, fragment: Option<&str>) {
Expand All @@ -133,8 +146,8 @@ impl ServoUrl {
self.0.password()
}

pub fn to_file_path(&self) -> Result<::std::path::PathBuf, ()> {
self.0.to_file_path()
pub fn to_file_path(&self) -> Result<::std::path::PathBuf, UrlError> {
self.0.to_file_path().map_err(|_| UrlError::ToFilePath)
}

pub fn host(&self) -> Option<url::Host<&str>> {
Expand Down Expand Up @@ -165,8 +178,10 @@ impl ServoUrl {
self.0.query()
}

pub fn from_file_path<P: AsRef<Path>>(path: P) -> Result<Self, ()> {
Ok(Self::from_url(Url::from_file_path(path)?))
pub fn from_file_path<P: AsRef<Path>>(path: P) -> Result<Self, UrlError> {
Url::from_file_path(path)
.map(Self::from_url)
.map_err(|_| UrlError::FromFilePath)
}

/// Return a non-standard shortened form of the URL. Mainly intended to be
Expand Down