mlc/link_validator/
mod.rs

1mod file_system;
2mod http;
3mod mail;
4
5pub mod link_type;
6
7use crate::link_extractors::link_extractor::MarkupLink;
8use crate::link_validator::file_system::check_filesystem;
9use crate::link_validator::http::check_http;
10use crate::Config;
11use mail::check_mail;
12
13pub use link_type::get_link_type;
14pub use link_type::LinkType;
15use wildmatch::WildMatch;
16
17#[derive(Debug, Eq, PartialEq, Clone)]
18pub enum LinkCheckResult {
19    Ok,
20    Failed(String),
21    Warning(String),
22    Ignored(String),
23    NotImplemented(String),
24}
25
26pub async fn resolve_target_link(
27    link: &MarkupLink,
28    link_type: &LinkType,
29    config: &Config,
30) -> String {
31    if link_type == &LinkType::FileSystem {
32        file_system::resolve_target_link(&link.source, &link.target, config).await
33    } else {
34        link.target.to_string()
35    }
36}
37
38pub async fn check(
39    link_target: &str,
40    link_type: &LinkType,
41    config: &Config,
42    do_not_warn_for_redirect_to: &[WildMatch],
43    http_headers: &[(String, String)],
44) -> LinkCheckResult {
45    info!("Check link {}.", &link_target);
46    match link_type {
47        LinkType::Ftp => LinkCheckResult::NotImplemented(format!(
48            "Link type '{:?}' is not supported yet...",
49            &link_target
50        )),
51        LinkType::UnknownUrlSchema | LinkType::Unknown => LinkCheckResult::NotImplemented(
52            "Link type is not implemented yet and cannot be checked.".to_string(),
53        ),
54        LinkType::Mail => check_mail(link_target),
55        LinkType::Http => {
56            if config.optional.offline.unwrap_or_default() {
57                LinkCheckResult::Ignored("Ignore web link because of the offline flag.".to_string())
58            } else {
59                check_http(link_target, do_not_warn_for_redirect_to, http_headers).await
60            }
61        }
62        LinkType::FileSystem => check_filesystem(link_target, config).await,
63    }
64}