Initial commit
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
use super::errors::ValidationError;
|
||||
|
||||
pub(crate) fn validate_range<T, R>(
|
||||
value: T,
|
||||
range: R,
|
||||
message: impl Fn() -> String,
|
||||
) -> Result<(), ValidationError>
|
||||
where
|
||||
T: PartialOrd,
|
||||
R: std::ops::RangeBounds<T>,
|
||||
{
|
||||
if !range.contains(&value) {
|
||||
return Err(ValidationError::value(message()));
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn validate_ascii_padded(
|
||||
value: &[u8],
|
||||
length: usize,
|
||||
padding: u8,
|
||||
field: &str,
|
||||
) -> Result<(), ValidationError> {
|
||||
if value.len() != length {
|
||||
return Err(ValidationError::value(format!(
|
||||
"{} must be exactly {} bytes",
|
||||
field, length
|
||||
)));
|
||||
}
|
||||
|
||||
for &byte in value {
|
||||
if !((0x20..=0x7E).contains(&byte) || byte == padding) {
|
||||
return Err(ValidationError::value(format!(
|
||||
"Invalid ASCII character: {:#04x}",
|
||||
byte
|
||||
)));
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn validate_bitmask(
|
||||
value: u8,
|
||||
allowed_bits: u8,
|
||||
message: impl Fn() -> String,
|
||||
) -> Result<(), ValidationError> {
|
||||
if value & !allowed_bits != 0 {
|
||||
return Err(ValidationError::value(message()));
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user