use std::num::NonZeroU64;
use std::num::NonZeroUsize;
use crate::ArchiveError;
use crate::ConfigProblem;
use crate::ConfigSetting;
use crate::Result;
#[macro_export]
macro_rules! non_zero {
($value:expr) => {
const {
match ::std::num::NonZero::new($value) {
::std::option::Option::Some(value) => value,
::std::option::Option::None => panic!("non_zero! needs a non-zero value"),
}
}
};
}
#[macro_export]
macro_rules! ratio_limit {
($value:expr) => {
const {
match $crate::RatioLimit::new($value) {
::std::option::Option::Some(ratio) => ratio,
::std::option::Option::None => {
panic!("ratio_limit! needs a finite, positive value")
}
}
}
};
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Limit<T> {
Bounded(T),
Unbounded,
}
const _: () = assert!(size_of::<Limit<NonZeroU64>>() == size_of::<u64>());
impl<T> From<T> for Limit<T> {
fn from(bound: T) -> Self {
Self::Bounded(bound)
}
}
impl Limit<NonZeroU64> {
#[must_use]
pub const fn admits(self, value: u64) -> bool {
match self {
Self::Bounded(bound) => value <= bound.get(),
Self::Unbounded => true,
}
}
#[must_use]
pub const fn ceiling(self) -> u64 {
match self {
Self::Bounded(bound) => bound.get(),
Self::Unbounded => u64::MAX,
}
}
pub fn parse(setting: ConfigSetting, value: u64) -> Result<Self> {
non_zero_u64(setting, value).map(Self::Bounded)
}
}
impl Limit<NonZeroUsize> {
#[must_use]
pub const fn admits(self, value: usize) -> bool {
match self {
Self::Bounded(bound) => value <= bound.get(),
Self::Unbounded => true,
}
}
#[must_use]
pub const fn ceiling(self) -> usize {
match self {
Self::Bounded(bound) => bound.get(),
Self::Unbounded => usize::MAX,
}
}
pub fn parse(setting: ConfigSetting, value: usize) -> Result<Self> {
non_zero_usize(setting, value).map(Self::Bounded)
}
}
pub fn non_zero_u64(setting: ConfigSetting, value: u64) -> Result<NonZeroU64> {
NonZeroU64::new(value).ok_or(ArchiveError::InvalidConfiguration {
setting,
problem: ConfigProblem::Zero,
})
}
pub fn positive_from_i128(setting: ConfigSetting, value: i128) -> Result<NonZeroU64> {
let refuse = |problem| ArchiveError::InvalidConfiguration { setting, problem };
if value < 0 {
return Err(refuse(ConfigProblem::Negative));
}
let value =
u64::try_from(value).map_err(|_| refuse(ConfigProblem::TooLarge { max: u64::MAX }))?;
non_zero_u64(setting, value)
}
const MAX_EXACT_F64: u64 = (1 << 53) - 1;
#[expect(
clippy::cast_possible_truncation,
clippy::cast_sign_loss,
reason = "value is a whole number in 1..=2^53-1 after the checks"
)]
pub fn positive_from_f64(setting: ConfigSetting, value: f64) -> Result<NonZeroU64> {
let refuse = |problem| ArchiveError::InvalidConfiguration { setting, problem };
if !value.is_finite() {
return Err(refuse(ConfigProblem::NotFinite));
}
if value < 0.0 {
return Err(refuse(ConfigProblem::Negative));
}
if value == 0.0 {
return Err(refuse(ConfigProblem::Zero));
}
if value.fract() != 0.0 {
return Err(refuse(ConfigProblem::NotWholeNumber));
}
if value > MAX_EXACT_F64 as f64 {
return Err(refuse(ConfigProblem::TooLarge { max: MAX_EXACT_F64 }));
}
non_zero_u64(setting, value as u64)
}
pub fn non_zero_usize(setting: ConfigSetting, value: usize) -> Result<NonZeroUsize> {
NonZeroUsize::new(value).ok_or(ArchiveError::InvalidConfiguration {
setting,
problem: ConfigProblem::Zero,
})
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct RatioLimit(f64);
impl RatioLimit {
pub fn parse(setting: ConfigSetting, ratio: f64) -> Result<Self> {
let problem = if !ratio.is_finite() {
ConfigProblem::NotFinite
} else if ratio < 0.0 {
ConfigProblem::Negative
} else {
ConfigProblem::Zero
};
Self::new(ratio).ok_or(ArchiveError::InvalidConfiguration { setting, problem })
}
#[must_use]
pub const fn get(self) -> f64 {
self.0
}
#[must_use]
pub const fn new(ratio: f64) -> Option<Self> {
if ratio.is_finite() && ratio > 0.0 {
Some(Self(ratio))
} else {
None
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn zero_is_refused_with_its_setting() {
let err = Limit::<NonZeroU64>::parse(ConfigSetting::MaxFileSize, 0).unwrap_err();
assert_eq!(
err.to_string(),
"invalid configuration: max_file_size must not be zero"
);
assert!(Limit::<NonZeroUsize>::parse(ConfigSetting::MaxFileCount, 0).is_err());
}
#[test]
fn limits_admit_up_to_the_bound() {
let limit = Limit::<NonZeroU64>::parse(ConfigSetting::MaxFileSize, 7).unwrap();
assert!(limit.admits(7));
assert!(!limit.admits(8));
}
#[test]
fn ratios_must_be_finite_and_positive() {
for bad in [0.0, -1.0, f64::NAN, f64::INFINITY] {
assert!(RatioLimit::parse(ConfigSetting::MaxCompressionRatio, bad).is_err());
}
}
}