dicom_anonymization/
actions.rsuse crate::config::ConfigError;
use dicom_core::Tag;
use thiserror::Error;
const HASH_LENGTH_MINIMUM: usize = 8;
#[derive(Error, Debug, Clone, Eq, PartialEq, Ord, PartialOrd)]
#[error("{0}")]
pub struct HashLengthError(String);
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct HashLength(pub(crate) usize);
impl HashLength {
pub fn new(length: usize) -> Result<Self, HashLengthError> {
if length < HASH_LENGTH_MINIMUM {
return Err(HashLengthError(format!(
"hash length must be at least {}",
HASH_LENGTH_MINIMUM
)));
}
Ok(HashLength(length))
}
}
impl From<HashLengthError> for ConfigError {
fn from(err: HashLengthError) -> Self {
ConfigError::InvalidHashLength(err.0)
}
}
impl TryFrom<usize> for HashLength {
type Error = HashLengthError;
fn try_from(value: usize) -> Result<Self, HashLengthError> {
let hash_length = HashLength::new(value)?;
Ok(hash_length)
}
}
#[derive(Debug, Clone, PartialEq)]
pub enum Action {
Empty,
Remove,
Replace(String),
Hash(Option<HashLength>),
HashDate(Tag),
HashUID,
Keep,
None,
}
#[cfg(test)]
mod tests {
use crate::actions::HashLength;
#[test]
fn test_hash_length() {
assert_eq!(HashLength::new(9).unwrap().0, 9);
}
#[test]
fn test_hash_length_new() {
assert!(HashLength::new(9).is_ok());
assert!(HashLength::new(8).is_ok());
assert!(HashLength::new(7).is_err());
}
#[test]
fn test_hash_length_try_into() {
assert!(<usize as TryInto<HashLength>>::try_into(9).is_ok());
assert!(<usize as TryInto<HashLength>>::try_into(8).is_ok());
assert!(<usize as TryInto<HashLength>>::try_into(7).is_err());
}
#[test]
fn test_hash_length_error() {
let result = HashLength::new(7);
assert!(result.is_err());
let error = result.unwrap_err();
assert_eq!(error.to_string(), "hash length must be at least 8");
}
}