Extract DEFAULT_ACCEPTED_STATUS_CODES & apply clippy's suggestions

This commit is contained in:
Thomas Zahner 2025-05-23 14:51:14 +02:00
parent 74961d2470
commit 326f119e38
2 changed files with 17 additions and 5 deletions

View file

@ -33,6 +33,7 @@ use crate::{
checker::{file::FileChecker, mail::MailChecker, website::WebsiteChecker},
filter::{Excludes, Filter, Includes},
remap::Remaps,
types::DEFAULT_ACCEPTED_STATUS_CODES,
};
/// Default number of redirects before a request is deemed as failed, 5.
@ -238,7 +239,7 @@ pub struct ClientBuilder {
/// Set of accepted return codes / status codes.
///
/// Unmatched return codes/ status codes are deemed as errors.
#[builder(default = HashSet::try_from(StatusCodeSelector::default()).unwrap())]
#[builder(default = DEFAULT_ACCEPTED_STATUS_CODES.clone())]
accepted: HashSet<StatusCode>,
/// Response timeout per request in seconds.

View file

@ -1,4 +1,4 @@
use std::{collections::HashSet, fmt::Display, str::FromStr};
use std::{collections::HashSet, fmt::Display, hash::BuildHasher, str::FromStr, sync::LazyLock};
use http::StatusCode;
use serde::{Deserialize, de::Visitor};
@ -6,6 +6,11 @@ use thiserror::Error;
use crate::{AcceptRangeError, types::accept::AcceptRange};
/// These values are the default status codes which are accepted by lychee.
/// SAFETY: This does not panic as all provided status codes are valid.
pub static DEFAULT_ACCEPTED_STATUS_CODES: LazyLock<HashSet<StatusCode>> =
LazyLock::new(|| <HashSet<StatusCode>>::try_from(StatusCodeSelector::default()).unwrap());
#[derive(Debug, Error, PartialEq)]
pub enum StatusCodeSelectorError {
#[error("invalid/empty input")]
@ -45,6 +50,7 @@ impl FromStr for StatusCodeSelector {
}
}
/// These values are the default status codes which are accepted by lychee.
impl Default for StatusCodeSelector {
fn default() -> Self {
Self::new_from(vec![AcceptRange::new(100, 103), AcceptRange::new(200, 299)])
@ -105,7 +111,7 @@ impl StatusCodeSelector {
}
}
impl From<StatusCodeSelector> for HashSet<u16> {
impl<S: BuildHasher + Default> From<StatusCodeSelector> for HashSet<u16, S> {
fn from(value: StatusCodeSelector) -> Self {
value
.ranges
@ -115,7 +121,7 @@ impl From<StatusCodeSelector> for HashSet<u16> {
}
}
impl TryFrom<StatusCodeSelector> for HashSet<StatusCode> {
impl<S: BuildHasher + Default> TryFrom<StatusCodeSelector> for HashSet<StatusCode, S> {
type Error = http::status::InvalidStatusCode;
fn try_from(value: StatusCodeSelector) -> Result<Self, Self::Error> {
@ -190,7 +196,6 @@ impl<'de> Deserialize<'de> for StatusCodeSelector {
#[cfg(test)]
mod test {
use super::*;
use http::status::InvalidStatusCode;
use rstest::rstest;
#[rstest]
@ -261,4 +266,10 @@ mod test {
let actual: HashSet<u16> = StatusCodeSelector::from_str(input).unwrap().into();
assert_eq!(actual, expected);
}
#[test]
fn test_default_accepted_values() {
// assert that accessing the value does not panic as described in the SAFETY note.
let _ = &*DEFAULT_ACCEPTED_STATUS_CODES;
}
}