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 {
61 let parsed = match value.parse::<f64>() {
62 Ok(v) => v,
63 Err(_) => return ConditionResult::Unknown,
64 };
65 let result = match op {
66 "==" => (parsed - threshold).abs() < f64::EPSILON,
67 "!=" => (parsed - threshold).abs() >= f64::EPSILON,
68 ">" => parsed > threshold,
69 ">=" => parsed >= threshold,
70 "<" => parsed < threshold,
71 "<=" => parsed <= threshold,
72 _ => return ConditionResult::Unknown,
73 };
74 ConditionResult::from(result)
75}
76
77pub fn validate_hhmm_equals(dtm_value: &str, expected_hhmm: &str) -> ConditionResult {
86 if dtm_value.len() < 12 {
87 return ConditionResult::False;
89 }
90 ConditionResult::from(&dtm_value[8..12] == expected_hhmm)
91}
92
93pub fn validate_hhmm_range(dtm_value: &str, min: &str, max: &str) -> ConditionResult {
97 if dtm_value.len() < 12 {
98 return ConditionResult::False;
99 }
100 let hhmm = &dtm_value[8..12];
101 ConditionResult::from(hhmm >= min && hhmm <= max)
102}
103
104pub fn validate_mmddhhmm_equals(dtm_value: &str, expected: &str) -> ConditionResult {
110 if dtm_value.len() < 12 {
111 return ConditionResult::False;
112 }
113 ConditionResult::from(&dtm_value[4..12] == expected)
114}
115
116pub fn validate_timezone_utc(dtm_value: &str) -> ConditionResult {
124 if dtm_value.len() < 15 {
125 return ConditionResult::False;
127 }
128 ConditionResult::from(&dtm_value[12..] == "+00")
129}
130
131pub fn validate_email(value: &str) -> ConditionResult {
137 if value.is_empty() {
138 return ConditionResult::Unknown;
139 }
140 ConditionResult::from(value.contains('@') && value.contains('.'))
141}
142
143pub fn validate_phone(value: &str) -> ConditionResult {
148 if value.is_empty() {
149 return ConditionResult::Unknown;
150 }
151 if !value.starts_with('+') || value.len() < 2 {
152 return ConditionResult::from(false);
153 }
154 ConditionResult::from(value[1..].chars().all(|c| c.is_ascii_digit()))
155}
156
157pub fn validate_malo_id(value: &str) -> ConditionResult {
171 if value.len() != 11 {
172 return ConditionResult::from(false);
173 }
174 if !value.chars().all(|c| c.is_ascii_digit()) {
175 return ConditionResult::from(false);
176 }
177 let digits: Vec<u32> = value.chars().filter_map(|c| c.to_digit(10)).collect();
178 let check = digits[10];
179 let mut sum = 0u32;
180 for (i, &d) in digits[..10].iter().enumerate() {
181 if (i + 1) % 2 == 0 {
183 sum += 2 * d;
184 } else {
185 sum += d;
186 }
187 }
188 let expected = (10 - (sum % 10)) % 10;
189 ConditionResult::from(check == expected)
190}
191
192pub fn validate_tr_id(value: &str) -> ConditionResult {
194 if value.is_empty() {
195 return ConditionResult::Unknown;
196 }
197 ConditionResult::from(value.len() <= 35 && value.chars().all(|c| c.is_ascii_alphanumeric()))
198}
199
200pub fn validate_sr_id(value: &str) -> ConditionResult {
202 validate_malo_id(value)
203}
204
205pub fn parse_obis(value: &str) -> Option<(u32, u32)> {
222 let value = value.trim();
223 let (_ab, cde) = value.split_once(':')?;
225 let cde = cde.split('*').next()?;
227 let mut parts = cde.split('.');
228 let c = parts.next()?.parse::<u32>().ok()?;
229 let d = parts.next()?.parse::<u32>().ok()?;
230 Some((c, d))
231}
232
233pub fn is_obis_wirkarbeit_kumuliert(value: &str) -> bool {
239 matches!(parse_obis(value), Some((1 | 2, 8 | 9)))
240}
241
242pub fn is_obis_wirkarbeit_quarter_hour(value: &str) -> bool {
249 matches!(parse_obis(value), Some((1 | 2, 29)))
250}
251
252pub fn validate_x509_cert_body(value: &str) -> ConditionResult {
271 let trimmed = value.trim();
272 if trimmed.is_empty() {
273 return ConditionResult::Unknown;
274 }
275 let body = trimmed
278 .trim_start_matches("-----BEGIN CERTIFICATE-----")
279 .trim_end_matches("-----END CERTIFICATE-----")
280 .trim();
281 let chars_ok = body.chars().all(|c| {
282 c.is_ascii_alphanumeric() || matches!(c, '+' | '/' | '=' | '\n' | '\r' | '\t' | ' ')
283 });
284 let len_ok = body.chars().filter(|c| !c.is_whitespace()).count() >= 100;
287 ConditionResult::from(chars_ok && len_ok)
288}
289
290pub fn validate_zahlpunkt(value: &str) -> ConditionResult {
292 if value.len() != 33 {
293 return ConditionResult::from(false);
294 }
295 ConditionResult::from(value.chars().all(|c| c.is_ascii_alphanumeric()))
296}
297
298pub fn validate_malo_or_zahlpunkt(value: &str) -> ConditionResult {
300 if value.len() == 11 && validate_malo_id(value).is_true() {
301 return ConditionResult::True;
302 }
303 if value.len() == 33 && validate_zahlpunkt(value).is_true() {
304 return ConditionResult::True;
305 }
306 ConditionResult::False
307}
308
309pub fn validate_artikel_pattern(value: &str, segment_lengths: &[usize]) -> ConditionResult {
318 if value.is_empty() {
319 return ConditionResult::Unknown;
320 }
321 let parts: Vec<&str> = value.split('-').collect();
322 if parts.len() != segment_lengths.len() {
323 return ConditionResult::from(false);
324 }
325 let valid = parts
326 .iter()
327 .zip(segment_lengths.iter())
328 .all(|(part, &expected_len)| {
329 part.len() == expected_len && part.chars().all(|c| c.is_ascii_digit())
330 });
331 ConditionResult::from(valid)
332}
333
334pub fn validate_exact_length(value: &str, expected: usize) -> ConditionResult {
338 if value.is_empty() {
339 return ConditionResult::Unknown;
340 }
341 ConditionResult::from(value.len() == expected)
342}
343
344pub fn validate_max_length(value: &str, max: usize) -> ConditionResult {
346 if value.is_empty() {
347 return ConditionResult::Unknown;
348 }
349 ConditionResult::from(value.len() <= max)
350}
351
352pub fn validate_all_digits(value: &str) -> ConditionResult {
354 if value.is_empty() {
355 return ConditionResult::Unknown;
356 }
357 ConditionResult::from(value.chars().all(|c| c.is_ascii_digit()))
358}
359
360pub fn utc_now_ccyymmddhhmm() -> String {
362 chrono::Utc::now().format("%Y%m%d%H%M").to_string()
363}
364
365#[cfg(test)]
366mod tests {
367 use super::*;
368
369 #[test]
372 fn test_max_decimal_places() {
373 assert_eq!(
374 validate_max_decimal_places("123.45", 2),
375 ConditionResult::True
376 );
377 assert_eq!(
378 validate_max_decimal_places("123.456", 2),
379 ConditionResult::False
380 );
381 assert_eq!(validate_max_decimal_places("123", 2), ConditionResult::True);
382 assert_eq!(validate_max_decimal_places("0.1", 3), ConditionResult::True);
383 assert_eq!(validate_max_decimal_places("", 2), ConditionResult::Unknown);
384 }
385
386 #[test]
387 fn test_no_decimal_places() {
388 assert_eq!(validate_max_decimal_places("100", 0), ConditionResult::True);
389 assert_eq!(
390 validate_max_decimal_places("100.5", 0),
391 ConditionResult::False
392 );
393 }
394
395 #[test]
396 fn test_max_integer_digits() {
397 assert_eq!(
398 validate_max_integer_digits("1234", 4),
399 ConditionResult::True
400 );
401 assert_eq!(
402 validate_max_integer_digits("12345", 4),
403 ConditionResult::False
404 );
405 assert_eq!(
406 validate_max_integer_digits("-123.45", 4),
407 ConditionResult::True
408 );
409 assert_eq!(validate_max_integer_digits("", 4), ConditionResult::Unknown);
410 }
411
412 #[test]
415 fn test_validate_numeric() {
416 assert_eq!(validate_numeric("5.0", ">=", 0.0), ConditionResult::True);
417 assert_eq!(validate_numeric("-1.0", ">=", 0.0), ConditionResult::False);
418 assert_eq!(validate_numeric("1", "==", 1.0), ConditionResult::True);
419 assert_eq!(validate_numeric("2", "==", 1.0), ConditionResult::False);
420 assert_eq!(validate_numeric("0", ">", 0.0), ConditionResult::False);
421 assert_eq!(validate_numeric("1", ">", 0.0), ConditionResult::True);
422 assert_eq!(validate_numeric("abc", ">=", 0.0), ConditionResult::Unknown);
423 }
424
425 #[test]
428 fn test_hhmm_equals() {
429 assert_eq!(
430 validate_hhmm_equals("202601012200+00", "2200"),
431 ConditionResult::True
432 );
433 assert_eq!(
434 validate_hhmm_equals("202601012300+00", "2200"),
435 ConditionResult::False
436 );
437 assert_eq!(
438 validate_hhmm_equals("short", "2200"),
439 ConditionResult::False
440 );
441 }
442
443 #[test]
444 fn test_hhmm_range() {
445 assert_eq!(
446 validate_hhmm_range("202601011530+00", "0000", "2359"),
447 ConditionResult::True
448 );
449 assert_eq!(
450 validate_hhmm_range("202601010000+00", "0000", "2359"),
451 ConditionResult::True
452 );
453 assert_eq!(
454 validate_hhmm_range("202601012359+00", "0000", "2359"),
455 ConditionResult::True
456 );
457 }
458
459 #[test]
460 fn test_mmddhhmm_equals() {
461 assert_eq!(
462 validate_mmddhhmm_equals("202612312300+00", "12312300"),
463 ConditionResult::True
464 );
465 assert_eq!(
466 validate_mmddhhmm_equals("202601012200+00", "12312300"),
467 ConditionResult::False
468 );
469 }
470
471 #[test]
472 fn test_timezone_utc() {
473 assert_eq!(
474 validate_timezone_utc("202601012200+00"),
475 ConditionResult::True
476 );
477 assert_eq!(
478 validate_timezone_utc("202601012200+01"),
479 ConditionResult::False
480 );
481 assert_eq!(
482 validate_timezone_utc("202601012200"),
483 ConditionResult::False
484 );
485 }
486
487 #[test]
490 fn test_email() {
491 assert_eq!(validate_email("user@example.com"), ConditionResult::True);
492 assert_eq!(validate_email("nope"), ConditionResult::False);
493 assert_eq!(validate_email("has@but-no-dot"), ConditionResult::False);
494 assert_eq!(validate_email(""), ConditionResult::Unknown);
495 }
496
497 #[test]
498 fn test_phone() {
499 assert_eq!(validate_phone("+4930123456"), ConditionResult::True);
500 assert_eq!(validate_phone("030123456"), ConditionResult::False);
501 assert_eq!(validate_phone("+"), ConditionResult::False);
502 assert_eq!(validate_phone("+49 30 123"), ConditionResult::False); assert_eq!(validate_phone(""), ConditionResult::Unknown);
504 }
505
506 #[test]
509 fn test_malo_id() {
510 assert_eq!(validate_malo_id("51238696781"), ConditionResult::True);
514 assert_eq!(validate_malo_id("41373559241"), ConditionResult::True);
515 assert_eq!(validate_malo_id("56789012345"), ConditionResult::True);
516 assert_eq!(validate_malo_id("52935155442"), ConditionResult::True);
517
518 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); }
524
525 #[test]
526 fn test_zahlpunkt() {
527 let valid = "DE0001234567890123456789012345678";
528 assert_eq!(valid.len(), 33);
529 assert_eq!(validate_zahlpunkt(valid), ConditionResult::True);
530 assert_eq!(validate_zahlpunkt("tooshort"), ConditionResult::False);
531 }
532
533 #[test]
536 fn test_artikel_pattern() {
537 assert_eq!(
538 validate_artikel_pattern("1-23-4", &[1, 2, 1]),
539 ConditionResult::True
540 );
541 assert_eq!(
542 validate_artikel_pattern("1-23-4-567", &[1, 2, 1, 3]),
543 ConditionResult::True
544 );
545 assert_eq!(
546 validate_artikel_pattern("1-23-4-56", &[1, 2, 1, 3]),
547 ConditionResult::False
548 );
549 assert_eq!(
550 validate_artikel_pattern("1-AB-4", &[1, 2, 1]),
551 ConditionResult::False
552 );
553 assert_eq!(
554 validate_artikel_pattern("", &[1, 2, 1]),
555 ConditionResult::Unknown
556 );
557 }
558
559 #[test]
562 fn test_tr_id() {
563 assert_eq!(validate_tr_id("ABC123"), ConditionResult::True);
564 assert_eq!(validate_tr_id("A"), ConditionResult::True);
565 assert_eq!(validate_tr_id(&"A".repeat(35)), ConditionResult::True);
566 assert_eq!(validate_tr_id(&"A".repeat(36)), ConditionResult::False);
567 assert_eq!(validate_tr_id("has spaces"), ConditionResult::False);
568 assert_eq!(validate_tr_id("has-dash"), ConditionResult::False);
569 assert_eq!(validate_tr_id(""), ConditionResult::Unknown);
570 }
571
572 #[test]
573 fn test_parse_obis() {
574 assert_eq!(parse_obis("1-1:1.8.0"), Some((1, 8)));
575 assert_eq!(parse_obis("1-1:2.29.0"), Some((2, 29)));
576 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);
579 assert_eq!(parse_obis("1-1:abc.8.0"), None);
580 assert_eq!(parse_obis(""), None);
581 }
582
583 #[test]
584 fn test_obis_wirkarbeit_kumuliert() {
585 assert!(is_obis_wirkarbeit_kumuliert("1-1:1.8.0"));
587 assert!(is_obis_wirkarbeit_kumuliert("1-1:2.8.0"));
588 assert!(is_obis_wirkarbeit_kumuliert("1-1:1.9.0"));
589 assert!(is_obis_wirkarbeit_kumuliert("1-1:2.9.0"));
590 assert!(is_obis_wirkarbeit_kumuliert("1-0:1.8.0*255"));
591 assert!(!is_obis_wirkarbeit_kumuliert("1-1:3.8.0"));
593 assert!(!is_obis_wirkarbeit_kumuliert("1-1:4.8.0"));
594 assert!(!is_obis_wirkarbeit_kumuliert("1-1:1.29.0"));
596 assert!(!is_obis_wirkarbeit_kumuliert("1-1:1.5.0"));
598 }
599
600 #[test]
601 fn test_obis_wirkarbeit_quarter_hour() {
602 assert!(is_obis_wirkarbeit_quarter_hour("1-1:1.29.0"));
604 assert!(is_obis_wirkarbeit_quarter_hour("1-1:2.29.0"));
605 assert!(is_obis_wirkarbeit_quarter_hour("1-0:1.29.0*255"));
606 assert!(!is_obis_wirkarbeit_quarter_hour("1-1:1.8.0"));
608 assert!(!is_obis_wirkarbeit_quarter_hour("1-1:1.9.0"));
609 assert!(!is_obis_wirkarbeit_quarter_hour("1-1:3.29.0"));
611 }
612
613 #[test]
614 fn test_x509_cert_body() {
615 let body = "A".repeat(600);
617 assert_eq!(validate_x509_cert_body(&body), ConditionResult::True);
618
619 let pem_body = "A".repeat(64) + "\n" + &"B".repeat(64) + "\n" + &"C".repeat(64);
621 assert_eq!(validate_x509_cert_body(&pem_body), ConditionResult::True);
622
623 let armored = format!(
625 "-----BEGIN CERTIFICATE-----\n{}\n-----END CERTIFICATE-----",
626 "A".repeat(200)
627 );
628 assert_eq!(validate_x509_cert_body(&armored), ConditionResult::True);
629
630 let with_punct = "AB+/==".to_string() + &"A".repeat(200);
632 assert_eq!(validate_x509_cert_body(&with_punct), ConditionResult::True);
633
634 assert_eq!(validate_x509_cert_body("QUJD"), ConditionResult::False);
636
637 let invalid_chars = "A".repeat(200) + "!@#";
639 assert_eq!(
640 validate_x509_cert_body(&invalid_chars),
641 ConditionResult::False
642 );
643
644 assert_eq!(validate_x509_cert_body(""), ConditionResult::Unknown);
646 assert_eq!(validate_x509_cert_body(" \n "), ConditionResult::Unknown);
647 }
648
649 #[test]
650 fn test_sr_id() {
651 assert_eq!(validate_sr_id("51238696781"), ConditionResult::True);
654 assert_eq!(validate_sr_id("41373559240"), ConditionResult::False);
655 assert_eq!(validate_sr_id("1234567890"), ConditionResult::False);
656 assert_eq!(validate_sr_id(""), ConditionResult::False);
657 }
658
659 #[test]
662 fn test_exact_length() {
663 assert_eq!(
664 validate_exact_length("1234567890123456", 16),
665 ConditionResult::True
666 );
667 assert_eq!(validate_exact_length("123", 16), ConditionResult::False);
668 assert_eq!(validate_exact_length("", 16), ConditionResult::Unknown);
669 }
670
671 #[test]
672 fn test_all_digits() {
673 assert_eq!(validate_all_digits("12345"), ConditionResult::True);
674 assert_eq!(validate_all_digits("123a5"), ConditionResult::False);
675 assert_eq!(validate_all_digits(""), ConditionResult::Unknown);
676 }
677}