1use super::evaluator::ConditionResult;
8
9pub use super::timezone::{dst_transitions_between, is_mesz_utc, is_mez_utc, parse_dtm303};
11pub use super::werktag::{
12 compute_easter, is_german_holiday, is_werktag, parse_ccyymmdd_prefix, werktage_between,
13};
14
15pub fn validate_max_decimal_places(value: &str, max: usize) -> ConditionResult {
26 if value.is_empty() {
27 return ConditionResult::Unknown;
28 }
29 let decimal_places = match value.find('.') {
30 Some(pos) => value.len() - pos - 1,
31 None => 0,
32 };
33 ConditionResult::from(decimal_places <= max)
34}
35
36pub fn validate_max_integer_digits(value: &str, max: usize) -> ConditionResult {
40 if value.is_empty() {
41 return ConditionResult::Unknown;
42 }
43 let s = value.strip_prefix('-').unwrap_or(value);
44 let integer_part = match s.find('.') {
45 Some(pos) => &s[..pos],
46 None => s,
47 };
48 ConditionResult::from(integer_part.len() <= max)
49}
50
51pub fn validate_numeric(value: &str, op: &str, threshold: f64) -> ConditionResult {
63 if value.is_empty() {
64 return ConditionResult::Unknown;
65 }
66 let parsed = match edifact_number(value) {
67 Some(v) => v,
68 None => return ConditionResult::False,
69 };
70 let result = match op {
71 "==" => (parsed - threshold).abs() < f64::EPSILON,
72 "!=" => (parsed - threshold).abs() >= f64::EPSILON,
73 ">" => parsed > threshold,
74 ">=" => parsed >= threshold,
75 "<" => parsed < threshold,
76 "<=" => parsed <= threshold,
77 _ => return ConditionResult::Unknown,
78 };
79 ConditionResult::from(result)
80}
81
82pub fn is_edifact_number(value: &str) -> bool {
86 edifact_number(value).is_some()
87}
88
89fn edifact_number(value: &str) -> Option<f64> {
92 let unsigned = value.strip_prefix('-').unwrap_or(value);
93 let mut marks = 0;
94 let mut digits = 0;
95 for c in unsigned.chars() {
96 match c {
97 '0'..='9' => digits += 1,
98 '.' | ',' => marks += 1,
99 _ => return None,
100 }
101 }
102 if digits == 0 || marks > 1 {
103 return None;
104 }
105 value.replace(',', ".").parse().ok()
106}
107
108pub fn fits_date_format(value: &str, format: &str) -> Option<bool> {
116 let digits = |s: &str| !s.is_empty() && s.bytes().all(|b| b.is_ascii_digit());
117 let num = |s: &str| s.parse::<u32>().unwrap_or(u32::MAX);
118 let month = |s: &str| (1..=12).contains(&num(s));
119 let day = |s: &str| (1..=31).contains(&num(s));
120 let week = |s: &str| (1..=53).contains(&num(s));
121 let time = |s: &str| num(&s[..2]) <= 23 && num(&s[2..]) <= 59;
122 let date = |s: &str| digits(s) && s.len() == 8 && month(&s[4..6]) && day(&s[6..8]);
123 let zone =
124 |s: &str| s.is_empty() || (s.len() == 3 && matches!(&s[..1], "+" | "-") && digits(&s[1..]));
125 if !value.is_ascii() {
127 return fits_date_format("", format);
128 }
129 let fits = match format {
130 "102" => date(value),
131 "104" => {
132 digits(value)
133 && value.len() == 8
134 && month(&value[..2])
135 && week(&value[2..4])
136 && month(&value[4..6])
137 && week(&value[6..])
138 }
139 "106" => digits(value) && value.len() == 4 && month(&value[..2]) && day(&value[2..]),
140 "203" => value.len() == 12 && date(&value[..8]) && digits(&value[8..]) && time(&value[8..]),
141 "303" => {
142 value.len() >= 12
143 && date(&value[..8])
144 && digits(&value[8..12])
145 && time(&value[8..12])
146 && zone(&value[12..])
147 }
148 "304" => {
149 value.len() >= 14
150 && date(&value[..8])
151 && digits(&value[8..14])
152 && time(&value[8..12])
153 && num(&value[12..14]) <= 59
154 && zone(&value[14..])
155 }
156 "401" => digits(value) && value.len() == 4 && time(value),
157 "501" => digits(value) && value.len() == 8 && time(&value[..4]) && time(&value[4..]),
158 "602" => digits(value) && value.len() == 4,
159 "610" => digits(value) && value.len() == 6 && month(&value[4..]),
160 "802" | "803" | "804" => digits(value),
161 "Z01" => {
165 let n = value.len().saturating_sub(2);
166 (1..=2).contains(&n)
167 && digits(&value[..n])
168 && matches!(&value[n..n + 1], "T" | "W" | "M")
169 && matches!(&value[n + 1..], "M" | "Q" | "H" | "J" | "T" | "R")
170 }
171 _ => return None,
172 };
173 Some(fits)
174}
175
176pub fn validate_hhmm_equals(dtm_value: &str, expected_hhmm: &str) -> ConditionResult {
185 if dtm_value.len() < 12 {
186 return ConditionResult::False;
188 }
189 ConditionResult::from(&dtm_value[8..12] == expected_hhmm)
190}
191
192pub fn validate_hhmm_range(dtm_value: &str, min: &str, max: &str) -> ConditionResult {
196 if dtm_value.len() < 12 {
197 return ConditionResult::False;
198 }
199 let hhmm = &dtm_value[8..12];
200 ConditionResult::from(hhmm >= min && hhmm <= max)
201}
202
203pub fn validate_mmddhhmm_equals(dtm_value: &str, expected: &str) -> ConditionResult {
209 if dtm_value.len() < 12 {
210 return ConditionResult::False;
211 }
212 ConditionResult::from(&dtm_value[4..12] == expected)
213}
214
215pub fn validate_timezone_utc(dtm_value: &str) -> ConditionResult {
226 if dtm_value.len() < 15 || !dtm_value.is_char_boundary(dtm_value.len() - 3) {
227 return ConditionResult::False;
229 }
230 ConditionResult::from(&dtm_value[dtm_value.len() - 3..] == "+00")
231}
232
233pub fn validate_email(value: &str) -> ConditionResult {
239 if value.is_empty() {
240 return ConditionResult::Unknown;
241 }
242 ConditionResult::from(value.contains('@') && value.contains('.'))
243}
244
245pub fn validate_phone(value: &str) -> ConditionResult {
250 if value.is_empty() {
251 return ConditionResult::Unknown;
252 }
253 if !value.starts_with('+') || value.len() < 2 {
254 return ConditionResult::from(false);
255 }
256 ConditionResult::from(value[1..].chars().all(|c| c.is_ascii_digit()))
257}
258
259pub fn validate_malo_id(value: &str) -> ConditionResult {
273 if value.len() != 11 {
274 return ConditionResult::from(false);
275 }
276 if !value.chars().all(|c| c.is_ascii_digit()) {
277 return ConditionResult::from(false);
278 }
279 let digits: Vec<u32> = value.chars().filter_map(|c| c.to_digit(10)).collect();
280 let check = digits[10];
281 let mut sum = 0u32;
282 for (i, &d) in digits[..10].iter().enumerate() {
283 if (i + 1) % 2 == 0 {
285 sum += 2 * d;
286 } else {
287 sum += d;
288 }
289 }
290 let expected = (10 - (sum % 10)) % 10;
291 ConditionResult::from(check == expected)
292}
293
294pub fn validate_tr_id(value: &str) -> ConditionResult {
296 if value.is_empty() {
297 return ConditionResult::Unknown;
298 }
299 ConditionResult::from(value.len() <= 35 && value.chars().all(|c| c.is_ascii_alphanumeric()))
300}
301
302fn validate_prefixed_location_id(value: &str, prefix: char) -> ConditionResult {
305 let mut chars = value.chars();
306 ConditionResult::from(
307 value.len() == 11
308 && chars.next() == Some(prefix)
309 && chars.all(|c| c.is_ascii_digit() || c.is_ascii_uppercase()),
310 )
311}
312
313pub fn validate_sr_id(value: &str) -> ConditionResult {
316 validate_prefixed_location_id(value, 'C')
317}
318
319pub fn validate_nelo_id(value: &str) -> ConditionResult {
321 validate_prefixed_location_id(value, 'E')
322}
323
324pub fn parse_obis(value: &str) -> Option<(u32, u32)> {
343 let value = value.trim();
344 let (_ab, cde) = value.split_once(':')?;
346 let cde = cde.split('*').next()?;
348 let mut parts = cde.split('.');
349 let c = parts.next()?.parse::<u32>().ok()?;
350 let d = parts.next()?.parse::<u32>().ok()?;
351 Some((c, d))
352}
353
354pub fn obis_tariff(value: &str) -> Option<u32> {
358 let (_ab, cde) = value.trim().split_once(':')?;
359 let cde = cde.split('*').next()?;
360 let mut parts = cde.split('.');
361 parts.next()?.parse::<u32>().ok()?;
362 parts.next()?.parse::<u32>().ok()?;
363 parts.next()?.parse::<u32>().ok()
364}
365
366pub fn is_obis_wirkarbeit_kumuliert(value: &str) -> bool {
372 matches!(parse_obis(value), Some((1 | 2, 8 | 9)))
373}
374
375pub fn is_obis_wirkarbeit_quarter_hour(value: &str) -> bool {
382 matches!(parse_obis(value), Some((1 | 2, 29)))
383}
384
385pub fn is_obis_energiemenge(value: &str) -> bool {
389 value.trim().starts_with("1-") && matches!(parse_obis(value), Some((_, 9 | 29)))
390}
391
392pub fn is_obis_zaehlerstand(value: &str) -> bool {
394 value.trim().starts_with("1-") && matches!(parse_obis(value), Some((_, 8)))
395}
396
397pub fn is_obis_wirkleistung_maximum(value: &str) -> bool {
401 value.trim().starts_with("1-") && matches!(parse_obis(value), Some((1 | 2, 6)))
402}
403
404pub fn is_obis_blindarbeit(value: &str) -> bool {
407 value.trim().starts_with("1-") && matches!(parse_obis(value), Some((3..=8, 8 | 9 | 29)))
408}
409
410pub fn obis_has_zaehlzeit(value: &str) -> bool {
413 obis_tariff(value).is_some_and(|e| (1..=62).contains(&e))
414}
415
416pub fn is_energy_register_obis(value: &str) -> bool {
422 value.trim().starts_with("1-") && matches!(parse_obis(value), Some((1..=8, 8)))
423}
424
425pub fn validate_x509_cert_body(value: &str) -> ConditionResult {
444 let trimmed = value.trim();
445 if trimmed.is_empty() {
446 return ConditionResult::Unknown;
447 }
448 let body = trimmed
451 .trim_start_matches("-----BEGIN CERTIFICATE-----")
452 .trim_end_matches("-----END CERTIFICATE-----")
453 .trim();
454 let chars_ok = body.chars().all(|c| {
455 c.is_ascii_alphanumeric() || matches!(c, '+' | '/' | '=' | '\n' | '\r' | '\t' | ' ')
456 });
457 let len_ok = body.chars().filter(|c| !c.is_whitespace()).count() >= 100;
460 ConditionResult::from(chars_ok && len_ok)
461}
462
463pub fn validate_zahlpunkt(value: &str) -> ConditionResult {
465 if value.len() != 33 {
466 return ConditionResult::from(false);
467 }
468 ConditionResult::from(value.chars().all(|c| c.is_ascii_alphanumeric()))
469}
470
471pub fn validate_malo_or_zahlpunkt(value: &str) -> ConditionResult {
473 if value.len() == 11 && validate_malo_id(value).is_true() {
474 return ConditionResult::True;
475 }
476 if value.len() == 33 && validate_zahlpunkt(value).is_true() {
477 return ConditionResult::True;
478 }
479 ConditionResult::False
480}
481
482pub fn validate_artikel_pattern(value: &str, segment_lengths: &[usize]) -> ConditionResult {
491 if value.is_empty() {
492 return ConditionResult::Unknown;
493 }
494 let parts: Vec<&str> = value.split('-').collect();
495 if parts.len() != segment_lengths.len() {
496 return ConditionResult::from(false);
497 }
498 let valid = parts
499 .iter()
500 .zip(segment_lengths.iter())
501 .all(|(part, &expected_len)| {
502 part.len() == expected_len && part.chars().all(|c| c.is_ascii_digit())
503 });
504 ConditionResult::from(valid)
505}
506
507pub fn validate_exact_length(value: &str, expected: usize) -> ConditionResult {
511 if value.is_empty() {
512 return ConditionResult::Unknown;
513 }
514 ConditionResult::from(value.len() == expected)
515}
516
517pub fn validate_max_length(value: &str, max: usize) -> ConditionResult {
519 if value.is_empty() {
520 return ConditionResult::Unknown;
521 }
522 ConditionResult::from(value.len() <= max)
523}
524
525pub fn validate_all_digits(value: &str) -> ConditionResult {
527 if value.is_empty() {
528 return ConditionResult::Unknown;
529 }
530 ConditionResult::from(value.chars().all(|c| c.is_ascii_digit()))
531}
532
533pub fn utc_now_ccyymmddhhmm() -> String {
535 chrono::Utc::now().format("%Y%m%d%H%M").to_string()
536}
537
538pub fn german_local_time(value: &str) -> Option<chrono::NaiveDateTime> {
545 use chrono::{Datelike, Duration, NaiveDate, NaiveDateTime, Weekday};
546 let value = value.replace('?', "");
547 let stamp = NaiveDateTime::parse_from_str(value.get(..12)?, "%Y%m%d%H%M").ok()?;
548 let offset: i64 = match value.get(12..) {
549 None | Some("") => 0,
550 Some(zone) => zone.parse().ok()?,
551 };
552 let utc = stamp - Duration::hours(offset);
553 let switch = |month: u32| {
554 let mut day = NaiveDate::from_ymd_opt(utc.year(), month, 31)?;
555 while day.weekday() != Weekday::Sun {
556 day = day.pred_opt()?;
557 }
558 day.and_hms_opt(1, 0, 0)
559 };
560 let summer = utc >= switch(3)? && utc < switch(10)?;
561 Some(utc + Duration::hours(if summer { 2 } else { 1 }))
562}
563
564#[cfg(test)]
565mod tests {
566
567 #[test]
568 fn date_values_follow_their_format_code() {
569 assert_eq!(fits_date_format("20250401", "102"), Some(true));
570 assert_eq!(fits_date_format("20250401", "104"), Some(false));
571 assert_eq!(fits_date_format("04010402", "104"), Some(true));
572 assert_eq!(fits_date_format("20250401", "106"), Some(false));
573 assert_eq!(fits_date_format("0401", "106"), Some(true));
574 assert_eq!(fits_date_format("202503312200+00", "303"), Some(true));
575 assert_eq!(fits_date_format("202503312200", "303"), Some(true));
576 assert_eq!(fits_date_format("20250331", "303"), Some(false));
577 assert_eq!(fits_date_format("20250331220000+00", "304"), Some(true));
578 assert_eq!(fits_date_format("2025", "602"), Some(true));
579 assert_eq!(fits_date_format("202513", "610"), Some(false));
580 assert_eq!(fits_date_format("12", "802"), Some(true));
581 assert_eq!(fits_date_format("06002200", "501"), Some(true));
582 assert_eq!(fits_date_format("03MM", "Z01"), Some(true));
583 assert_eq!(fits_date_format("20250401", "Z01"), Some(false));
584 assert_eq!(fits_date_format("x", "999"), None);
585 assert_eq!(fits_date_format("ä", "999"), None);
586 assert_eq!(fits_date_format("2025040ä", "102"), Some(false));
587 }
588
589 #[test]
592 fn obis_classes_follow_the_code_list() {
593 use super::*;
594 assert!(is_obis_zaehlerstand("1-1:1.8.0") && !is_obis_energiemenge("1-1:1.8.0"));
596 assert!(is_obis_energiemenge("1-1:1.9.0") && !is_obis_zaehlerstand("1-1:1.9.0"));
597 assert!(
598 is_obis_energiemenge("1-1:1.29.0") && is_obis_wirkarbeit_quarter_hour("1-1:1.29.0")
599 );
600 assert!(!is_obis_wirkarbeit_quarter_hour("1-1:1.9.0"));
601 assert!(is_obis_wirkleistung_maximum("1-1:2.6.0"));
603 assert!(!is_obis_wirkleistung_maximum("1-1:3.6.0"));
604 assert!(is_obis_blindarbeit("1-1:3.9.0") && is_obis_blindarbeit("1-1:4.29.0"));
605 assert!(!is_obis_blindarbeit("1-1:1.8.0"));
606 assert!(obis_has_zaehlzeit("1-1:1.9.1") && obis_has_zaehlzeit("1-1:1.8.62"));
608 assert!(!obis_has_zaehlzeit("1-1:1.9.0") && !obis_has_zaehlzeit("1-1:1.8.63"));
609 assert!(!is_obis_energiemenge("7-0:3.9.0"));
611 }
612
613 #[test]
614 fn the_tariff_is_the_e_of_c_d_e() {
615 use super::obis_tariff;
616 assert_eq!(obis_tariff("1-1:1.8.1"), Some(1));
617 assert_eq!(obis_tariff("1-0:2.8.62*255"), Some(62));
618 assert_eq!(obis_tariff("1-0:1.8.0"), Some(0));
619 assert_eq!(obis_tariff("1-0:1.8"), None);
620 assert_eq!(obis_tariff("x"), None);
621 }
622
623 #[test]
624 fn energy_registers_are_c_8_e_with_c_one_to_eight() {
625 use super::is_energy_register_obis;
626 for obis in [
627 "1-1:1.8.0",
628 "1-0:2.8.1",
629 "1-1:8.8.0",
630 "1-1:1.8.63",
631 "1-65:3.8.2",
632 ] {
633 assert!(is_energy_register_obis(obis), "{obis}");
634 }
635 for obis in [
636 "1-1:1.29.0",
637 "1-1:2.29.0",
638 "1-1:9.8.0",
639 "7-0:3.0.0",
640 "1-1:1.9.0",
641 "x",
642 ] {
643 assert!(!is_energy_register_obis(obis), "{obis}");
644 }
645 }
646
647 use super::*;
648
649 #[test]
652 fn test_max_decimal_places() {
653 assert_eq!(
654 validate_max_decimal_places("123.45", 2),
655 ConditionResult::True
656 );
657 assert_eq!(
658 validate_max_decimal_places("123.456", 2),
659 ConditionResult::False
660 );
661 assert_eq!(validate_max_decimal_places("123", 2), ConditionResult::True);
662 assert_eq!(validate_max_decimal_places("0.1", 3), ConditionResult::True);
663 assert_eq!(validate_max_decimal_places("", 2), ConditionResult::Unknown);
664 }
665
666 #[test]
667 fn test_no_decimal_places() {
668 assert_eq!(validate_max_decimal_places("100", 0), ConditionResult::True);
669 assert_eq!(
670 validate_max_decimal_places("100.5", 0),
671 ConditionResult::False
672 );
673 }
674
675 #[test]
676 fn test_max_integer_digits() {
677 assert_eq!(
678 validate_max_integer_digits("1234", 4),
679 ConditionResult::True
680 );
681 assert_eq!(
682 validate_max_integer_digits("12345", 4),
683 ConditionResult::False
684 );
685 assert_eq!(
686 validate_max_integer_digits("-123.45", 4),
687 ConditionResult::True
688 );
689 assert_eq!(validate_max_integer_digits("", 4), ConditionResult::Unknown);
690 }
691
692 #[test]
695 fn test_validate_numeric() {
696 assert_eq!(validate_numeric("5.0", ">=", 0.0), ConditionResult::True);
697 assert_eq!(validate_numeric("-1.0", ">=", 0.0), ConditionResult::False);
698 assert_eq!(validate_numeric("1", "==", 1.0), ConditionResult::True);
699 assert_eq!(validate_numeric("2", "==", 1.0), ConditionResult::False);
700 assert_eq!(validate_numeric("0", ">", 0.0), ConditionResult::False);
701 assert_eq!(validate_numeric("1", ">", 0.0), ConditionResult::True);
702 assert_eq!(validate_numeric("abc", ">=", 0.0), ConditionResult::False);
705 assert_eq!(
706 validate_numeric("O5D2DXBG1A", ">", 0.0),
707 ConditionResult::False
708 );
709 assert_eq!(validate_numeric("NaN", ">", 0.0), ConditionResult::False);
710 assert_eq!(validate_numeric("inf", ">", 0.0), ConditionResult::False);
711 assert_eq!(validate_numeric("1e3", ">", 0.0), ConditionResult::False);
712 assert_eq!(validate_numeric("0,5", ">", 0.0), ConditionResult::True);
714 assert_eq!(validate_numeric("-2", "<=", 0.0), ConditionResult::True);
715 assert_eq!(validate_numeric("", ">", 0.0), ConditionResult::Unknown);
716 }
717
718 #[test]
721 fn test_hhmm_equals() {
722 assert_eq!(
723 validate_hhmm_equals("202601012200+00", "2200"),
724 ConditionResult::True
725 );
726 assert_eq!(
727 validate_hhmm_equals("202601012300+00", "2200"),
728 ConditionResult::False
729 );
730 assert_eq!(
731 validate_hhmm_equals("short", "2200"),
732 ConditionResult::False
733 );
734 }
735
736 #[test]
737 fn test_hhmm_range() {
738 assert_eq!(
739 validate_hhmm_range("202601011530+00", "0000", "2359"),
740 ConditionResult::True
741 );
742 assert_eq!(
743 validate_hhmm_range("202601010000+00", "0000", "2359"),
744 ConditionResult::True
745 );
746 assert_eq!(
747 validate_hhmm_range("202601012359+00", "0000", "2359"),
748 ConditionResult::True
749 );
750 }
751
752 #[test]
753 fn test_mmddhhmm_equals() {
754 assert_eq!(
755 validate_mmddhhmm_equals("202612312300+00", "12312300"),
756 ConditionResult::True
757 );
758 assert_eq!(
759 validate_mmddhhmm_equals("202601012200+00", "12312300"),
760 ConditionResult::False
761 );
762 }
763
764 #[test]
765 fn test_timezone_utc() {
766 assert_eq!(
767 validate_timezone_utc("202601012200+00"),
768 ConditionResult::True
769 );
770 assert_eq!(
771 validate_timezone_utc("202601012200+01"),
772 ConditionResult::False
773 );
774 assert_eq!(
775 validate_timezone_utc("202601012200"),
776 ConditionResult::False
777 );
778 assert_eq!(
780 validate_timezone_utc("20260101220000+00"),
781 ConditionResult::True
782 );
783 assert_eq!(
784 validate_timezone_utc("20260101220000+01"),
785 ConditionResult::False
786 );
787 }
788
789 #[test]
792 fn test_email() {
793 assert_eq!(validate_email("user@example.com"), ConditionResult::True);
794 assert_eq!(validate_email("nope"), ConditionResult::False);
795 assert_eq!(validate_email("has@but-no-dot"), ConditionResult::False);
796 assert_eq!(validate_email(""), ConditionResult::Unknown);
797 }
798
799 #[test]
800 fn test_phone() {
801 assert_eq!(validate_phone("+4930123456"), ConditionResult::True);
802 assert_eq!(validate_phone("030123456"), ConditionResult::False);
803 assert_eq!(validate_phone("+"), ConditionResult::False);
804 assert_eq!(validate_phone("+49 30 123"), ConditionResult::False); assert_eq!(validate_phone(""), ConditionResult::Unknown);
806 }
807
808 #[test]
811 fn test_malo_id() {
812 assert_eq!(validate_malo_id("51238696781"), ConditionResult::True);
816 assert_eq!(validate_malo_id("41373559241"), ConditionResult::True);
817 assert_eq!(validate_malo_id("56789012345"), ConditionResult::True);
818 assert_eq!(validate_malo_id("52935155442"), ConditionResult::True);
819
820 assert_eq!(validate_malo_id("41373559240"), ConditionResult::False); assert_eq!(validate_malo_id("512386967890"), ConditionResult::False); assert_eq!(validate_malo_id("1234567890"), ConditionResult::False); assert_eq!(validate_malo_id("abcdefghijk"), ConditionResult::False); }
826
827 #[test]
828 fn test_zahlpunkt() {
829 let valid = "DE0001234567890123456789012345678";
830 assert_eq!(valid.len(), 33);
831 assert_eq!(validate_zahlpunkt(valid), ConditionResult::True);
832 assert_eq!(validate_zahlpunkt("tooshort"), ConditionResult::False);
833 }
834
835 #[test]
838 fn test_artikel_pattern() {
839 assert_eq!(
840 validate_artikel_pattern("1-23-4", &[1, 2, 1]),
841 ConditionResult::True
842 );
843 assert_eq!(
844 validate_artikel_pattern("1-23-4-567", &[1, 2, 1, 3]),
845 ConditionResult::True
846 );
847 assert_eq!(
848 validate_artikel_pattern("1-23-4-56", &[1, 2, 1, 3]),
849 ConditionResult::False
850 );
851 assert_eq!(
852 validate_artikel_pattern("1-AB-4", &[1, 2, 1]),
853 ConditionResult::False
854 );
855 assert_eq!(
856 validate_artikel_pattern("", &[1, 2, 1]),
857 ConditionResult::Unknown
858 );
859 }
860
861 #[test]
864 fn test_tr_id() {
865 assert_eq!(validate_tr_id("ABC123"), ConditionResult::True);
866 assert_eq!(validate_tr_id("A"), ConditionResult::True);
867 assert_eq!(validate_tr_id(&"A".repeat(35)), ConditionResult::True);
868 assert_eq!(validate_tr_id(&"A".repeat(36)), ConditionResult::False);
869 assert_eq!(validate_tr_id("has spaces"), ConditionResult::False);
870 assert_eq!(validate_tr_id("has-dash"), ConditionResult::False);
871 assert_eq!(validate_tr_id(""), ConditionResult::Unknown);
872 }
873
874 #[test]
875 fn test_parse_obis() {
876 assert_eq!(parse_obis("1-1:1.8.0"), Some((1, 8)));
877 assert_eq!(parse_obis("1-1:2.29.0"), Some((2, 29)));
878 assert_eq!(parse_obis("1-0:1.8.0*255"), Some((1, 8))); assert_eq!(parse_obis(" 1-1:1.8.0 "), Some((1, 8))); assert_eq!(parse_obis("no-colon"), None);
881 assert_eq!(parse_obis("1-1:abc.8.0"), None);
882 assert_eq!(parse_obis(""), None);
883 }
884
885 #[test]
886 fn test_obis_wirkarbeit_kumuliert() {
887 assert!(is_obis_wirkarbeit_kumuliert("1-1:1.8.0"));
889 assert!(is_obis_wirkarbeit_kumuliert("1-1:2.8.0"));
890 assert!(is_obis_wirkarbeit_kumuliert("1-1:1.9.0"));
891 assert!(is_obis_wirkarbeit_kumuliert("1-1:2.9.0"));
892 assert!(is_obis_wirkarbeit_kumuliert("1-0:1.8.0*255"));
893 assert!(!is_obis_wirkarbeit_kumuliert("1-1:3.8.0"));
895 assert!(!is_obis_wirkarbeit_kumuliert("1-1:4.8.0"));
896 assert!(!is_obis_wirkarbeit_kumuliert("1-1:1.29.0"));
898 assert!(!is_obis_wirkarbeit_kumuliert("1-1:1.5.0"));
900 }
901
902 #[test]
903 fn test_obis_wirkarbeit_quarter_hour() {
904 assert!(is_obis_wirkarbeit_quarter_hour("1-1:1.29.0"));
906 assert!(is_obis_wirkarbeit_quarter_hour("1-1:2.29.0"));
907 assert!(is_obis_wirkarbeit_quarter_hour("1-0:1.29.0*255"));
908 assert!(!is_obis_wirkarbeit_quarter_hour("1-1:1.8.0"));
910 assert!(!is_obis_wirkarbeit_quarter_hour("1-1:1.9.0"));
911 assert!(!is_obis_wirkarbeit_quarter_hour("1-1:3.29.0"));
913 }
914
915 #[test]
916 fn test_x509_cert_body() {
917 let body = "A".repeat(600);
919 assert_eq!(validate_x509_cert_body(&body), ConditionResult::True);
920
921 let pem_body = "A".repeat(64) + "\n" + &"B".repeat(64) + "\n" + &"C".repeat(64);
923 assert_eq!(validate_x509_cert_body(&pem_body), ConditionResult::True);
924
925 let armored = format!(
927 "-----BEGIN CERTIFICATE-----\n{}\n-----END CERTIFICATE-----",
928 "A".repeat(200)
929 );
930 assert_eq!(validate_x509_cert_body(&armored), ConditionResult::True);
931
932 let with_punct = "AB+/==".to_string() + &"A".repeat(200);
934 assert_eq!(validate_x509_cert_body(&with_punct), ConditionResult::True);
935
936 assert_eq!(validate_x509_cert_body("QUJD"), ConditionResult::False);
938
939 let invalid_chars = "A".repeat(200) + "!@#";
941 assert_eq!(
942 validate_x509_cert_body(&invalid_chars),
943 ConditionResult::False
944 );
945
946 assert_eq!(validate_x509_cert_body(""), ConditionResult::Unknown);
948 assert_eq!(validate_x509_cert_body(" \n "), ConditionResult::Unknown);
949 }
950
951 #[test]
955 fn test_sr_id() {
956 for real in ["C816417ST77", "CZZ6M5DGSS5", "COPFWS2EX96"] {
957 assert_eq!(validate_sr_id(real), ConditionResult::True, "{real}");
958 }
959 assert_eq!(
960 validate_sr_id("51238696781"),
961 ConditionResult::False,
962 "a MaLo-ID"
963 );
964 assert_eq!(validate_sr_id("C12345"), ConditionResult::False);
965 assert_eq!(validate_sr_id(""), ConditionResult::False);
966 }
967
968 #[test]
972 fn test_nelo_id() {
973 for real in ["E1688117482", "EU5UQAWW8D0", "EEW7TM7V906"] {
974 assert_eq!(validate_nelo_id(real), ConditionResult::True, "{real}");
975 }
976 assert_eq!(
977 validate_nelo_id("51238696781"),
978 ConditionResult::False,
979 "a MaLo-ID"
980 );
981 assert_eq!(validate_nelo_id("E123"), ConditionResult::False);
982 assert_eq!(validate_nelo_id("e1688117482"), ConditionResult::False);
983 }
984
985 #[test]
988 fn test_exact_length() {
989 assert_eq!(
990 validate_exact_length("1234567890123456", 16),
991 ConditionResult::True
992 );
993 assert_eq!(validate_exact_length("123", 16), ConditionResult::False);
994 assert_eq!(validate_exact_length("", 16), ConditionResult::Unknown);
995 }
996
997 #[test]
998 fn test_all_digits() {
999 assert_eq!(validate_all_digits("12345"), ConditionResult::True);
1000 assert_eq!(validate_all_digits("123a5"), ConditionResult::False);
1001 assert_eq!(validate_all_digits(""), ConditionResult::Unknown);
1002 }
1003}