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
60 changes: 45 additions & 15 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 @@ -25,6 +24,29 @@ use url::{Position, Url};

pub use crate::origin::{ImmutableOrigin, MutableOrigin, OpaqueOrigin};

#[derive(Debug)]
pub enum UrlError {
SetUsername,
SetIpHost,
SetPassword,
ToFilePath,
FromFilePath,
}

impl std::fmt::Display for UrlError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
ektuu marked this conversation as resolved.
Show resolved Hide resolved
match self {
UrlError::SetUsername => write!(f, "Error setting username"),
UrlError::SetIpHost => write!(f, "Error setting IP host"),
UrlError::SetPassword => write!(f, "Error setting password"),
UrlError::ToFilePath => write!(f, "Error converting to file path"),
UrlError::FromFilePath => write!(f, "Error converting from file path"),
}
}
}

impl std::error::Error for UrlError {}

#[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 +131,32 @@ 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<(), UrlError> {
self.as_mut_url()
.set_ip_host(addr)
.map_err(|_| UrlError::SetIpHost)
}

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_ip_host(&mut self, addr: IpAddr) -> Result<(), ()> {
self.as_mut_url().set_ip_host(addr)
pub fn to_file_path(&self) -> Result<::std::path::PathBuf, UrlError> {
self.0.to_file_path().map_err(|_| UrlError::ToFilePath)
}

pub fn set_password(&mut self, pass: Option<&str>) -> Result<(), ()> {
self.as_mut_url().set_password(pass)
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)
ektuu marked this conversation as resolved.
Show resolved Hide resolved
}

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

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

pub fn host(&self) -> Option<url::Host<&str>> {
self.0.host()
}
Expand Down Expand Up @@ -165,10 +199,6 @@ 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)?))
}

/// Return a non-standard shortened form of the URL. Mainly intended to be
/// used for debug printing in a constrained space (e.g., thread names).
pub fn debug_compact(&self) -> impl std::fmt::Display + '_ {
Expand Down