use std::{fmt, str::FromStr};
use serde::{Deserialize, Deserializer, Serialize, Serializer, de};
#[derive(Copy, Clone, PartialEq, Eq, Hash, Default, PartialOrd, Ord)]
pub struct ConfigSize(u64);
const EXPECTED_UNITS: &str = "expected one of 'k', 'M', 'G', 'T', 'P', 'E', 'Ki', 'Mi', 'Gi', \
'Ti', 'Pi', 'Ei', each with or without a trailing 'B'";
#[derive(Debug, thiserror::Error, Eq, PartialEq)]
#[non_exhaustive]
pub enum ParseError {
#[error("empty size: write a number of bytes, or a number and a unit such as `512Mi`")]
Empty,
#[error("unknown unit `{unit}` in size `{input}`: {EXPECTED_UNITS}")]
InvalidUnit { unit: String, input: String },
#[error("`{unit}` in size `{input}` has no number in front of it")]
MissingNumber { unit: String, input: String },
#[error("size `{input}` has no digits")]
NoDigits { input: String },
#[error("size `{input}` cannot be negative")]
Negative { input: String },
#[error("size `{input}` is too large: at most {} bytes", u64::MAX)]
Overflow { input: String },
}
impl ConfigSize {
pub const ZERO: ConfigSize = ConfigSize(0);
pub const MAX: ConfigSize = ConfigSize(u64::MAX);
pub const fn from_bytes(bytes: u64) -> Self {
Self(bytes)
}
pub const fn from_kib(kib: u64) -> Self {
Self(kib.saturating_mul(1 << 10))
}
pub const fn from_mib(mib: u64) -> Self {
Self(mib.saturating_mul(1 << 20))
}
pub const fn from_gib(gib: u64) -> Self {
Self(gib.saturating_mul(1 << 30))
}
pub const fn from_mb(mb: u64) -> Self {
Self(mb.saturating_mul(1_000_000))
}
pub const fn as_bytes(self) -> u64 {
self.0
}
pub const fn as_usize(self) -> usize {
if self.0 > usize::MAX as u64 {
usize::MAX
} else {
self.0 as usize
}
}
pub const fn is_zero(self) -> bool {
self.0 == 0
}
pub const fn saturating_sub(self, other: Self) -> Self {
Self(self.0.saturating_sub(other.0))
}
}
impl From<u64> for ConfigSize {
fn from(bytes: u64) -> Self {
Self(bytes)
}
}
impl From<ConfigSize> for u64 {
fn from(ConfigSize(bytes): ConfigSize) -> Self {
bytes
}
}
impl PartialEq<u64> for ConfigSize {
fn eq(&self, other: &u64) -> bool {
self.0 == *other
}
}
impl PartialOrd<u64> for ConfigSize {
fn partial_cmp(&self, other: &u64) -> Option<std::cmp::Ordering> {
self.0.partial_cmp(other)
}
}
const UNITS: [(&str, u128); 13] = [
("Ki", 1 << 10),
("Mi", 1 << 20),
("Gi", 1 << 30),
("Ti", 1 << 40),
("Pi", 1 << 50),
("Ei", 1 << 60),
("k", 1_000),
("K", 1_000),
("M", 1_000_000),
("G", 1_000_000_000),
("T", 1_000_000_000_000),
("P", 1_000_000_000_000_000),
("E", 1_000_000_000_000_000_000),
];
const MAX_FRACTION_DIGITS: usize = 18;
impl FromStr for ConfigSize {
type Err = ParseError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Self::from_str_with_unit(s).map(|(size, _has_unit)| size)
}
}
impl ConfigSize {
pub fn from_str_with_unit(s: &str) -> Result<(Self, bool), ParseError> {
let input = s;
let overflow = || ParseError::Overflow {
input: input.to_string(),
};
if input.starts_with('-') {
return Err(ParseError::Negative {
input: input.to_string(),
});
}
let body = input.strip_prefix('+').unwrap_or(input);
if body.is_empty() {
return Err(ParseError::Empty);
}
let whole_end = body
.bytes()
.position(|b| !b.is_ascii_digit())
.unwrap_or(body.len());
let (whole, mut rest) = body.split_at(whole_end);
let mut fraction = "";
if let Some(after_point) = rest.strip_prefix('.') {
let fraction_end = after_point
.bytes()
.position(|b| !b.is_ascii_digit())
.unwrap_or(after_point.len());
(fraction, rest) = after_point.split_at(fraction_end);
}
let unit = rest;
if whole.is_empty() && fraction.is_empty() {
return Err(if unit.is_empty() {
ParseError::NoDigits {
input: input.to_string(),
}
} else if unit_bytes(unit).is_some() {
ParseError::MissingNumber {
unit: unit.to_string(),
input: input.to_string(),
}
} else {
ParseError::InvalidUnit {
unit: unit.to_string(),
input: input.to_string(),
}
});
}
let Some(bytes_per_unit) = unit_bytes(unit) else {
return Err(ParseError::InvalidUnit {
unit: unit.to_string(),
input: input.to_string(),
});
};
let whole: u128 = if whole.is_empty() {
0
} else {
whole.parse().map_err(|_| overflow())?
};
let mut total = whole.checked_mul(bytes_per_unit).ok_or_else(overflow)?;
if !fraction.is_empty() {
let kept = fraction.len().min(MAX_FRACTION_DIGITS);
let numerator: u128 = fraction[..kept].parse().map_err(|_| overflow())?;
let denominator = 10u128.pow(kept as u32);
let scaled = bytes_per_unit.checked_mul(numerator).ok_or_else(overflow)?;
let rounded = (scaled + denominator / 2) / denominator;
total = total.checked_add(rounded).ok_or_else(overflow)?;
}
let size = u64::try_from(total)
.map(ConfigSize)
.map_err(|_| overflow())?;
Ok((size, !unit.is_empty()))
}
}
fn unit_bytes(unit: &str) -> Option<u128> {
let unit = unit.strip_suffix('B').unwrap_or(unit);
if unit.is_empty() {
return Some(1);
}
UNITS
.iter()
.find(|(name, _)| *name == unit)
.map(|&(_, bytes)| bytes)
}
impl fmt::Display for ConfigSize {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let bytes = u128::from(self.0);
if bytes == 0 {
return f.write_str("0");
}
let mut best: (u128, &str) = (bytes, "");
for &(unit, base) in UNITS.iter().filter(|(name, _)| *name != "K") {
if bytes % base == 0 && bytes / base < best.0 {
best = (bytes / base, unit);
}
}
write!(f, "{}{}", best.0, best.1)
}
}
impl fmt::Debug for ConfigSize {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(self, f)
}
}
impl Serialize for ConfigSize {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
let text = self.to_string();
if text.bytes().all(|b| b.is_ascii_digit()) {
serializer.collect_str(&format_args!("{text}B"))
} else {
serializer.serialize_str(&text)
}
}
}
impl<'de> Deserialize<'de> for ConfigSize {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
struct Visitor;
impl de::Visitor<'_> for Visitor {
type Value = ConfigSize;
fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("a size such as \"500M\", \"512Mi\" or \"4096\"")
}
fn visit_str<E: de::Error>(self, value: &str) -> Result<Self::Value, E> {
value.parse::<ConfigSize>().map_err(de::Error::custom)
}
}
deserializer.deserialize_str(Visitor)
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum LegacySizeUnit {
Bytes,
Megabytes,
Mebibytes,
}
impl LegacySizeUnit {
pub const fn bytes(self) -> u64 {
match self {
LegacySizeUnit::Bytes => 1,
LegacySizeUnit::Megabytes => 1_000_000,
LegacySizeUnit::Mebibytes => 1 << 20,
}
}
}
pub fn size_from_legacy_number<E: de::Error>(
warned: &std::sync::atomic::AtomicBool,
field: &str,
unit: LegacySizeUnit,
value: u64,
) -> Result<ConfigSize, E> {
let size = ConfigSize(value.saturating_mul(unit.bytes()));
if value != u64::MAX && !warned.swap(true, std::sync::atomic::Ordering::Relaxed) {
log::warn!(
"configuration field `{field}` was given the bare number {value}, which is \
deprecated and is removed no earlier than release 0.369.0; write \
`\"{size}\"` instead"
);
}
Ok(size)
}
fn whole_number(value: f64) -> Option<u64> {
const TWO_TO_THE_64: f64 = 18_446_744_073_709_551_616.0;
(value.is_finite() && value >= 0.0 && value.fract() == 0.0 && value < TWO_TO_THE_64)
.then_some(value as u64)
}
pub fn size_or_legacy_number<'de, D>(
deserializer: D,
warned: &std::sync::atomic::AtomicBool,
field: &'static str,
unit: LegacySizeUnit,
) -> Result<Option<ConfigSize>, D::Error>
where
D: Deserializer<'de>,
{
struct Visitor<'a> {
warned: &'a std::sync::atomic::AtomicBool,
field: &'static str,
unit: LegacySizeUnit,
}
impl Visitor<'_> {
fn not_a_number<E: de::Error>(field: &str, value: &str) -> E {
de::Error::custom(format!(
"`{field}` expects a size such as \"512Mi\", or a whole number of the unit \
its earlier spelling took; `{value}` is neither"
))
}
}
impl<'de> de::Visitor<'de> for Visitor<'_> {
type Value = Option<ConfigSize>;
fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("a size such as \"500M\", \"512Mi\" or \"4096\"")
}
fn visit_str<E: de::Error>(self, value: &str) -> Result<Self::Value, E> {
let (size, has_unit) =
ConfigSize::from_str_with_unit(value).map_err(de::Error::custom)?;
if !has_unit && self.unit != LegacySizeUnit::Bytes {
let unit = match self.unit {
LegacySizeUnit::Bytes => "",
LegacySizeUnit::Megabytes => "M",
LegacySizeUnit::Mebibytes => "Mi",
};
return Err(de::Error::custom(format!(
"`{}` expects a size with a unit, such as \"{value}{unit}\"; \"{value}\" \
has none, and a bare number counts {} only as the number {value}, \
without quotes",
self.field,
match self.unit {
LegacySizeUnit::Bytes => "bytes",
LegacySizeUnit::Megabytes => "megabytes",
LegacySizeUnit::Mebibytes => "mebibytes",
}
)));
}
Ok(Some(size))
}
fn visit_u64<E: de::Error>(self, value: u64) -> Result<Self::Value, E> {
size_from_legacy_number(self.warned, self.field, self.unit, value).map(Some)
}
fn visit_i64<E: de::Error>(self, value: i64) -> Result<Self::Value, E> {
let value = u64::try_from(value)
.map_err(|_| de::Error::custom(format!("`{}` cannot be negative", self.field)))?;
self.visit_u64(value)
}
fn visit_f64<E: de::Error>(self, value: f64) -> Result<Self::Value, E> {
match whole_number(value) {
Some(whole) => self.visit_u64(whole),
None => Err(Self::not_a_number(self.field, &value.to_string())),
}
}
fn visit_map<A: de::MapAccess<'de>>(self, mut map: A) -> Result<Self::Value, A::Error> {
const NUMBER: &str = "$serde_json::private::Number";
let Some(key) = map.next_key::<String>()? else {
return Err(Self::not_a_number(self.field, "{}"));
};
if key != NUMBER {
return Err(Self::not_a_number(self.field, "an object"));
}
let text = map.next_value::<String>()?;
match text.parse::<f64>().ok().and_then(whole_number) {
Some(whole) => self.visit_u64(whole),
None if text
.parse::<f64>()
.is_ok_and(|v| v >= 18_446_744_073_709_551_616.0) =>
{
Err(de::Error::custom(format!(
"`{}` is too large: at most {} of its unit",
self.field,
u64::MAX
)))
}
None => Err(Self::not_a_number(self.field, &text)),
}
}
fn visit_unit<E: de::Error>(self) -> Result<Self::Value, E> {
Ok(None)
}
}
deserializer.deserialize_any(Visitor {
warned,
field,
unit,
})
}
pub fn required_size_or_legacy_number<'de, D>(
deserializer: D,
warned: &std::sync::atomic::AtomicBool,
field: &'static str,
unit: LegacySizeUnit,
) -> Result<ConfigSize, D::Error>
where
D: Deserializer<'de>,
{
size_or_legacy_number(deserializer, warned, field, unit)?
.ok_or_else(|| de::Error::custom(format!("`{field}` cannot be null")))
}
#[doc(hidden)]
#[macro_export]
macro_rules! size_setting {
($name:ident, $field:literal, $unit:expr) => {
fn $name<'de, D>(
deserializer: D,
) -> ::std::result::Result<Option<$crate::size::ConfigSize>, D::Error>
where
D: ::serde::Deserializer<'de>,
{
static WARNED: ::std::sync::atomic::AtomicBool =
::std::sync::atomic::AtomicBool::new(false);
$crate::size::size_or_legacy_number(deserializer, &WARNED, $field, $unit)
}
};
}
#[doc(hidden)]
#[macro_export]
macro_rules! required_size_setting {
($name:ident, $field:literal, $unit:expr) => {
fn $name<'de, D>(
deserializer: D,
) -> ::std::result::Result<$crate::size::ConfigSize, D::Error>
where
D: ::serde::Deserializer<'de>,
{
static WARNED: ::std::sync::atomic::AtomicBool =
::std::sync::atomic::AtomicBool::new(false);
$crate::size::required_size_or_legacy_number(deserializer, &WARNED, $field, $unit)
}
};
}
#[cfg(test)]
mod tests {
use super::*;
const KIB: u64 = 1 << 10;
const MIB: u64 = 1 << 20;
const GIB: u64 = 1 << 30;
#[test]
fn parses_kubernetes_quantities() {
for (input, expected) in [
("0", 0),
("4096", 4096),
("4096B", 4096),
("+4096", 4096),
("1k", 1_000),
("1K", 1_000),
("1kB", 1_000),
("1Ki", KIB),
("1KiB", KIB),
("500M", 500_000_000),
("512Mi", 512 * MIB),
("512MiB", 512 * MIB),
("1G", 1_000_000_000),
("1Gi", GIB),
("1T", 1_000_000_000_000),
("1Ti", 1 << 40),
("1P", 1_000_000_000_000_000),
("1Pi", 1 << 50),
("1E", 1_000_000_000_000_000_000),
("1Ei", 1 << 60),
("15Ei", 15 << 60),
("18446744073709551615", u64::MAX),
] {
assert_eq!(
input.parse::<ConfigSize>().map(ConfigSize::as_bytes),
Ok(expected),
"parsing {input}"
);
}
}
#[test]
fn fractions_scale_exactly() {
for (input, expected) in [
("1.5Gi", GIB + GIB / 2),
("0.5Mi", MIB / 2),
("2.5k", 2_500),
("1.Gi", GIB),
(".5Gi", GIB / 2),
("0.5", 1),
("0.4", 0),
("1.0005k", 1_001),
("1.0004k", 1_000),
("0.0000000000000000001Ei", 0),
("1.000000000000000001Ei", (1 << 60) + 1),
] {
assert_eq!(
input.parse::<ConfigSize>().map(ConfigSize::as_bytes),
Ok(expected),
"parsing {input}"
);
}
}
#[test]
fn rejects_bad_input() {
use ParseError::*;
let input = |s: &str| s.to_string();
for (text, expected) in [
("", Empty),
("+", Empty),
(
"-1Gi",
Negative {
input: input("-1Gi"),
},
),
(
"--1",
Negative {
input: input("--1"),
},
),
(
"++1",
InvalidUnit {
unit: input("+1"),
input: input("++1"),
},
),
(
"1m",
InvalidUnit {
unit: input("m"),
input: input("1m"),
},
),
(
"1g",
InvalidUnit {
unit: input("g"),
input: input("1g"),
},
),
(
"1b",
InvalidUnit {
unit: input("b"),
input: input("1b"),
},
),
(
"1MB ",
InvalidUnit {
unit: input("MB "),
input: input("1MB "),
},
),
(
"1 Mi",
InvalidUnit {
unit: input(" Mi"),
input: input("1 Mi"),
},
),
(
"1Gi512Mi",
InvalidUnit {
unit: input("Gi512Mi"),
input: input("1Gi512Mi"),
},
),
(
"1e3",
InvalidUnit {
unit: input("e3"),
input: input("1e3"),
},
),
(
"1..2",
InvalidUnit {
unit: input(".2"),
input: input("1..2"),
},
),
(
"lots",
InvalidUnit {
unit: input("lots"),
input: input("lots"),
},
),
(
"Mi",
MissingNumber {
unit: input("Mi"),
input: input("Mi"),
},
),
(
"B",
MissingNumber {
unit: input("B"),
input: input("B"),
},
),
(".", NoDigits { input: input(".") }),
(
"18446744073709551616",
Overflow {
input: input("18446744073709551616"),
},
),
(
"16Ei",
Overflow {
input: input("16Ei"),
},
),
(
"19E",
Overflow {
input: input("19E"),
},
),
(
"1000000000000000000000000000000000000000000Ei",
Overflow {
input: input("1000000000000000000000000000000000000000000Ei"),
},
),
] {
assert_eq!(
text.parse::<ConfigSize>(),
Err(expected),
"parsing {text:?}"
);
}
}
#[test]
fn rejection_messages_quote_the_input() {
let error = "1g".parse::<ConfigSize>().unwrap_err().to_string();
assert!(error.contains("`g`") && error.contains("`1g`"), "{error}");
assert!(error.contains("'Gi'") && error.contains("'B'"), "{error}");
let error = "Mi".parse::<ConfigSize>().unwrap_err().to_string();
assert!(error.contains("no number in front"), "{error}");
let error = "16Ei".parse::<ConfigSize>().unwrap_err().to_string();
assert!(error.contains("too large"), "{error}");
}
#[test]
fn units_are_listed_in_messages() {
for (unit, _) in UNITS.iter().filter(|(name, _)| *name != "K") {
assert!(EXPECTED_UNITS.contains(&format!("'{unit}'")), "{unit}");
}
}
#[test]
fn displays_in_the_largest_exact_unit() {
for (bytes, expected) in [
(0, "0"),
(1, "1"),
(999, "999"),
(1_000, "1k"),
(1_024, "1Ki"),
(1_500, "1500"),
(1_000_000, "1M"),
(MIB, "1Mi"),
(MIB + MIB / 2, "1536Ki"),
(500_000_000, "500M"),
(GIB, "1Gi"),
(1_024_000_000, "1024M"),
(1 << 60, "1Ei"),
(1_000_000_000_000_000_000, "1E"),
(u64::MAX, "18446744073709551615"),
] {
assert_eq!(
ConfigSize::from_bytes(bytes).to_string(),
expected,
"{bytes} bytes"
);
}
}
#[test]
fn round_trips_through_json() {
for (text, written) in [
("0", "0B"),
("0B", "0B"),
("4095", "4095B"),
("1.1Gi", "1181116006B"),
("1k", "1k"),
("512Mi", "512Mi"),
("1Gi", "1Gi"),
("1536Ki", "1536Ki"),
("1024M", "1024M"),
("1Ei", "1Ei"),
] {
let size: ConfigSize = serde_json::from_str(&format!("\"{text}\"")).unwrap();
let json = serde_json::to_string(&size).unwrap();
assert_eq!(json, format!("\"{written}\""), "{text}");
assert_eq!(serde_json::from_str::<ConfigSize>(&json).unwrap(), size);
}
let error = serde_json::from_str::<ConfigSize>("4096")
.unwrap_err()
.to_string();
assert!(error.contains("a size such as"), "{error}");
}
#[test]
fn constructors_saturate() {
assert_eq!(ConfigSize::from_mib(u64::MAX), ConfigSize::MAX);
assert_eq!(ConfigSize::from_mb(u64::MAX), ConfigSize::MAX);
assert_eq!(ConfigSize::from_gib(3).as_bytes(), 3 * GIB);
assert_eq!(ConfigSize::from_mb(16_000).as_bytes(), 16_000_000_000);
assert_eq!(ConfigSize::from_mib(256).as_bytes(), 256 << 20);
assert_eq!(ConfigSize::MAX.as_usize(), usize::MAX);
}
#[test]
fn a_superseded_spelling_reads_any_whole_number() {
#[derive(Debug, serde::Deserialize)]
struct Config {
#[serde(default, alias = "cap_mb", deserialize_with = "size_cap")]
cap: Option<ConfigSize>,
}
crate::size_setting!(size_cap, "cap", LegacySizeUnit::Megabytes);
for json in [
r#"{"cap_mb": 10}"#,
r#"{"cap_mb": 10.0}"#,
r#"{"cap_mb": 1e1}"#,
r#"{"cap": "10M"}"#,
r#"{"cap": "10000k"}"#,
r#"{"cap": 10}"#,
] {
let config: Config = serde_json::from_str(json).unwrap();
assert_eq!(config.cap, Some(ConfigSize::from_mb(10)), "parsing {json}");
}
assert_eq!(serde_json::from_str::<Config>("{}").unwrap().cap, None);
assert_eq!(
serde_json::from_str::<Config>(r#"{"cap": null}"#)
.unwrap()
.cap,
None
);
let config: Config = serde_json::from_str(r#"{"cap_mb": 18446744073709551615}"#).unwrap();
assert_eq!(config.cap, Some(ConfigSize::MAX));
for (bad, quoted) in [
(r#"{"cap_mb": 10.5}"#, "`10.5`"),
(r#"{"cap_mb": -5}"#, "cannot be negative"),
(r#"{"cap_mb": {}}"#, "`{}`"),
(r#"{"cap_mb": 10.50}"#, "`10.50`"),
(r#"{"cap_mb": {"bytes": 1}}"#, "`an object`"),
(r#"{"cap": "10m"}"#, "unknown unit `m`"),
] {
let error = serde_json::from_str::<Config>(bad).unwrap_err().to_string();
assert!(error.contains(quoted), "parsing {bad} reported {error}");
}
}
#[test]
fn the_parser_says_whether_a_unit_was_written() {
for (text, has_unit) in [
("16G", true),
("4096B", true),
("1.5Gi", true),
("16000", false),
("+16000", false),
("1.5", false),
(".5", false),
("0", false),
] {
let (size, written) = ConfigSize::from_str_with_unit(text).unwrap();
assert_eq!(written, has_unit, "{text}");
assert_eq!(size, text.parse::<ConfigSize>().unwrap(), "{text}");
}
assert!(ConfigSize::from_str_with_unit("1.2.3").is_err());
}
#[test]
fn a_unitless_string_needs_a_unit_unless_the_setting_counts_bytes() {
#[derive(Debug, serde::Deserialize)]
struct Config {
#[serde(default, alias = "cap_mb", deserialize_with = "size_cap")]
cap: Option<ConfigSize>,
#[serde(default, alias = "buffer_bytes", deserialize_with = "size_buffer")]
buffer: Option<ConfigSize>,
}
crate::size_setting!(size_cap, "cap", LegacySizeUnit::Megabytes);
crate::size_setting!(size_buffer, "buffer", LegacySizeUnit::Bytes);
for json in [
r#"{"cap": "16000"}"#,
r#"{"cap_mb": "16000"}"#,
r#"{"cap": "+1.5"}"#,
] {
let error = serde_json::from_str::<Config>(json)
.unwrap_err()
.to_string();
assert!(
error.contains("expects a size with a unit"),
"{json}: {error}"
);
assert!(error.contains("`cap`"), "{json}: {error}");
}
let error = serde_json::from_str::<Config>(r#"{"cap": "16000"}"#)
.unwrap_err()
.to_string();
assert!(error.contains("\"16000M\""), "{error}");
for (json, expected) in [
(r#"{"cap": "16000M"}"#, ConfigSize::from_mb(16_000)),
(r#"{"cap": "16000B"}"#, ConfigSize::from_bytes(16_000)),
(r#"{"cap": 16000}"#, ConfigSize::from_mb(16_000)),
(r#"{"buffer": "4096"}"#, ConfigSize::from_bytes(4096)),
(r#"{"buffer": 4096}"#, ConfigSize::from_bytes(4096)),
(r#"{"buffer_bytes": 4096}"#, ConfigSize::from_bytes(4096)),
] {
let config: Config = serde_json::from_str(json).unwrap();
assert_eq!(
config.cap.or(config.buffer),
Some(expected),
"parsing {json}"
);
}
}
#[test]
fn writing_both_spellings_is_rejected() {
#[derive(Debug, serde::Deserialize)]
struct Config {
#[serde(default, alias = "cap_mb", deserialize_with = "size_cap")]
cap: Option<ConfigSize>,
}
crate::size_setting!(size_cap, "cap", LegacySizeUnit::Megabytes);
let one: Config = serde_json::from_str(r#"{"cap": "30M"}"#).unwrap();
assert_eq!(one.cap, Some(ConfigSize::from_mb(30)));
let error = serde_json::from_str::<Config>(r#"{"cap": "30M", "cap_mb": 30}"#)
.unwrap_err()
.to_string();
assert!(error.contains("duplicate field `cap`"), "{error}");
}
#[test]
fn a_required_setting_refuses_null() {
#[derive(Debug, serde::Deserialize)]
struct Config {
#[serde(
default = "default_buffer",
alias = "buffer_bytes",
deserialize_with = "size_buffer"
)]
buffer: ConfigSize,
}
fn default_buffer() -> ConfigSize {
ConfigSize::from_mib(1)
}
crate::required_size_setting!(size_buffer, "buffer", LegacySizeUnit::Bytes);
assert_eq!(
serde_json::from_str::<Config>("{}").unwrap().buffer,
ConfigSize::from_mib(1)
);
assert_eq!(
serde_json::from_str::<Config>(r#"{"buffer_bytes": 2048}"#)
.unwrap()
.buffer,
ConfigSize::from_kib(2)
);
assert_eq!(
serde_json::from_str::<Config>(r#"{"buffer": "2Ki"}"#)
.unwrap()
.buffer,
ConfigSize::from_kib(2)
);
let error = serde_json::from_str::<Config>(r#"{"buffer": null}"#)
.unwrap_err()
.to_string();
assert!(error.contains("`buffer` cannot be null"), "{error}");
}
#[test]
fn the_no_limit_value_is_read_without_a_warning() {
let warned = std::sync::atomic::AtomicBool::new(false);
let size: ConfigSize = size_from_legacy_number::<serde_json::Error>(
&warned,
"cap",
LegacySizeUnit::Bytes,
u64::MAX,
)
.unwrap();
assert_eq!(size, ConfigSize::MAX);
assert!(!warned.load(std::sync::atomic::Ordering::Relaxed));
let _ = size_from_legacy_number::<serde_json::Error>(
&warned,
"cap",
LegacySizeUnit::Mebibytes,
3,
);
assert!(warned.load(std::sync::atomic::Ordering::Relaxed));
}
mod properties {
use super::*;
use proptest::prelude::*;
fn bytes() -> impl Strategy<Value = u64> {
prop_oneof![
1 => Just(0u64),
1 => Just(1u64),
1 => Just(u64::MAX),
1 => Just(1u64 << 60),
1 => Just(1_000_000_000_000_000_000u64),
2 => 0u64..1_000,
2 => (0u64..1_000).prop_map(|k| k << 20),
2 => (0u64..1_000).prop_map(|m| m * 1_000_000),
6 => any::<u64>(),
]
}
fn size_like_text() -> impl Strategy<Value = String> {
prop_oneof![
2 => any::<String>(),
3 => "[-+0-9. kKMGTPEiB]{0,40}",
3 => "[0-9]{1,40}(k|K|M|G|T|P|E|Ki|Mi|Gi|Ti|Pi|Ei)B?",
2 => "[0-9]{1,25}\\.[0-9]{1,25}(k|M|Gi|Ei)",
]
}
proptest! {
#![proptest_config(ProptestConfig::with_cases(256))]
#[test]
fn display_round_trips(bytes in bytes()) {
let size = ConfigSize::from_bytes(bytes);
let text = size.to_string();
prop_assert_eq!(
text.parse::<ConfigSize>().map_err(|e| e.to_string()),
Ok(size),
"formatted as {:?}",
text
);
}
#[test]
fn serde_agrees_with_from_str(bytes in bytes()) {
let size = ConfigSize::from_bytes(bytes);
let json = serde_json::to_string(&size).unwrap();
let text = size.to_string();
let written = if text.bytes().all(|b| b.is_ascii_digit()) {
format!("\"{text}B\"")
} else {
format!("\"{text}\"")
};
prop_assert_eq!(&json, &written);
prop_assert_eq!(serde_json::from_str::<ConfigSize>(&json).unwrap(), size);
prop_assert_eq!(json.trim_matches('"').parse::<ConfigSize>().unwrap(), size);
}
#[test]
fn parsing_never_panics(text in size_like_text()) {
let _ = text.parse::<ConfigSize>();
}
#[test]
fn whole_units_multiply(count in 0u64..1_000_000, unit in 0usize..UNITS.len()) {
let (name, bytes) = UNITS[unit];
let expected = u128::from(count) * bytes;
let parsed = format!("{count}{name}").parse::<ConfigSize>();
if let Ok(expected) = u64::try_from(expected) {
prop_assert_eq!(parsed.map(ConfigSize::as_bytes), Ok(expected));
} else {
let overflowed = matches!(parsed, Err(ParseError::Overflow { .. }));
prop_assert!(overflowed, "parsed {:?}", parsed);
}
}
}
}
}