From b2ce61357fda9d7544f564b583f9698be132aa8d Mon Sep 17 00:00:00 2001 From: Matthias Date: Mon, 6 Sep 2021 23:46:31 +0200 Subject: [PATCH] Fix build errors; cleanup code --- lychee-lib/src/client.rs | 4 ++-- lychee-lib/src/extract.rs | 8 ++++---- lychee-lib/src/types/error.rs | 6 ++---- lychee-lib/src/types/uri.rs | 16 ++++++++-------- 4 files changed, 16 insertions(+), 18 deletions(-) diff --git a/lychee-lib/src/client.rs b/lychee-lib/src/client.rs index e8d477f..8fdb0a1 100644 --- a/lychee-lib/src/client.rs +++ b/lychee-lib/src/client.rs @@ -93,7 +93,7 @@ pub struct ClientBuilder { accepted: Option>, /// Response timeout per request timeout: Option, - /// Treat HTTP links as erros when HTTPS is available + /// Treat HTTP links as errors when HTTPS is available require_https: bool, } @@ -252,7 +252,7 @@ impl Client { } pub async fn check_file(&self, uri: &Uri) -> Status { - if let Ok(path) = uri.inner.to_file_path() { + if let Ok(path) = uri.url.to_file_path() { if path.exists() { return Status::Ok(StatusCode::OK); } diff --git a/lychee-lib/src/extract.rs b/lychee-lib/src/extract.rs index 0a48b33..672150f 100644 --- a/lychee-lib/src/extract.rs +++ b/lychee-lib/src/extract.rs @@ -32,15 +32,15 @@ pub(crate) fn extract_links( for link in links { let req = if let Ok(uri) = Uri::try_from(link.as_str()) { Request::new(uri, input_content.input.clone()) - } else if let Some(new_url) = base.as_ref().and_then(|u| u.join(&link)) { - Request::new(Uri { inner: new_url }, input_content.input.clone()) + } else if let Some(url) = base.as_ref().and_then(|u| u.join(&link)) { + Request::new(Uri { url }, input_content.input.clone()) } else if let Input::FsPath(root) = &input_content.input { if url::is_anchor(&link) { // Silently ignore anchor links for now continue; } - let uri = create_uri_from_path(root, base, &link)?; - Request::new(Uri { inner: uri }, input_content.input.clone()) + let url = create_uri_from_path(root, base, &link)?; + Request::new(Uri { url }, input_content.input.clone()) } else { info!("Handling of {} not implemented yet", &link); continue; diff --git a/lychee-lib/src/types/error.rs b/lychee-lib/src/types/error.rs index 1dd68d9..208e0af 100644 --- a/lychee-lib/src/types/error.rs +++ b/lychee-lib/src/types/error.rs @@ -74,11 +74,10 @@ impl Hash for ErrorKind { Self::HubcapsError(e) => e.to_string().hash(state), Self::FileNotFound(e) => e.to_string_lossy().hash(state), Self::UrlParseError(s, e) => (s, e.type_id()).hash(state), - Self::UnreachableEmailAddress(u) | Self::InsecureURL(u) => u.hash(state), Self::InvalidFileUri(u) => u.hash(state), - Self::InvalidFileUri(f) => f.hash(state), Self::InvalidPath(p) => p.hash(state), - Self::UnreachableEmailAddress(u) => u.hash(state), + Self::UnreachableEmailAddress(u) | Self::InsecureURL(u) => u.hash(state), + Self::InvalidBase(base, e) => (base, e).hash(state), Self::InvalidHeader(e) => e.to_string().hash(state), Self::InvalidGlobPattern(e) => e.to_string().hash(state), Self::MissingGitHubToken => std::mem::discriminant(self).hash(state), @@ -123,7 +122,6 @@ impl Display for ErrorKind { "This URL is available in HTTPS protocol, but HTTP is provided, use '{}' instead", uri ), - Self::InvalidBase(base, e) => write!(f, "Error while base dir `{}` : {}", base, e), Self::InvalidBase(base, e) => write!(f, "Error with base dir `{}` : {}", base, e), } } diff --git a/lychee-lib/src/types/uri.rs b/lychee-lib/src/types/uri.rs index aaf7d0c..edb6fc7 100644 --- a/lychee-lib/src/types/uri.rs +++ b/lychee-lib/src/types/uri.rs @@ -6,14 +6,14 @@ use url::Url; use crate::{ErrorKind, Result}; -/// Lychee's own representation of a URI, which encapsulates all supported formats. +/// Lychee's own representation of a URI, which encapsulates all support formats. /// /// If the scheme is `mailto`, it's a mail address. /// Otherwise it's treated as a website URL. #[derive(Clone, Debug, PartialOrd, Ord, PartialEq, Eq, Hash, Serialize, Deserialize)] pub struct Uri { /// Website URL or mail address - pub(crate) inner: Url, + pub(crate) url: Url, } impl Uri { @@ -24,21 +24,21 @@ impl Uri { #[inline] #[must_use] pub fn as_str(&self) -> &str { - self.inner.as_ref().trim_start_matches("mailto:") + self.url.as_ref().trim_start_matches("mailto:") } #[inline] #[must_use] /// Returns the scheme of the URI (e.g. `http` or `mailto`) pub fn scheme(&self) -> &str { - self.inner.scheme() + self.url.scheme() } #[inline] #[must_use] /// Returns the domain of the URI (e.g. `example.org`) pub fn domain(&self) -> Option<&str> { - self.inner.domain() + self.url.domain() } #[inline] @@ -49,14 +49,14 @@ impl Uri { /// /// Return `None` for cannot-be-a-base URLs. pub fn path_segments(&self) -> Option> { - self.inner.path_segments() + self.url.path_segments() } #[must_use] /// Returns the IP address (either IPv4 or IPv6) of the URI, /// or `None` if it is a domain pub fn host_ip(&self) -> Option { - match self.inner.host()? { + match self.url.host()? { url::Host::Domain(_) => None, url::Host::Ipv4(v4_addr) => Some(v4_addr.into()), url::Host::Ipv6(v6_addr) => Some(v6_addr.into()), @@ -100,7 +100,7 @@ impl AsRef for Uri { impl From for Uri { fn from(url: Url) -> Self { - Self { inner: url } + Self { url } } }