From bd6a4c4b3713aec3f0732250f3489686f21b636c Mon Sep 17 00:00:00 2001 From: Matthias Date: Mon, 14 Oct 2024 02:30:28 +0200 Subject: [PATCH] fix typos; extend docs --- README.md | 2 +- lychee-bin/src/commands/check.rs | 42 ++++++++++++++------ lychee-bin/src/options.rs | 2 +- lychee-lib/src/types/accept/range.rs | 4 +- lychee-lib/src/types/status_code/excluder.rs | 10 +++-- lychee-lib/src/types/status_code/selector.rs | 10 +++-- 6 files changed, 46 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index a2f8a72ef3..763eb22327 100644 --- a/README.md +++ b/README.md @@ -336,7 +336,7 @@ Options: [default: 1d] --cache-exclude-status - A List status codes that will be ignored from the cache + A list of status codes that will be ignored from the cache The following accept range syntax is supported: [start]..[=]end|code. Some valid examples are: diff --git a/lychee-bin/src/commands/check.rs b/lychee-bin/src/commands/check.rs index 4bd8ac2d19..fd654998c0 100644 --- a/lychee-bin/src/commands/check.rs +++ b/lychee-bin/src/commands/check.rs @@ -309,6 +309,14 @@ async fn handle( response } +/// Returns `true` if the response should be ignored in the cache. +/// +/// The response should be ignored if: +/// - The URI is a file URI. +/// - The status is excluded. +/// - The status is unsupported. +/// - The status is unknown. +/// - The status code is excluded from the cache. fn ignore_cache(uri: &Uri, status: &Status, cache_exclude_status: &HashSet) -> bool { let status_code_excluded = status .code() @@ -433,39 +441,49 @@ mod tests { } #[test] - fn test_ignore_cache() { - let mut exclude = HashSet::new(); - - // Cache is not ignored + fn test_cache_by_default() { assert!(!ignore_cache( &Uri::try_from("https://[::1]").unwrap(), &Status::Ok(StatusCode::OK), - &exclude + &HashSet::default() )); + } - // Cache is ignored for file URLs + #[test] + // Cache is ignored for file URLs + fn test_cache_ignore_file_urls() { assert!(ignore_cache( &Uri::try_from("file:///home").unwrap(), &Status::Ok(StatusCode::OK), - &exclude + &HashSet::default() )); + } - // Cache is ignored for unsupported status + #[test] + // Cache is ignored for unsupported status + fn test_cache_ignore_unsupported_status() { assert!(ignore_cache( &Uri::try_from("https://[::1]").unwrap(), &Status::Unsupported(ErrorKind::EmptyUrl), - &exclude + &HashSet::default() )); + } - // Cache is ignored for unknown status + #[test] + // Cache is ignored for unknown status + fn test_cache_ignore_unknown_status() { assert!(ignore_cache( &Uri::try_from("https://[::1]").unwrap(), &Status::UnknownStatusCode(StatusCode::IM_A_TEAPOT), - &exclude + &HashSet::default() )); + } + #[test] + fn test_cache_ignore_excluded_status() { // Cache is ignored for excluded status codes - exclude.insert(200); + let exclude = [StatusCode::OK.as_u16()].iter().copied().collect(); + assert!(ignore_cache( &Uri::try_from("https://[::1]").unwrap(), &Status::Ok(StatusCode::OK), diff --git a/lychee-bin/src/options.rs b/lychee-bin/src/options.rs index 1a0b12a4f5..988dc9aa60 100644 --- a/lychee-bin/src/options.rs +++ b/lychee-bin/src/options.rs @@ -236,7 +236,7 @@ pub(crate) struct Config { #[arg( long, default_value_t, - long_help = "A List status codes that will be ignored from the cache + long_help = "A list of status codes that will be ignored from the cache The following accept range syntax is supported: [start]..[=]end|code. Some valid examples are: diff --git a/lychee-lib/src/types/accept/range.rs b/lychee-lib/src/types/accept/range.rs index 2f4c7b914b..3707404b6b 100644 --- a/lychee-lib/src/types/accept/range.rs +++ b/lychee-lib/src/types/accept/range.rs @@ -7,8 +7,8 @@ use thiserror::Error; static RANGE_PATTERN: Lazy = Lazy::new(|| Regex::new(r"^([0-9]{3})?\.\.(=?)([0-9]{3})+$|^([0-9]{3})$").unwrap()); -/// The [`AcceptRangeParseError`] indicates that the parsing process of an -/// [`AcceptRange`] from a string failed due to various underlying reasons. +/// Indicates that the parsing process of an [`AcceptRange`] from a string +/// failed due to various underlying reasons. #[derive(Debug, Error, PartialEq)] pub enum AcceptRangeError { /// The string input didn't contain any range pattern. diff --git a/lychee-lib/src/types/status_code/excluder.rs b/lychee-lib/src/types/status_code/excluder.rs index e8f30517ef..34068eb203 100644 --- a/lychee-lib/src/types/status_code/excluder.rs +++ b/lychee-lib/src/types/status_code/excluder.rs @@ -6,10 +6,12 @@ use crate::{ types::accept::AcceptRange, types::status_code::StatusCodeSelectorError, AcceptRangeError, }; -/// An [`StatusCodeExcluder`] holds ranges of HTTP status codes, and determines whether a specific -/// code is matched, so the link can be counted as valid (not broken) or excluded. -/// `StatusCodeExcluder` differs from `StatusCodeSelector` in the defaults it provides. As this is -/// meant to exclude status codes, the default is to keep everything. +/// A [`StatusCodeExcluder`] holds ranges of HTTP status codes, and determines +/// whether a specific code is matched, so the link can be counted as valid (not +/// broken) or excluded. `StatusCodeExcluder` differs from +/// [`StatusCodeSelector`](super::selector::StatusCodeSelector) in the defaults +/// it provides. As this is meant to exclude status codes, the default is to +/// keep everything. #[derive(Clone, Debug, PartialEq)] pub struct StatusCodeExcluder { ranges: Vec, diff --git a/lychee-lib/src/types/status_code/selector.rs b/lychee-lib/src/types/status_code/selector.rs index 9530e288af..060e097227 100644 --- a/lychee-lib/src/types/status_code/selector.rs +++ b/lychee-lib/src/types/status_code/selector.rs @@ -14,10 +14,12 @@ pub enum StatusCodeSelectorError { AcceptRangeError(#[from] AcceptRangeError), } -/// An [`StatusCodeSelector`] holds ranges of HTTP status codes, and determines whether a specific -/// code is matched, so the link can be counted as valid (not broken) or excluded. -/// `StatusCodeSelector` differs from `StatusCodeExcluder` in the defaults it provides. As this is -/// meant to select valid status codes, the default includes every successful status. +/// A [`StatusCodeSelector`] holds ranges of HTTP status codes, and determines +/// whether a specific code is matched, so the link can be counted as valid (not +/// broken) or excluded. `StatusCodeSelector` differs from +/// [`StatusCodeExcluder`](super::excluder::StatusCodeExcluder) +/// in the defaults it provides. As this is meant to +/// select valid status codes, the default includes every successful status. #[derive(Clone, Debug, PartialEq)] pub struct StatusCodeSelector { ranges: Vec,