Skip to content

Commit

Permalink
Cleanup how fragments are shortened and actually shorten them
Browse files Browse the repository at this point in the history
  • Loading branch information
jyn514 committed Nov 28, 2020
1 parent 3f3c7a6 commit 7cbf343
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 22 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@
#### Changed

* `walk_dir` now takes `&CheckContext`, not `CheckContext`. [PR#118]
* `Link::File` now stores a `PathBuf`, not a `String`.
* `print_shortened` has been removed; using `Display` directly is recommended instead.
In particular, it's no longer possible to shorten files without going
through `unavailable_urls`. If you were using this API, please let me know
so I can help design an API that fits your use case; the previous one was a
maintenance burden.
#### Fixed

* Fragment errors are now shortened to use the directory being checked as the base, the same as normal 'file not found errors'.

[PR#118]: https://github.com/deadlinks/cargo-deadlinks/pull/118

Expand Down
39 changes: 18 additions & 21 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ pub struct FileError {

impl fmt::Display for FileError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.print_shortened(None))?;
write!(f, "Found invalid urls in {}:", self.path.display())?;
for e in &self.errors {
write!(f, "\n\t{}", e)?;
}
Ok(())
}
}
Expand All @@ -58,12 +61,11 @@ pub fn walk_dir(dir_path: &Path, ctx: &CheckContext) -> bool {

pool.install(|| {
unavailable_urls(dir_path, ctx)
.map(|err| {
if ctx.verbose {
println!("{}", err);
} else {
println!("{}", err.print_shortened(Some(dir_path)));
.map(|mut err| {
if !ctx.verbose {
err.shorten_all(dir_path);
}
println!("{}", err);
true
})
// ||||||
Expand All @@ -72,24 +74,19 @@ pub fn walk_dir(dir_path: &Path, ctx: &CheckContext) -> bool {
}

impl FileError {
pub fn print_shortened(&self, prefix: Option<&Path>) -> String {
let prefix = prefix.unwrap_or_else(|| Path::new(""));
let filepath = self.path.strip_prefix(&prefix).unwrap_or(&self.path);
let mut ret = format!("Found invalid urls in {}:", filepath.display());

for e in &self.errors {
use std::fmt::Write;

ret.write_str("\n\t").unwrap();
match e {
CheckError::File(epath) => {
let epath = epath.strip_prefix(prefix).unwrap_or(epath);
write!(ret, "Linked file at path {} does not exist!", epath.display()).unwrap();
fn shorten_all(&mut self, prefix: &Path) {
use check::Link;

if let Ok(shortened) = self.path.strip_prefix(&prefix) {
self.path = shortened.to_path_buf();
};
for mut e in &mut self.errors {
if let CheckError::File(epath) | CheckError::Fragment(Link::File(epath), _, _) = &mut e {
if let Ok(shortened) = epath.strip_prefix(prefix) {
*epath = shortened.to_path_buf();
}
_ => write!(ret, "{}", e).unwrap(),
}
}
ret
}
}

Expand Down
3 changes: 2 additions & 1 deletion tests/broken_links.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ fn reports_broken_links() {
.stderr(contains("unresolved link"))
.stdout(
contains("Linked file at path fn.not_here.html does not exist")
.and(contains("Linked file at path links does not exist!")),
.and(contains("Linked file at path links does not exist!"))
.and(contains("Fragment #fragments at index.html does not exist!"))
);
}
1 change: 1 addition & 0 deletions tests/broken_links/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
//! Links to [not here](./fn.not_here.html)
//! with [intra-doc](links)
//! and [links to](#fragments).

0 comments on commit 7cbf343

Please sign in to comment.