diff --git a/Cargo.toml b/Cargo.toml index 4e074ab..2f5a23c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,6 +14,10 @@ rust-version = "1.70.0" clap = { version = "4.4.6", features = ["cargo", "derive"] } pulldown-cmark = "0.6.0" rayon = "^1.0" -reqwest = { version = "0.11.22", features = ["blocking"] } +reqwest = { version = "0.11.22", features = ["blocking"], optional = true } url = "2.4.1" walkdir = "^2.0" + +[features] +default = [ "network" ] +network = [ "dep:reqwest" ] \ No newline at end of file diff --git a/src/error.rs b/src/error.rs index 59ced15..72d4d1d 100644 --- a/src/error.rs +++ b/src/error.rs @@ -14,15 +14,16 @@ use std::fmt; use std::path::PathBuf; -use std::sync::Arc; use url::ParseError; #[derive(Clone)] pub enum LinkError { PathAbsolute, PathNonExistant, + #[cfg(feature = "network")] HttpStatus(reqwest::StatusCode), - HttpError(Arc), + #[cfg(feature = "network")] + HttpError(std::sync::Arc), UrlMalformed(ParseError), ReferenceBroken, } @@ -62,7 +63,9 @@ impl fmt::Display for LocatedDocumentError { let (title, detail): (&str, Option<&dyn fmt::Display>) = match *error { LinkError::PathAbsolute => ("Found absolute path", None), LinkError::PathNonExistant => ("Found broken path", None), + #[cfg(feature = "network")] LinkError::HttpStatus(ref status) => ("Found broken url", Some(status)), + #[cfg(feature = "network")] LinkError::HttpError(ref err) => ("HTTP failure", Some(err)), LinkError::UrlMalformed(ref err) => ("Found malformed URL", Some(err)), LinkError::ReferenceBroken => ("Found broken reference", None), diff --git a/src/main.rs b/src/main.rs index 26d6f85..521c414 100644 --- a/src/main.rs +++ b/src/main.rs @@ -15,19 +15,15 @@ mod document; mod error; +use clap::Parser; use document::{Document, Error, Event}; use error::{DocumentError, DocumentLocation, LinkError, LocatedDocumentError}; -use rayon::prelude::*; use std::collections::HashMap; use std::ffi::OsStr; use std::fs::File; use std::io::Read; use std::path::{Path, PathBuf}; use std::process::exit; -use std::sync::Arc; -use std::time::Duration; -use clap::Parser; -use reqwest::{blocking::Client, StatusCode}; use url::{ParseError, Url}; use walkdir::WalkDir; @@ -173,6 +169,24 @@ fn main() { } } + #[cfg(feature = "network")] + { + found_error |= check_urls(urls); + } + + if found_error { + exit(1) + } +} + +#[cfg(feature = "network")] +fn check_urls(urls: HashMap>) -> bool { + use rayon::prelude::*; + use reqwest::blocking::Client; + use std::time::Duration; + + let mut found_error = false; + let client = match Client::builder() .user_agent(format!("marker/{}", clap::crate_version!())) .timeout(Duration::from_secs(10)) @@ -194,12 +208,14 @@ fn main() { } } - if found_error { - exit(1) - } + found_error } -fn check_url(client: &Client, url: &Url) -> Result<(), LinkError> { +#[cfg(feature = "network")] +fn check_url(client: &reqwest::blocking::Client, url: &Url) -> Result<(), LinkError> { + use reqwest::StatusCode; + use std::sync::Arc; + if url.scheme() != "http" && url.scheme() != "https" { return Ok(()); }