|
| 1 | +//! CTV3. |
| 2 | +//! |
| 3 | +//! Rules (preserved from the legacy `ctv3_validator`): |
| 4 | +//! 1. Exactly 5 characters. |
| 5 | +//! 2. Allowed characters: `a-z`, `A-Z`, `0-9`, and `.`. |
| 6 | +//! 3. N alphanumeric characters followed by `5 - N` trailing dots (N = 0..=5). |
| 7 | +//! |
| 8 | +//! CTV3 is case-sensitive, so `normalize` trims whitespace only — |
| 9 | +//! it does not fold case the way ICD10 and OPCS do. |
| 10 | +
|
| 11 | +use std::sync::LazyLock; |
| 12 | + |
| 13 | +use codelist_rs::types::{Code, CodeSystemId, NormalizedCode}; |
| 14 | +use regex::Regex; |
| 15 | + |
| 16 | +use crate::{ |
| 17 | + core::CodingSystem, |
| 18 | + errors::{SystemError, ValidationError}, |
| 19 | +}; |
| 20 | + |
| 21 | +/// CTV3 coding system marker. |
| 22 | +#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 23 | +pub struct Ctv3; |
| 24 | + |
| 25 | +static CTV3_REGEX: LazyLock<Regex> = LazyLock::new(|| { |
| 26 | + Regex::new( |
| 27 | + r"^(?:[a-zA-Z0-9]{5}|[a-zA-Z0-9]{4}\.|[a-zA-Z0-9]{3}\.{2}|[a-zA-Z0-9]{2}\.{3}|[a-zA-Z0-9]\.{4}|\.{5})$", |
| 28 | + ) |
| 29 | + .expect("CTV3 regex compiles") |
| 30 | +}); |
| 31 | + |
| 32 | +impl CodingSystem for Ctv3 { |
| 33 | + const ID: CodeSystemId = CodeSystemId("CTV3"); |
| 34 | + |
| 35 | + fn normalize(code: &Code) -> Result<NormalizedCode, SystemError> { |
| 36 | + // CTV3 is case-sensitive — no case folding, trim only. |
| 37 | + let s = code.as_str().trim().to_string(); |
| 38 | + if s.is_empty() { |
| 39 | + return Err(SystemError::normalisation("CTV3", "empty after trim")); |
| 40 | + } |
| 41 | + Ok(NormalizedCode::from(s)) |
| 42 | + } |
| 43 | + |
| 44 | + fn validate_syntax(code: &NormalizedCode) -> Result<(), ValidationError> { |
| 45 | + let len = code.as_str().len(); |
| 46 | + if len != 5 { |
| 47 | + return Err(ValidationError::invalid_length( |
| 48 | + code.as_str().to_string(), |
| 49 | + "CTV3".to_string(), |
| 50 | + format!("length must be exactly 5 (got {len})"), |
| 51 | + )); |
| 52 | + } |
| 53 | + if !CTV3_REGEX.is_match(code.as_str()) { |
| 54 | + return Err(ValidationError::invalid_contents( |
| 55 | + code.as_str().to_string(), |
| 56 | + "CTV3".to_string(), |
| 57 | + "does not match expected format".to_string(), |
| 58 | + )); |
| 59 | + } |
| 60 | + Ok(()) |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +#[cfg(test)] |
| 65 | +mod tests { |
| 66 | + use codelist_rs::types::Code; |
| 67 | + use proptest::prelude::*; |
| 68 | + |
| 69 | + use super::*; |
| 70 | + |
| 71 | + #[test] |
| 72 | + fn valid_ctv3_codes_pass_syntax() { |
| 73 | + for ok in ["Af918", "ABb..", "alkif", "F....", "bn89.", "Me...", "99999", "....."] { |
| 74 | + let c = Code::from(ok); |
| 75 | + let n = Ctv3::normalize(&c).unwrap(); |
| 76 | + Ctv3::validate_syntax(&n).unwrap_or_else(|_| panic!("{ok} should pass")); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + #[test] |
| 81 | + fn too_short_ctv3_codes_fail_with_invalid_length() { |
| 82 | + for bad in ["Af.", "A00A", "10"] { |
| 83 | + let c = Code::from(bad); |
| 84 | + let n = Ctv3::normalize(&c).unwrap(); |
| 85 | + let err = Ctv3::validate_syntax(&n).unwrap_err(); |
| 86 | + assert!( |
| 87 | + matches!(err, crate::errors::ValidationError::InvalidLength { .. }), |
| 88 | + "{bad} should fail with InvalidLength" |
| 89 | + ); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + #[test] |
| 94 | + fn too_long_ctv3_codes_fail_with_invalid_length() { |
| 95 | + for bad in ["A009000000", "9874ji", "Q90....."] { |
| 96 | + let c = Code::from(bad); |
| 97 | + let n = Ctv3::normalize(&c).unwrap(); |
| 98 | + let err = Ctv3::validate_syntax(&n).unwrap_err(); |
| 99 | + assert!( |
| 100 | + matches!(err, crate::errors::ValidationError::InvalidLength { .. }), |
| 101 | + "{bad} should fail with InvalidLength" |
| 102 | + ); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + #[test] |
| 107 | + fn bad_content_ctv3_codes_fail_with_invalid_contents() { |
| 108 | + for bad in [".a009", "10a.f", "Af!!!", "A..9k", "..9jJ", "A00.l"] { |
| 109 | + let c = Code::from(bad); |
| 110 | + let n = Ctv3::normalize(&c).unwrap(); |
| 111 | + let err = Ctv3::validate_syntax(&n).unwrap_err(); |
| 112 | + assert!( |
| 113 | + matches!(err, crate::errors::ValidationError::InvalidContents { .. }), |
| 114 | + "{bad} should fail with InvalidContents" |
| 115 | + ); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + #[test] |
| 120 | + fn ctv3_normalize_preserves_case() { |
| 121 | + // CTV3 is case-sensitive: normalize must not fold to uppercase. |
| 122 | + // "Af918" must remain "Af918", not become "AF918" as ICD10/OPCS would. |
| 123 | + let c = Code::from("Af918"); |
| 124 | + let n = Ctv3::normalize(&c).unwrap(); |
| 125 | + assert_eq!(n.as_str(), "Af918"); |
| 126 | + } |
| 127 | + |
| 128 | + #[test] |
| 129 | + fn ctv3_normalize_trims_whitespace() { |
| 130 | + let c = Code::from(" Af918 "); |
| 131 | + let n = Ctv3::normalize(&c).unwrap(); |
| 132 | + assert_eq!(n.as_str(), "Af918"); |
| 133 | + Ctv3::validate_syntax(&n).unwrap(); |
| 134 | + } |
| 135 | + |
| 136 | + #[test] |
| 137 | + fn ctv3_normalize_rejects_empty_after_trim() { |
| 138 | + let c = Code::from(" "); |
| 139 | + assert!(Ctv3::normalize(&c).is_err()); |
| 140 | + } |
| 141 | + |
| 142 | + fn valid_ctv3() -> impl Strategy<Value = String> { |
| 143 | + (0u32..=5).prop_flat_map(|n| { |
| 144 | + proptest::string::string_regex(&format!("[a-zA-Z0-9]{{{n}}}")) |
| 145 | + .unwrap() |
| 146 | + .prop_map(move |s| format!("{s}{}", ".".repeat((5 - n) as usize))) |
| 147 | + }) |
| 148 | + } |
| 149 | + |
| 150 | + proptest! { |
| 151 | + #[test] |
| 152 | + fn valid_shape_ctv3_validates_ok(s in valid_ctv3()) { |
| 153 | + let c = Code::from(s.as_str()); |
| 154 | + let n = Ctv3::normalize(&c).unwrap(); |
| 155 | + prop_assert!(Ctv3::validate_syntax(&n).is_ok()); |
| 156 | + } |
| 157 | + |
| 158 | + #[test] |
| 159 | + fn ctv3_disallowed_chars_fail_invalid_contents( |
| 160 | + illegal in r"[!@#$%]", |
| 161 | + suffix in r"[a-zA-Z0-9.]{4}", |
| 162 | + ) { |
| 163 | + let s = format!("{illegal}{suffix}"); |
| 164 | + let c = Code::from(s.as_str()); |
| 165 | + let n = Ctv3::normalize(&c).unwrap(); |
| 166 | + let err = Ctv3::validate_syntax(&n).unwrap_err(); |
| 167 | + let is_invalid_contents = matches!(err, ValidationError::InvalidContents { .. }); |
| 168 | + prop_assert!(is_invalid_contents); |
| 169 | + } |
| 170 | + |
| 171 | + #[test] |
| 172 | + fn ctv3_out_of_range_length_fails_invalid_length( |
| 173 | + s in prop_oneof![r"[a-zA-Z0-9.]{1,4}", r"[a-zA-Z0-9.]{6,12}"], |
| 174 | + ) { |
| 175 | + let c = Code::from(s.as_str()); |
| 176 | + let n = Ctv3::normalize(&c).unwrap(); |
| 177 | + let err = Ctv3::validate_syntax(&n).unwrap_err(); |
| 178 | + let is_invalid_length = matches!(err, ValidationError::InvalidLength { .. }); |
| 179 | + prop_assert!(is_invalid_length); |
| 180 | + } |
| 181 | + |
| 182 | + #[test] |
| 183 | + fn ctv3_trim_idempotent( |
| 184 | + s in valid_ctv3(), |
| 185 | + left in 0usize..5, |
| 186 | + right in 0usize..5, |
| 187 | + ) { |
| 188 | + let padded = format!("{}{s}{}", " ".repeat(left), " ".repeat(right)); |
| 189 | + let base = Ctv3::normalize(&Code::from(s.as_str())).unwrap(); |
| 190 | + let pad = Ctv3::normalize(&Code::from(padded.as_str())).unwrap(); |
| 191 | + prop_assert_eq!(base.as_str(), pad.as_str()); |
| 192 | + } |
| 193 | + } |
| 194 | +} |
0 commit comments