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
#[macro_use] extern crate log; #[macro_use] extern crate clap; #[macro_use] extern crate lazy_static; use std::path::PathBuf; use crate::link_extractors::link_extractor::MarkupLink; use crate::markup::MarkupFile; 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 ignore_links: Vec<WildMatch>, pub root_dir: Option<PathBuf>, } #[derive(Debug, Clone)] struct FinalResult { link: MarkupLink, result_code: LinkCheckResult, } 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) { 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); } } match &result.result_code { LinkCheckResult::Ok => { print_helper(&result.link, &"OK".green(), "", false); } LinkCheckResult::NotImplemented(msg) => { print_helper(&result.link, &"Warn".yellow(), msg, false); } LinkCheckResult::Warning(msg) => { print_helper(&result.link, &"Warn".yellow(), msg, false); } LinkCheckResult::Ignored(msg) => { print_helper(&result.link, &"Skip".green(), msg, false); } LinkCheckResult::Failed(msg) => { print_helper(&result.link, &"Err".red(), msg, true); } } } pub async fn run(config: &Config) -> Result<(), ()> { let links = find_all_links(&config); let mut link_check_results = stream::iter(links) .map(|link| { async move { let result_code = link_validator::check(&link.source, &link.target, &config).await; FinalResult { link: link, result_code: result_code, } } }) .buffer_unordered(PARALLEL_REQUESTS); let mut skipped = vec![]; let mut errors = vec![]; let mut warnings = vec![]; let mut oks = vec![]; while let Some(result) = link_check_results.next().await { print_result(&result); match &result.result_code { LinkCheckResult::Ok => { oks.push(result.clone()); } LinkCheckResult::NotImplemented(_) | LinkCheckResult::Warning(_) => { warnings.push(result.clone()); } LinkCheckResult::Ignored(_) => { skipped.push(result.clone()); } LinkCheckResult::Failed(_) => { errors.push(result.clone()); } } } println!(); let sum = skipped.len() + errors.len() + warnings.len() + oks.len(); println!("Result ({} links):", sum); println!(); println!("OK {}", oks.len()); println!("Skipped {}", skipped.len()); println!("Warnings {}", warnings.len()); println!("Errors {}", errors.len()); println!(); if errors.len() > 0 { eprintln!(); eprintln!("The following links could not be resolved:"); println!(); for res in errors { let error_msg = format!( "{} ({}, {}) => {}.", res.link.source, res.link.line, res.link.column, res.link.target ); eprintln!("{}", error_msg); } println!(); Err(()) } else { Ok(()) } }