pub const ID_MAX_LEN: usize = 1024;
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub enum IdError {
#[error("ID must not be empty")]
Empty,
#[error("ID is too long: {len} bytes (max {max})")]
TooLong {
len: usize,
max: usize,
},
#[error("ID must not contain NUL bytes")]
ContainsNul,
#[error("id length {requested} out of range [{min}, {max}]")]
LengthOutOfRange {
requested: usize,
min: usize,
max: usize,
},
}
pub(super) fn validate(id: &str) -> Result<(), IdError> {
if id.is_empty() {
return Err(IdError::Empty);
}
if id.len() > ID_MAX_LEN {
return Err(IdError::TooLong {
len: id.len(),
max: ID_MAX_LEN,
});
}
if id.contains('\0') {
return Err(IdError::ContainsNul);
}
Ok(())
}