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
#![deny(
intra_doc_link_resolution_failure,
missing_docs,
missing_debug_implementations,
missing_copy_implementations
)]
#[cfg(test)]
#[macro_use]
extern crate pretty_assertions;
pub const COMPATIBLE_MDBOOK_VERSIONS: &str = "^0.3.0";
mod cache;
mod config;
mod links;
mod validate;
pub use crate::{
cache::Cache,
config::Config,
links::{extract_links, Link},
validate::{
validate, InvalidLink, Reason, UnknownScheme, ValidationOutcome,
},
};
use codespan::{CodeMap, FileName};
use failure::{Error, ResultExt};
use mdbook::book::{Book, BookItem};
use semver::{Version, VersionReq};
pub fn get_config(cfg: &mdbook::Config) -> Result<Config, Error> {
match cfg.get("output.linkcheck") {
Some(raw) => raw
.clone()
.try_into()
.context("Unable to deserialize the `output.linkcheck` table.")
.map_err(Error::from),
None => Ok(Config::default()),
}
}
pub fn version_check(version: &str) -> Result<(), Error> {
let constraints = VersionReq::parse(COMPATIBLE_MDBOOK_VERSIONS)?;
let found = Version::parse(version)?;
if constraints.matches(&found) {
Ok(())
} else {
let msg = format!(
"mdbook-linkcheck isn't compatible with this version of mdbook ({} is not in the range {})",
found, constraints
);
Err(failure::err_msg(msg))
}
}
pub fn book_to_codemap(book: &Book) -> CodeMap {
let mut map = CodeMap::new();
for item in book.iter() {
match item {
BookItem::Chapter(ref ch) => {
map.add_filemap(FileName::real(&ch.path), ch.content.clone());
},
BookItem::Separator => {},
}
}
map
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn always_stay_compatible_with_mdbook_dependency() {
version_check(mdbook::MDBOOK_VERSION).unwrap();
}
}