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 ad6d8ee commit 62eb65e
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 27 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,19 @@
#### Changed

* `walk_dir` now takes `&CheckContext`, not `CheckContext`. [PR#118]
* `Link::File` now stores a `PathBuf`, not a `String`. [PR#127]
* `print_shortened` has been removed; using `Display` directly is recommended instead. [PR#127]
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#127]

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

<a name="0.6.2"></a>
## 0.6.2 (2020-11-27)
Expand Down
45 changes: 19 additions & 26 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,29 +74,20 @@ 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
5 changes: 4 additions & 1 deletion tests/broken_links.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ 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 62eb65e

Please sign in to comment.