Skip to content

Commit

Permalink
*: move network requests behind a feature
Browse files Browse the repository at this point in the history
Building with `--no-default-features` _significantly_ decreases the
amount of time it takes to compile (about 1/10th on my machine).
  • Loading branch information
crawford committed Oct 13, 2023
1 parent 8745c92 commit 7b1ddca
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 12 deletions.
6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" ]
7 changes: 5 additions & 2 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<reqwest::Error>),
#[cfg(feature = "network")]
HttpError(std::sync::Arc<reqwest::Error>),
UrlMalformed(ParseError),
ReferenceBroken,
}
Expand Down Expand Up @@ -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),
Expand Down
34 changes: 25 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<Url, Vec<LinkContext>>) -> 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))
Expand All @@ -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(());
}
Expand Down

0 comments on commit 7b1ddca

Please sign in to comment.