Skip to content

Commit

Permalink
Merge pull request #25 from crawford/features
Browse files Browse the repository at this point in the history
*: move network requests behind a feature
  • Loading branch information
crawford committed Oct 13, 2023
2 parents 8745c92 + a64090a commit df7c833
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 13 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ jobs:
run: cargo fmt --check

- name: Clippy check
run: cargo clippy --no-deps -- --deny=warnings
run: |
cargo clippy --no-deps -- --deny=warnings
cargo clippy --no-deps --no-default-features -- --deny=warnings
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 df7c833

Please sign in to comment.