reinhardt-core 0.1.1

Core components for Reinhardt framework
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
//! IBAN (International Bank Account Number) validator
//!
//! This module provides validation for IBAN according to ISO 13616 standard.
//! IBAN validation includes:
//! - Format validation (country code, check digits, BBAN)
//! - MOD-97 checksum validation
//! - Country-specific length validation
//! - Optional country code filtering

use super::{ValidationError, ValidationResult, Validator};
use std::collections::HashMap;

/// IBAN validator implementing ISO 13616 standard
///
/// The validator checks:
/// 1. Basic format (alphanumeric, uppercase)
/// 2. Country code validity and IBAN length for that country
/// 3. MOD-97 checksum algorithm
/// 4. Optional country filtering
///
/// # Examples
///
/// ```
/// use reinhardt_core::validators::{IBANValidator, Validator};
///
/// // Basic validation
/// let validator = IBANValidator::new();
/// assert!(validator.validate("DE89370400440532013000").is_ok());
///
/// // With country filtering
/// let validator = IBANValidator::with_countries(vec!["DE".to_string(), "FR".to_string()]);
/// assert!(validator.validate("DE89370400440532013000").is_ok());
/// assert!(validator.validate("GB82WEST12345698765432").is_err());
/// ```
pub struct IBANValidator {
	/// Optional list of allowed country codes (ISO 3166-1 alpha-2)
	pub country_codes: Option<Vec<String>>,
	country_lengths: HashMap<String, usize>,
	message: Option<String>,
}

impl IBANValidator {
	/// Creates a new IBAN validator with default settings
	///
	/// # Examples
	///
	/// ```
	/// use reinhardt_core::validators::IBANValidator;
	///
	/// let validator = IBANValidator::new();
	/// ```
	pub fn new() -> Self {
		Self {
			country_codes: None,
			country_lengths: Self::init_country_lengths(),
			message: None,
		}
	}

	/// Creates a new IBAN validator that only accepts specified country codes
	///
	/// # Examples
	///
	/// ```
	/// use reinhardt_core::validators::IBANValidator;
	///
	/// let validator = IBANValidator::with_countries(vec!["DE".to_string(), "FR".to_string()]);
	/// ```
	pub fn with_countries(codes: Vec<String>) -> Self {
		Self {
			country_codes: Some(codes.iter().map(|c| c.to_uppercase()).collect()),
			country_lengths: Self::init_country_lengths(),
			message: None,
		}
	}

	/// Sets a custom error message for validation failures.
	///
	/// # Examples
	///
	/// ```
	/// use reinhardt_core::validators::{IBANValidator, Validator};
	///
	/// let validator = IBANValidator::new().with_message("Invalid IBAN");
	/// let result = validator.validate("not-an-iban");
	/// assert!(result.is_err());
	/// ```
	pub fn with_message(mut self, message: impl Into<String>) -> Self {
		self.message = Some(message.into());
		self
	}

	/// Initializes the mapping of country codes to IBAN lengths
	///
	/// Based on ISO 13616 standard as of 2024
	fn init_country_lengths() -> HashMap<String, usize> {
		let mut map = HashMap::new();

		// European countries
		map.insert("AD".to_string(), 24); // Andorra
		map.insert("AT".to_string(), 20); // Austria
		map.insert("BE".to_string(), 16); // Belgium
		map.insert("BG".to_string(), 22); // Bulgaria
		map.insert("CH".to_string(), 21); // Switzerland
		map.insert("CY".to_string(), 28); // Cyprus
		map.insert("CZ".to_string(), 24); // Czech Republic
		map.insert("DE".to_string(), 22); // Germany
		map.insert("DK".to_string(), 18); // Denmark
		map.insert("EE".to_string(), 20); // Estonia
		map.insert("ES".to_string(), 24); // Spain
		map.insert("FI".to_string(), 18); // Finland
		map.insert("FR".to_string(), 27); // France
		map.insert("GB".to_string(), 22); // United Kingdom
		map.insert("GI".to_string(), 23); // Gibraltar
		map.insert("GR".to_string(), 27); // Greece
		map.insert("HR".to_string(), 21); // Croatia
		map.insert("HU".to_string(), 28); // Hungary
		map.insert("IE".to_string(), 22); // Ireland
		map.insert("IS".to_string(), 26); // Iceland
		map.insert("IT".to_string(), 27); // Italy
		map.insert("LI".to_string(), 21); // Liechtenstein
		map.insert("LT".to_string(), 20); // Lithuania
		map.insert("LU".to_string(), 20); // Luxembourg
		map.insert("LV".to_string(), 21); // Latvia
		map.insert("MC".to_string(), 27); // Monaco
		map.insert("MT".to_string(), 31); // Malta
		map.insert("NL".to_string(), 18); // Netherlands
		map.insert("NO".to_string(), 15); // Norway
		map.insert("PL".to_string(), 28); // Poland
		map.insert("PT".to_string(), 25); // Portugal
		map.insert("RO".to_string(), 24); // Romania
		map.insert("SE".to_string(), 24); // Sweden
		map.insert("SI".to_string(), 19); // Slovenia
		map.insert("SK".to_string(), 24); // Slovakia
		map.insert("SM".to_string(), 27); // San Marino
		map.insert("VA".to_string(), 22); // Vatican City

		// Middle East and North Africa
		map.insert("AE".to_string(), 23); // United Arab Emirates
		map.insert("BH".to_string(), 22); // Bahrain
		map.insert("IL".to_string(), 23); // Israel
		map.insert("JO".to_string(), 30); // Jordan
		map.insert("KW".to_string(), 30); // Kuwait
		map.insert("LB".to_string(), 28); // Lebanon
		map.insert("PS".to_string(), 29); // Palestine
		map.insert("QA".to_string(), 29); // Qatar
		map.insert("SA".to_string(), 24); // Saudi Arabia
		map.insert("TR".to_string(), 26); // Turkey

		// Other countries
		map.insert("AZ".to_string(), 28); // Azerbaijan
		map.insert("BR".to_string(), 29); // Brazil
		map.insert("CR".to_string(), 22); // Costa Rica
		map.insert("DO".to_string(), 28); // Dominican Republic
		map.insert("GE".to_string(), 22); // Georgia
		map.insert("GT".to_string(), 28); // Guatemala
		map.insert("KZ".to_string(), 20); // Kazakhstan
		map.insert("MD".to_string(), 24); // Moldova
		map.insert("MK".to_string(), 19); // North Macedonia
		map.insert("MR".to_string(), 27); // Mauritania
		map.insert("MU".to_string(), 30); // Mauritius
		map.insert("PK".to_string(), 24); // Pakistan
		map.insert("RS".to_string(), 22); // Serbia
		map.insert("TN".to_string(), 24); // Tunisia
		map.insert("UA".to_string(), 29); // Ukraine
		map.insert("XK".to_string(), 20); // Kosovo

		map
	}

	/// Validates IBAN format and structure
	///
	/// Returns Ok if valid, Err with ValidationError if invalid
	fn validate_format(&self, iban: &str) -> ValidationResult<String> {
		// Remove spaces and convert to uppercase
		let iban = iban.replace(' ', "").to_uppercase();

		// Check minimum length (15 is the shortest IBAN - Norway)
		if iban.len() < 15 {
			return Err(ValidationError::InvalidIBAN(
				"IBAN is too short".to_string(),
			));
		}

		// Check maximum length (31 is the longest IBAN - Malta)
		if iban.len() > 31 {
			return Err(ValidationError::InvalidIBAN("IBAN is too long".to_string()));
		}

		// Check if all characters are alphanumeric
		if !iban.chars().all(|c| c.is_ascii_alphanumeric()) {
			return Err(ValidationError::InvalidIBAN(
				"IBAN contains invalid characters".to_string(),
			));
		}

		// Extract country code (first 2 characters)
		let country_code = &iban[0..2];

		// Check if country code consists of letters only
		if !country_code.chars().all(|c| c.is_ascii_alphabetic()) {
			return Err(ValidationError::InvalidIBAN(
				"Invalid country code".to_string(),
			));
		}

		// Check if country code is in allowed list (if specified)
		if let Some(allowed) = &self.country_codes
			&& !allowed.contains(&country_code.to_string())
		{
			return Err(ValidationError::IBANCountryNotAllowed {
				country_code: country_code.to_string(),
				allowed_codes: allowed.join(", "),
			});
		}

		// Check country-specific length
		if let Some(&expected_length) = self.country_lengths.get(country_code) {
			if iban.len() != expected_length {
				return Err(ValidationError::InvalidIBAN(format!(
					"Invalid IBAN length for country '{}': expected {}, got {}",
					country_code,
					expected_length,
					iban.len()
				)));
			}
		} else {
			return Err(ValidationError::InvalidIBAN(format!(
				"Unknown country code: {}",
				country_code
			)));
		}

		// Extract check digits (characters 3-4)
		let check_digits = &iban[2..4];

		// Check if check digits are numeric
		if !check_digits.chars().all(|c| c.is_ascii_digit()) {
			return Err(ValidationError::InvalidIBAN(
				"Check digits must be numeric".to_string(),
			));
		}

		Ok(iban)
	}

	/// Performs MOD-97 checksum validation according to ISO 13616
	///
	/// Algorithm:
	/// 1. Move the first 4 characters to the end
	/// 2. Replace each letter with its numeric value (A=10, B=11, ..., Z=35)
	/// 3. Calculate the remainder when divided by 97
	/// 4. Valid IBAN has remainder of 1
	///
	/// # Examples
	///
	/// ```
	/// use reinhardt_core::validators::IBANValidator;
	///
	/// let validator = IBANValidator::new();
	/// // This is tested internally by the validate method
	/// ```
	fn mod97_check(iban: &str) -> bool {
		let iban = iban.replace(' ', "").to_uppercase();

		// Move first 4 characters to the end
		let rearranged = format!("{}{}", &iban[4..], &iban[0..4]);

		// Convert letters to numbers (A=10, B=11, ..., Z=35)
		let numeric_string: String = rearranged
			.chars()
			.map(|c| {
				if c.is_ascii_digit() {
					c.to_string()
				} else {
					// A=10, B=11, ..., Z=35
					(c as u32 - 'A' as u32 + 10).to_string()
				}
			})
			.collect();

		// Calculate MOD-97
		// We need to handle very large numbers, so we process in chunks
		let mut remainder: u32 = 0;

		for chunk in numeric_string.chars() {
			let digit = chunk.to_digit(10).unwrap();
			remainder = (remainder * 10 + digit) % 97;
		}

		remainder == 1
	}

	/// Returns custom error message if set, otherwise returns the fallback error
	fn error_with_fallback(&self, fallback: ValidationError) -> ValidationError {
		if let Some(ref msg) = self.message {
			ValidationError::Custom(msg.clone())
		} else {
			fallback
		}
	}
}

impl Default for IBANValidator {
	fn default() -> Self {
		Self::new()
	}
}

impl Validator<String> for IBANValidator {
	fn validate(&self, value: &String) -> ValidationResult<()> {
		// Validate format and get normalized IBAN
		let iban = self
			.validate_format(value)
			.map_err(|e| self.error_with_fallback(e))?;

		// Perform MOD-97 check
		if !Self::mod97_check(&iban) {
			return Err(self.error_with_fallback(ValidationError::InvalidIBAN(
				"Invalid IBAN checksum".to_string(),
			)));
		}

		Ok(())
	}
}

impl Validator<str> for IBANValidator {
	fn validate(&self, value: &str) -> ValidationResult<()> {
		// Validate format and get normalized IBAN
		let iban = self
			.validate_format(value)
			.map_err(|e| self.error_with_fallback(e))?;

		// Perform MOD-97 check
		if !Self::mod97_check(&iban) {
			return Err(self.error_with_fallback(ValidationError::InvalidIBAN(
				"Invalid IBAN checksum".to_string(),
			)));
		}

		Ok(())
	}
}

#[cfg(test)]
mod tests {
	use super::*;

	// Valid IBAN test cases from various countries
	#[test]
	fn test_valid_ibans() {
		let validator = IBANValidator::new();

		let valid_ibans = vec![
			"DE89370400440532013000",      // Germany
			"GB82WEST12345698765432",      // United Kingdom
			"FR1420041010050500013M02606", // France
			"IT60X0542811101000000123456", // Italy
			"ES9121000418450200051332",    // Spain
			"NL91ABNA0417164300",          // Netherlands
			"BE68539007547034",            // Belgium
			"CH9300762011623852957",       // Switzerland
			"AT611904300234573201",        // Austria
			"NO9386011117947",             // Norway (shortest IBAN)
		];

		for iban in valid_ibans {
			assert!(
				validator.validate(iban).is_ok(),
				"Expected {} to be valid",
				iban
			);
		}
	}

	// Test IBANs with spaces (should be handled correctly)
	#[test]
	fn test_ibans_with_spaces() {
		let validator = IBANValidator::new();

		// IBANs are often written with spaces for readability
		assert!(validator.validate("DE89 3704 0044 0532 0130 00").is_ok());
		assert!(validator.validate("GB82 WEST 1234 5698 7654 32").is_ok());
		assert!(
			validator
				.validate("FR14 2004 1010 0505 0001 3M02 606")
				.is_ok()
		);
	}

	#[test]
	fn test_invalid_ibans() {
		let validator = IBANValidator::new();

		let invalid_ibans = vec![
			"DE89370400440532013001",            // Wrong check digit
			"GB82WEST12345698765433",            // Wrong check digit
			"XX1234567890",                      // Unknown country code
			"DE8937040044053201300",             // Too short for Germany
			"DE893704004405320130000",           // Too long for Germany
			"12345678901234567890",              // No country code
			"DEAA370400440532013000",            // Invalid check digits (should be numeric)
			"",                                  // Empty string
			"DE",                                // Too short
			"DE89 3704 0044 0532 0130 00 EXTRA", // Too long with extra characters
		];

		for iban in invalid_ibans {
			assert!(
				validator.validate(iban).is_err(),
				"Expected {} to be invalid",
				iban
			);
		}
	}

	#[test]
	fn test_mod97_algorithm() {
		// Test the MOD-97 algorithm directly
		assert!(IBANValidator::mod97_check("DE89370400440532013000"));
		assert!(IBANValidator::mod97_check("GB82WEST12345698765432"));
		assert!(IBANValidator::mod97_check("NO9386011117947"));

		// Invalid checksums
		assert!(!IBANValidator::mod97_check("DE89370400440532013001"));
		assert!(!IBANValidator::mod97_check("GB82WEST12345698765433"));
	}

	#[test]
	fn test_allowed_countries() {
		let validator = IBANValidator::with_countries(vec!["DE".to_string(), "FR".to_string()]);

		// German IBAN should be accepted
		assert!(validator.validate("DE89370400440532013000").is_ok());

		// French IBAN should be accepted
		assert!(validator.validate("FR1420041010050500013M02606").is_ok());

		// British IBAN should be rejected (not in allowed list)
		let result = validator.validate("GB82WEST12345698765432");
		assert!(result.is_err());
		match result {
			Err(ValidationError::IBANCountryNotAllowed {
				country_code,
				allowed_codes,
			}) => {
				assert_eq!(country_code, "GB");
				assert!(allowed_codes.contains("DE"));
				assert!(allowed_codes.contains("FR"));
			}
			_ => panic!("Expected IBANCountryNotAllowed error"),
		}
	}

	#[test]
	fn test_country_code_validation() {
		let validator = IBANValidator::new();

		// Invalid country codes
		assert!(validator.validate("12345678901234567890123456").is_err()); // No letters
		assert!(validator.validate("A1234567890123456789012345").is_err()); // Single letter country code
		assert!(validator.validate("1A234567890123456789012345").is_err()); // Number in country code
	}

	#[test]
	fn test_check_digit_validation() {
		let validator = IBANValidator::new();

		// Check digits must be numeric
		assert!(validator.validate("DEAA370400440532013000").is_err());
		assert!(validator.validate("DE8A370400440532013000").is_err());
		assert!(validator.validate("DEA9370400440532013000").is_err());
	}

	#[test]
	fn test_case_insensitivity() {
		let validator = IBANValidator::new();

		// Lowercase should work (will be converted to uppercase internally)
		assert!(validator.validate("de89370400440532013000").is_ok());
		assert!(validator.validate("gb82west12345698765432").is_ok());

		// Mixed case
		assert!(validator.validate("De89370400440532013000").is_ok());
		assert!(validator.validate("GB82west12345698765432").is_ok());
	}

	#[test]
	fn test_specific_country_lengths() {
		let validator = IBANValidator::new();

		// Norway has the shortest IBAN (15 characters)
		assert!(validator.validate("NO9386011117947").is_ok());

		// Malta has the longest IBAN (31 characters)
		// Note: This is a fabricated example for testing length
		// Real Maltese IBANs would need proper check digits
		let maltese_length_test = "MT84MALT011000012345MTLCAST001S";
		// We expect this to fail on checksum, not length
		assert!(validator.validate_format(maltese_length_test).is_ok());
	}

	#[test]
	fn test_invalid_characters() {
		let validator = IBANValidator::new();

		assert!(validator.validate("DE89-3704-0044-0532-0130-00").is_err()); // Hyphens
		assert!(validator.validate("DE89.3704.0044.0532.0130.00").is_err()); // Dots
		assert!(validator.validate("DE89_3704_0044_0532_0130_00").is_err()); // Underscores
		assert!(validator.validate("DE89/3704/0044/0532/0130/00").is_err()); // Slashes
	}

	#[test]
	fn test_string_type_validation() {
		let validator = IBANValidator::new();

		let iban = String::from("DE89370400440532013000");
		assert!(validator.validate(&iban).is_ok());

		let invalid = String::from("INVALID");
		assert!(validator.validate(&invalid).is_err());
	}

	#[test]
	fn test_with_countries_builder() {
		let validator = IBANValidator::with_countries(vec!["DE".to_string()]);

		assert!(validator.validate("DE89370400440532013000").is_ok());
		assert!(validator.validate("GB82WEST12345698765432").is_err());
	}

	// Additional edge cases
	#[test]
	fn test_numeric_string_conversion_in_mod97() {
		// This tests that the MOD-97 algorithm correctly handles
		// the conversion of letters to numbers
		// For DE89370400440532013000:
		// Rearranged: 370400440532013000DE89
		// Converted: 3704004405320130001314 (D=13, E=14)
		assert!(IBANValidator::mod97_check("DE89370400440532013000"));
	}

	#[test]
	fn test_all_supported_countries() {
		let validator = IBANValidator::new();

		// Verify that all countries in the map are accessible
		assert_eq!(validator.country_lengths.len(), 63); // Total number of supported countries

		// Spot check a few countries
		assert_eq!(validator.country_lengths.get("DE"), Some(&22));
		assert_eq!(validator.country_lengths.get("GB"), Some(&22));
		assert_eq!(validator.country_lengths.get("FR"), Some(&27));
		assert_eq!(validator.country_lengths.get("NO"), Some(&15));
		assert_eq!(validator.country_lengths.get("MT"), Some(&31));
	}
}