Skip to content

Commit

Permalink
fix typos; extend docs
Browse files Browse the repository at this point in the history
  • Loading branch information
mre committed Oct 14, 2024
1 parent f9a44a3 commit bd6a4c4
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 24 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ Options:
[default: 1d]
--cache-exclude-status <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:
Expand Down
42 changes: 30 additions & 12 deletions lychee-bin/src/commands/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u16>) -> bool {
let status_code_excluded = status
.code()
Expand Down Expand Up @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion lychee-bin/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions lychee-lib/src/types/accept/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use thiserror::Error;
static RANGE_PATTERN: Lazy<Regex> =
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.
Expand Down
10 changes: 6 additions & 4 deletions lychee-lib/src/types/status_code/excluder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<AcceptRange>,
Expand Down
10 changes: 6 additions & 4 deletions lychee-lib/src/types/status_code/selector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<AcceptRange>,
Expand Down

0 comments on commit bd6a4c4

Please sign in to comment.