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

* clippy: fix warnings in components/url

* Fix code formatting issues in components/url/lib.rs

* Update components/url/lib.rs

Co-authored-by: eri <eri@inventati.org>

* made requested changes

---------

Co-authored-by: Ekta Siwach <ektasiwach@Ektas-MacBook-Air.local>
Co-authored-by: eri <eri@inventati.org>
This commit is contained in:
Ekta Siwach 2024-03-26 17:53:07 +05:30 committed by GitHub
parent 2463017c49
commit 68b0be6dc7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -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<Url>);
@ -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<url::Host<&str>> {
@ -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