Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shorten fragment errors #127

Merged
merged 3 commits into from
Nov 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
10 changes: 3 additions & 7 deletions src/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ impl fmt::Display for IoError {

#[derive(Debug, Clone)]
pub enum Link {
File(String),
File(PathBuf),
Http(Url),
}

impl fmt::Display for Link {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Link::File(path) => f.write_str(path),
Link::File(path) => write!(f, "{}", path.display()),
Link::Http(url) => f.write_str(url.as_str()),
}
}
Expand Down Expand Up @@ -237,11 +237,7 @@ fn check_file_fragment(
})
};

is_fragment_available(
&Link::File(path.to_str().unwrap().to_string()),
fragment,
fetch_html,
)
is_fragment_available(&Link::File(path.to_path_buf()), fragment, fetch_html)
}

fn handle_response(url: &Url, resp: ureq::Response) -> Result<ureq::Response, CheckError> {
Expand Down
46 changes: 19 additions & 27 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,30 +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 CheckError::*;

match e {
File(epath) => {
let epath = epath.strip_prefix(&prefix).unwrap_or(&epath);
ret.push_str("\n\tLinked file at path ");
ret.push_str(&epath.display().to_string());
ret.push_str(" does not exist!");
}
Http(_) => ret.push_str(&format!("\n\t{}", e)),
Fragment(_, _, _) => {
ret.push_str("\n\t");
ret.push_str(e.to_string().as_str());
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();
}
Io(_) => ret.push_str(&format!("\n\t{}", e)),
}
}
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).