diff --git a/components/url/lib.rs b/components/url/lib.rs index 6fc0d145b96..6a9f3766021 100644 --- a/components/url/lib.rs +++ b/components/url/lib.rs @@ -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"] @@ -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); @@ -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>) { @@ -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> { @@ -165,8 +178,10 @@ impl ServoUrl { self.0.query() } - pub fn from_file_path>(path: P) -> Result { - Ok(Self::from_url(Url::from_file_path(path)?)) + pub fn from_file_path>(path: P) -> Result { + 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