1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
mod cache;
mod context;
mod filesystem;
mod web;
pub use cache::{Cache, CacheEntry};
pub use context::{BasicContext, Context};
pub use filesystem::{check_filesystem, resolve_link, Options};
#[allow(deprecated)]
pub use web::get;
pub use web::{check_web, head};
use crate::{Category, Link};
use futures::{Future, StreamExt};
use std::path::Path;
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum Reason {
#[error("Linking outside of the \"root\" directory is forbidden")]
TraversesParentDirectories,
#[error("An OS-level error occurred")]
Io(#[from] std::io::Error),
#[error("The web client encountered an error")]
Web(#[from] reqwest::Error),
}
impl Reason {
pub fn file_not_found(&self) -> bool {
match self {
Reason::Io(e) => e.kind() == std::io::ErrorKind::NotFound,
_ => false,
}
}
pub fn timed_out(&self) -> bool {
match self {
Reason::Web(e) => e.is_timeout(),
_ => false,
}
}
}
pub fn validate<'a, L, C>(
current_directory: &'a Path,
links: L,
ctx: &'a C,
) -> impl Future<Output = Outcomes> + 'a
where
L: IntoIterator<Item = Link>,
L::IntoIter: 'a,
C: Context + ?Sized,
{
futures::stream::iter(links)
.map(move |link| validate_one(link, current_directory, ctx))
.buffer_unordered(ctx.concurrency())
.collect()
}
async fn validate_one<C>(
link: Link,
current_directory: &Path,
ctx: &C,
) -> Outcome
where
C: Context + ?Sized,
{
if ctx.should_ignore(&link) {
log::debug!("Ignoring \"{}\"", link.href);
return Outcome::Ignored(link);
}
match link.category() {
Some(Category::FileSystem { path, fragment }) => Outcome::from_result(
link,
check_filesystem(
current_directory,
&path,
fragment.as_deref(),
ctx,
),
),
Some(Category::CurrentFile { fragment }) => {
log::warn!("Not checking \"{}\" in the current file because fragment resolution isn't implemented", fragment);
Outcome::Ignored(link)
},
Some(Category::Url(url)) => {
Outcome::from_result(link, check_web(&url, ctx).await)
},
Some(Category::MailTo(_)) => Outcome::Ignored(link),
None => Outcome::UnknownCategory(link),
}
}
#[derive(Debug, Default)]
pub struct Outcomes {
pub valid: Vec<Link>,
pub invalid: Vec<InvalidLink>,
pub ignored: Vec<Link>,
pub unknown_category: Vec<Link>,
}
impl Outcomes {
pub fn empty() -> Self { Outcomes::default() }
pub fn merge(&mut self, other: Outcomes) {
self.valid.extend(other.valid);
self.invalid.extend(other.invalid);
self.ignored.extend(other.ignored);
self.unknown_category.extend(other.unknown_category);
}
}
impl Extend<Outcome> for Outcomes {
fn extend<T: IntoIterator<Item = Outcome>>(&mut self, items: T) {
for outcome in items {
match outcome {
Outcome::Valid(v) => self.valid.push(v),
Outcome::Invalid(i) => self.invalid.push(i),
Outcome::Ignored(i) => self.ignored.push(i),
Outcome::UnknownCategory(u) => self.unknown_category.push(u),
}
}
}
}
impl Extend<Outcomes> for Outcomes {
fn extend<T: IntoIterator<Item = Outcomes>>(&mut self, items: T) {
for item in items {
self.merge(item);
}
}
}
#[derive(Debug)]
pub struct InvalidLink {
pub link: Link,
pub reason: Reason,
}
#[derive(Debug)]
enum Outcome {
Valid(Link),
Invalid(InvalidLink),
Ignored(Link),
UnknownCategory(Link),
}
impl Outcome {
fn from_result<T, E>(link: Link, result: Result<T, E>) -> Self
where
E: Into<Reason>,
{
match result {
Ok(_) => Outcome::Valid(link),
Err(e) => Outcome::Invalid(InvalidLink {
link,
reason: e.into(),
}),
}
}
}