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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#[macro_use]
extern crate log;
#[macro_use]
extern crate clap;
#[macro_use]
extern crate lazy_static;

use crate::link_extractors::link_extractor::MarkupLink;
use crate::link_validator::link_type::get_link_type;
use crate::link_validator::link_type::LinkType;
use crate::link_validator::resolve_target_link;
use crate::markup::MarkupFile;
use futures::future;
use std::collections::HashMap;
use std::path::PathBuf;
use std::time::Duration;
use tokio::time::throttle;
pub mod cli;
pub mod file_traversal;
pub mod link_extractors;
pub mod link_validator;
pub mod logger;
pub mod markup;
pub use colored::*;
pub use wildmatch::WildMatch;

use futures::{stream, StreamExt};
use link_validator::LinkCheckResult;

const PARALLEL_REQUESTS: usize = 20;

#[derive(Default, Debug)]
pub struct Config {
    pub log_level: logger::LogLevel,
    pub folder: PathBuf,
    pub markup_types: Vec<markup::MarkupType>,
    pub no_web_links: bool,
    pub match_file_extension: bool,
    pub ignore_links: Vec<WildMatch>,
    pub ignore_path: Vec<PathBuf>,
    pub root_dir: Option<PathBuf>,
    pub throttle: u32,
}

#[derive(Debug, Clone)]
struct FinalResult {
    target: Target,
    result_code: LinkCheckResult,
}

#[derive(Hash, Clone, Debug)]
struct Target {
    target: String,
    link_type: LinkType,
}

impl PartialEq for Target {
    fn eq(&self, other: &Self) -> bool {
        self.target == other.target
    }
}
impl Eq for Target {}

fn find_all_links(config: &Config) -> Vec<MarkupLink> {
    let mut files: Vec<MarkupFile> = Vec::new();
    file_traversal::find(&config, &mut files);
    let mut links = vec![];
    for file in files {
        links.append(&mut link_extractors::link_extractor::find_links(&file));
    }
    links
}

fn print_result(result: &FinalResult, map: &HashMap<Target, Vec<MarkupLink>>) {
    fn print_helper(
        link: &MarkupLink,
        status_code: &colored::ColoredString,
        msg: &str,
        error_channel: bool,
    ) {
        let link_str = format!(
            "[{:^4}] {} ({}, {}) => {}. {}",
            status_code, link.source, link.line, link.column, link.target, msg
        );
        if error_channel {
            eprintln!("{}", link_str);
        } else {
            println!("{}", link_str);
        }
    }
    for link in &map[&result.target] {
        match &result.result_code {
            LinkCheckResult::Ok => {
                print_helper(&link, &"OK".green(), "", false);
            }
            LinkCheckResult::NotImplemented(msg) => {
                print_helper(&link, &"Warn".yellow(), msg, false);
            }
            LinkCheckResult::Warning(msg) => {
                print_helper(&link, &"Warn".yellow(), msg, false);
            }
            LinkCheckResult::Ignored(msg) => {
                print_helper(&link, &"Skip".green(), msg, false);
            }
            LinkCheckResult::Failed(msg) => {
                print_helper(&link, &"Err".red(), msg, true);
            }
        }
    }
}

pub async fn run(config: &Config) -> Result<(), ()> {
    let links = find_all_links(&config);
    let mut link_target_groups: HashMap<Target, Vec<MarkupLink>> = HashMap::new();

    for link in &links {
        let link_type = get_link_type(&link.target);
        let target = resolve_target_link(&link, &link_type, config).await;
        let t = Target { target, link_type };
        match link_target_groups.get_mut(&t) {
            Some(v) => v.push(link.clone()),
            None => {
                link_target_groups.insert(t, vec![link.clone()]);
            }
        }
    }

    let do_throttle = config.throttle > 0;
    info!("Use throttle for HTTP: {:?}", do_throttle);
    let mut buffered_stream = stream::iter(link_target_groups.keys())
        .filter(|t| future::ready(!do_throttle || t.link_type != LinkType::HTTP))
        .map(|target| async move {
            let result_code =
                link_validator::check(&target.target, &target.link_type, &config).await;
            FinalResult {
                target: target.clone(),
                result_code: result_code,
            }
        })
        .buffer_unordered(PARALLEL_REQUESTS);
    let mut throttled_stream = throttle(
        Duration::from_millis(config.throttle.into()),
        stream::iter(link_target_groups.keys())
            .filter(|t| future::ready(do_throttle && t.link_type == LinkType::HTTP))
            .map(|target| async move {
                let result_code =
                    link_validator::check(&target.target, &target.link_type, &config).await;
                FinalResult {
                    target: target.clone(),
                    result_code: result_code,
                }
            })
            .buffer_unordered(1),
    );

    let mut skipped = 0;
    let mut oks = 0;
    let mut warnings = 0;
    let mut errors = vec![];

    let mut process_result = |result| {
        print_result(&result, &link_target_groups);
        match &result.result_code {
            LinkCheckResult::Ok => {
                oks += link_target_groups[&result.target].len();
            }
            LinkCheckResult::NotImplemented(_) | LinkCheckResult::Warning(_) => {
                warnings += link_target_groups[&result.target].len();
            }
            LinkCheckResult::Ignored(_) => {
                skipped += link_target_groups[&result.target].len();
            }
            LinkCheckResult::Failed(_) => {
                errors.push(result.clone());
            }
        }
    };

    while let Some(result) = buffered_stream.next().await {
        process_result(result);
    }

    while let Some(result) = throttled_stream.next().await {
        process_result(result);
    }

    println!();
    let error_sum: usize = errors
        .iter()
        .map(|e| link_target_groups[&e.target].len())
        .sum();
    let sum = skipped + error_sum + warnings + oks;
    println!("Result ({} links):", sum);
    println!();
    println!("OK       {}", oks);
    println!("Skipped  {}", skipped);
    println!("Warnings {}", warnings);
    println!("Errors   {}", error_sum);
    println!();

    if errors.len() > 0 {
        eprintln!();
        eprintln!("The following links could not be resolved:");
        println!();
        for res in errors {
            for link in &link_target_groups[&res.target] {
                eprintln!(
                    "{} ({}, {}) => {}.",
                    link.source, link.line, link.column, link.target
                );
            }
        }
        println!();
        Err(())
    } else {
        Ok(())
    }
}