Skip to content

Commit

Permalink
Remove unused code
Browse files Browse the repository at this point in the history
  • Loading branch information
trask committed Dec 1, 2024
1 parent 24c2e12 commit 4d3ef2b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 89 deletions.
9 changes: 0 additions & 9 deletions lychee-lib/src/types/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,6 @@ impl Base {
}
}

/// Return the directory if the base is local
#[must_use]
pub(crate) fn dir(&self) -> Option<PathBuf> {
match self {
Self::Remote(_) => None,
Self::Local(d) => Some(d.clone()),
}
}

pub(crate) fn from_source(source: &InputSource) -> Option<Base> {
match &source {
InputSource::RemoteUrl(url) => {
Expand Down
80 changes: 15 additions & 65 deletions lychee-lib/src/utils/path.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{Base, ErrorKind, Result};
use crate::{ErrorKind, Result};
use cached::proc_macro::cached;
use once_cell::sync::Lazy;
use path_clean::PathClean;
Expand All @@ -9,11 +9,6 @@ use std::path::{Path, PathBuf};
static CURRENT_DIR: Lazy<PathBuf> =
Lazy::new(|| env::current_dir().expect("cannot get current dir from environment"));

/// Returns the base if it is a valid `PathBuf`
fn get_base_dir(base: &Option<Base>) -> Option<PathBuf> {
base.as_ref().and_then(Base::dir)
}

/// Create an absolute path out of a `PathBuf`.
///
/// The `clean` method is relatively expensive
Expand Down Expand Up @@ -44,7 +39,6 @@ pub(crate) fn resolve(
src: &Path,
dst: &PathBuf,
root_path: &Option<PathBuf>,
base: &Option<Base>,
) -> Result<Option<PathBuf>> {
let resolved = match dst {
relative if !dst.starts_with("/") => {
Expand All @@ -55,31 +49,18 @@ pub(crate) fn resolve(
parent.join(relative)
}
absolute if dst.starts_with("/") => {
let with_root_path = match root_path {
Some(root) => &join(root.to_path_buf(), absolute),
None => absolute,
// Absolute local links (leading slash) are ignored unless
// root_path is provided
let Some(base) = root_path else {
return Ok(None);
};
let Some(dir) = dirname(&base) else {
return Err(ErrorKind::InvalidBase(
base.display().to_string(),
"The given directory cannot be a base".to_string(),
));
};
match get_base_dir(base) {
Some(base) => {
let Some(dir) = dirname(&base) else {
return Err(ErrorKind::InvalidBase(
base.display().to_string(),
"The given directory cannot be a base".to_string(),
));
};
join(dir.to_path_buf(), with_root_path)
}
None => {
if root_path.is_some() {
with_root_path.to_path_buf()
} else {
// Absolute local links (leading slash) require the `base_url` to
// define the document root. Silently ignore the link in case the
// `base_url` is not defined.
return Ok(None);
}
}
}
join(dir.to_path_buf(), absolute)
}
_ => return Err(ErrorKind::InvalidFile(dst.to_path_buf())),
};
Expand Down Expand Up @@ -127,7 +108,7 @@ mod test_path {
let dummy = PathBuf::from("index.html");
let abs_path = PathBuf::from("./foo.html");
assert_eq!(
resolve(&dummy, &abs_path, &None, &None)?,
resolve(&dummy, &abs_path, &None)?,
Some(env::current_dir().unwrap().join("foo.html"))
);
Ok(())
Expand All @@ -140,7 +121,7 @@ mod test_path {
let dummy = PathBuf::from("./index.html");
let abs_path = PathBuf::from("./foo.html");
assert_eq!(
resolve(&dummy, &abs_path, &None, &None)?,
resolve(&dummy, &abs_path, &None)?,
Some(env::current_dir().unwrap().join("foo.html"))
);
Ok(())
Expand All @@ -153,43 +134,12 @@ mod test_path {
let abs_index = PathBuf::from("/path/to/index.html");
let abs_path = PathBuf::from("./foo.html");
assert_eq!(
resolve(&abs_index, &abs_path, &None, &None)?,
resolve(&abs_index, &abs_path, &None)?,
Some(PathBuf::from("/path/to/foo.html"))
);
Ok(())
}

// dummy
// foo.html
// valid base dir
#[test]
fn test_resolve_absolute_from_base_dir() -> Result<()> {
let dummy = PathBuf::new();
let abs_path = PathBuf::from("/foo.html");
let base = Some(Base::Local(PathBuf::from("/some/absolute/base/dir")));
assert_eq!(
resolve(&dummy, &abs_path, &None, &base)?,
Some(PathBuf::from("/some/absolute/base/dir/foo.html"))
);
Ok(())
}

// /path/to/index.html
// /other/path/to/foo.html
#[test]
fn test_resolve_absolute_from_absolute() -> Result<()> {
let abs_index = PathBuf::from("/path/to/index.html");
let abs_path = PathBuf::from("/other/path/to/foo.html");
let base = Some(Base::Local(PathBuf::from("/some/absolute/base/dir")));
assert_eq!(
resolve(&abs_index, &abs_path, &None, &base)?,
Some(PathBuf::from(
"/some/absolute/base/dir/other/path/to/foo.html"
))
);
Ok(())
}

#[test]
fn test_contains() {
let parent_dir = tempfile::tempdir().unwrap();
Expand Down
21 changes: 6 additions & 15 deletions lychee-lib/src/utils/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,7 @@ fn try_parse_into_uri(
None => return Err(ErrorKind::InvalidBaseJoin(text.clone())),
},
None => match source {
InputSource::FsPath(root) => {
create_uri_from_file_path(root, &text, root_path, base)?
}
InputSource::FsPath(root) => create_uri_from_file_path(root, &text, root_path)?,
_ => return Err(ErrorKind::UnsupportedUriType(text)),
},
},
Expand All @@ -90,7 +88,6 @@ fn create_uri_from_file_path(
file_path: &Path,
link_text: &str,
root_path: &Option<PathBuf>,
base: &Option<Base>,
) -> Result<Uri> {
let target_path = if is_anchor(link_text) {
// For anchors, we need to append the anchor to the file name.
Expand All @@ -103,8 +100,7 @@ fn create_uri_from_file_path(
} else {
link_text.to_string()
};
let Ok(constructed_url) = resolve_and_create_url(file_path, &target_path, root_path, base)
else {
let Ok(constructed_url) = resolve_and_create_url(file_path, &target_path, root_path) else {
return Err(ErrorKind::InvalidPathToUri(target_path));
};
Ok(Uri {
Expand Down Expand Up @@ -170,20 +166,16 @@ fn resolve_and_create_url(
src_path: &Path,
dest_path: &str,
root_path: &Option<PathBuf>,
base_uri: &Option<Base>,
) -> Result<Url> {
let (dest_path, fragment) = url::remove_get_params_and_separate_fragment(dest_path);

// Decode the destination path to avoid double-encoding
// This addresses the issue mentioned in the original comment about double-encoding
let decoded_dest = percent_decode_str(dest_path).decode_utf8()?;

let Ok(Some(resolved_path)) = path::resolve(
src_path,
&PathBuf::from(&*decoded_dest),
root_path,
base_uri,
) else {
let Ok(Some(resolved_path)) =
path::resolve(src_path, &PathBuf::from(&*decoded_dest), root_path)
else {
return Err(ErrorKind::InvalidPathToUri(decoded_dest.to_string()));
};

Expand All @@ -208,8 +200,7 @@ mod tests {
#[test]
fn test_create_uri_from_path() {
let result =
resolve_and_create_url(&PathBuf::from("/README.md"), "test+encoding", &None, &None)
.unwrap();
resolve_and_create_url(&PathBuf::from("/README.md"), "test+encoding", &None).unwrap();
assert_eq!(result.as_str(), "file:///test+encoding");
}

Expand Down

0 comments on commit 4d3ef2b

Please sign in to comment.