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
use std::fmt::Display;
/// A raw URI that got extracted from a document with a fuzzy parser.
/// Note that this can still be invalid according to stricter URI standards
#[derive(Clone, Debug, PartialEq)]
pub struct RawUri {
/// Unparsed URI represented as a `String`. There is no guarantee that it
/// can be parsed into a URI object
pub text: String,
/// Name of the element that contained the URI (e.g. `a` for the <a> tag).
/// This is a way to classify links to make it easier to offer fine control
/// over the links that will be checked e.g. by trying to filter out links
/// that were found in unwanted tags like `<pre>` or `<code>`.
pub element: Option<String>,
/// Name of the attribute that contained the URI (e.g. `src`). This is a way
/// to classify links to make it easier to offer fine control over the links
/// that will be checked e.g. by trying to filter out links that were found
/// in unwanted attributes like `srcset` or `manifest`.
pub attribute: Option<String>,
}
impl RawUri {
// Taken from https://github.com/getzola/zola/blob/master/components/link_checker/src/lib.rs
pub(crate) fn is_anchor(&self) -> bool {
self.text.starts_with('#')
}
}
impl Display for RawUri {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{} (Attribute: {:?})", self.text, self.attribute)
}
}
impl From<&str> for RawUri {
fn from(text: &str) -> Self {
RawUri {
text: text.to_string(),
element: None,
attribute: None,
}
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_is_anchor() {
let raw_uri = RawUri::from("#anchor");
assert!(raw_uri.is_anchor());
let raw_uri = RawUri::from("notan#anchor");
assert!(!raw_uri.is_anchor());
}
}