use crate::error::{Error, Result};
use crate::limits::{MAX_CACHE_KEY_LENGTH, MAX_TIMESTAMP, MIN_TIMESTAMP};
pub(crate) fn validate_timestamp_bounds(value: i64) -> Result<()> {
if !(MIN_TIMESTAMP..=MAX_TIMESTAMP).contains(&value) {
return Err(Error::TimestampOutOfBounds {
value,
min: MIN_TIMESTAMP,
max: MAX_TIMESTAMP,
});
}
Ok(())
}
pub(crate) fn apply_clock_skew(timestamp: i64, skew_seconds: u64, add: bool) -> Result<i64> {
let skew_i64 = skew_seconds as i64;
if add {
timestamp.checked_add(skew_i64)
} else {
timestamp.checked_sub(skew_i64)
}
.ok_or(Error::TimestampOverflow)
}
pub(crate) fn validate_field_size(field: &str, value: &str, max: usize) -> Result<()> {
if value.len() > max {
return Err(Error::HeaderFieldTooLong {
field: field.into(),
length: value.len(),
max,
});
}
Ok(())
}
pub(crate) fn is_valid_cache_key(key: &str) -> bool {
key.len() <= MAX_CACHE_KEY_LENGTH
}