Fix lints

This commit is contained in:
Matthias 2021-07-05 02:03:00 +02:00
parent 1546d6ee38
commit afdb721612
4 changed files with 22 additions and 30 deletions

View file

@ -122,8 +122,8 @@ pub(crate) fn extract_links(
} 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 Input::FsPath(root) = &input_content.input {
let link = fs::sanitize(link);
if link.starts_with("#") {
let link = fs::sanitize(&link);
if link.starts_with('#') {
// Silently ignore anchors for now.
continue;
}

View file

@ -3,7 +3,7 @@ use std::path::{Component, Path, PathBuf};
// Returns the base if it is a valid `PathBuf`
fn get_base_dir(base: &Option<Base>) -> Option<PathBuf> {
base.as_ref().and_then(|b| b.dir())
base.as_ref().and_then(Base::dir)
}
/// Normalize a path, removing things like `.` and `..`.
@ -15,15 +15,14 @@ fn get_base_dir(base: &Option<Base>) -> Option<PathBuf> {
/// fail, or on Windows returns annoying device paths. This is a problem Cargo
/// needs to improve on.
///
/// Taken from https://github.com/rust-lang/cargo/blob/fede83ccf973457de319ba6fa0e36ead454d2e20/src/cargo/util/paths.rs#L61
/// Taken from [`cargo`](https://github.com/rust-lang/cargo/blob/fede83ccf973457de319ba6fa0e36ead454d2e20/src/cargo/util/paths.rs#L61)
pub(crate) fn normalize(path: &Path) -> PathBuf {
let mut components = path.components().peekable();
let mut ret = if let Some(c @ Component::Prefix(..)) = components.peek().cloned() {
let mut ret = components.peek().copied().map_or_else(PathBuf::new, |c| {
components.next();
PathBuf::from(c.as_os_str())
} else {
PathBuf::new()
};
});
for component in components {
match component {
@ -54,12 +53,11 @@ pub(crate) fn resolve(src: &Path, dst: &Path, base: &Option<Base>) -> Result<Pat
if dst.is_absolute() {
// Absolute local links (leading slash) require the base_url to
// define the document root.
let base_dir = get_base_dir(base).unwrap_or(
let base_dir = get_base_dir(base).unwrap_or_else(|| {
src.to_path_buf()
.parent()
.map(|p| p.to_path_buf())
.unwrap_or(PathBuf::new()),
);
.map_or(PathBuf::new(), Path::to_path_buf)
});
let abs_path = join(base_dir, dst);
return Ok(normalize(&abs_path));
}
@ -77,10 +75,10 @@ fn join(base: PathBuf, dst: &Path) -> PathBuf {
/// A little helper function to remove the get parameters from a URL link.
/// The link is not a URL but a String as that link may not have a base domain.
pub(crate) fn sanitize(link: String) -> String {
pub(crate) fn sanitize(link: &str) -> String {
let path = match link.split_once('?') {
Some((path, _params)) => path,
None => link.as_str(),
None => link,
};
path.to_string()
}
@ -92,27 +90,18 @@ mod test_fs_tree {
#[test]
fn test_sanitize() {
assert_eq!(sanitize("/".to_string()), "/".to_string());
assert_eq!(sanitize("/"), "/".to_string());
assert_eq!(sanitize("index.html?foo=bar"), "index.html".to_string());
assert_eq!(sanitize("/index.html?foo=bar"), "/index.html".to_string());
assert_eq!(
sanitize("index.html?foo=bar".to_string()),
"index.html".to_string()
);
assert_eq!(
sanitize("/index.html?foo=bar".to_string()),
sanitize("/index.html?foo=bar&baz=zorx?bla=blub"),
"/index.html".to_string()
);
assert_eq!(
sanitize("/index.html?foo=bar&baz=zorx?bla=blub".to_string()),
"/index.html".to_string()
);
assert_eq!(
sanitize("https://example.org/index.html?foo=bar".to_string()),
sanitize("https://example.org/index.html?foo=bar"),
"https://example.org/index.html".to_string()
);
assert_eq!(
sanitize("test.png?foo=bar".to_string()),
"test.png".to_string()
);
assert_eq!(sanitize("test.png?foo=bar"), "test.png".to_string());
}
// dummy root

View file

@ -18,6 +18,7 @@ pub enum Base {
impl Base {
/// Join link with base url
#[must_use]
pub fn join(&self, link: &str) -> Option<Url> {
match self {
Self::Remote(url) => url.join(link).ok(),
@ -26,10 +27,11 @@ impl Base {
}
/// Return the directory if the base is local
#[must_use]
pub fn dir(&self) -> Option<PathBuf> {
match self {
Self::Remote(_) => None,
Self::Local(d) => Some(d.to_path_buf()),
Self::Local(d) => Some(d.clone()),
}
}
}

View file

@ -76,6 +76,7 @@ impl Hash for ErrorKind {
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::InvalidHeader(e) => e.to_string().hash(state),