brazilian_utils 0.1.0

Brazilian utilities for Rust - validation and formatting for CPF, CNPJ, RENAVAM, voter ID, and more
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
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
/// Currency formatting utilities for Brazilian Real (BRL).
const ONES: &[&str] = &[
    "", "um", "dois", "três", "quatro", "cinco", "seis", "sete", "oito", "nove",
];

const TENS_TEENS: &[&str] = &[
    "",
    "onze",
    "doze",
    "treze",
    "catorze",
    "quinze",
    "dezesseis",
    "dezessete",
    "dezoito",
    "dezenove",
];

const TENS: &[&str] = &[
    "",
    "dez",
    "vinte",
    "trinta",
    "quarenta",
    "cinquenta",
    "sessenta",
    "setenta",
    "oitenta",
    "noventa",
];

const HUNDREDS: &[&str] = &[
    "",
    "cento",
    "duzentos",
    "trezentos",
    "quatrocentos",
    "quinhentos",
    "seiscentos",
    "setecentos",
    "oitocentos",
    "novecentos",
];

/// Convert an integer to its Portuguese (Brazilian) word representation.
///
/// # Arguments
///
/// * `n` - The number to convert (0-999,999,999,999,999,999)
///
/// # Returns
///
/// The number written in words in Brazilian Portuguese.
///
/// # Examples
///
/// ```
/// use brazilian_utils::currency::number_to_words;
///
/// assert_eq!(number_to_words(123), "cento e vinte e três");
/// assert_eq!(number_to_words(1000), "mil");
/// ```
pub fn number_to_words(n: i64) -> String {
    if n == 0 {
        return "zero".to_string();
    }

    if n < 0 {
        return format!("menos {}", number_to_words(-n));
    }

    // Handle quadrillions (10^15)
    if n >= 1_000_000_000_000_000 {
        let quadrillions = n / 1_000_000_000_000_000;
        let remainder = n % 1_000_000_000_000_000;

        let quadrillion_text = if quadrillions == 1 {
            "um quatrilhão".to_string()
        } else {
            format!("{} quatrilhões", number_to_words(quadrillions))
        };

        if remainder == 0 {
            return quadrillion_text;
        }

        let connector = if remainder < 100 { " e " } else { ", " };
        return format!(
            "{}{}{}",
            quadrillion_text,
            connector,
            number_to_words(remainder)
        );
    }

    // Handle trillions (10^12)
    if n >= 1_000_000_000_000 {
        let trillions = n / 1_000_000_000_000;
        let remainder = n % 1_000_000_000_000;

        let trillion_text = if trillions == 1 {
            "um trilhão".to_string()
        } else {
            format!("{} trilhões", number_to_words(trillions))
        };

        if remainder == 0 {
            return trillion_text;
        }

        let connector = if remainder < 100 { " e " } else { ", " };
        return format!(
            "{}{}{}",
            trillion_text,
            connector,
            number_to_words(remainder)
        );
    }

    // Handle billions (10^9)
    if n >= 1_000_000_000 {
        let billions = n / 1_000_000_000;
        let remainder = n % 1_000_000_000;

        let billion_text = if billions == 1 {
            "um bilhão".to_string()
        } else {
            format!("{} bilhões", number_to_words(billions))
        };

        if remainder == 0 {
            return billion_text;
        }

        let connector = if remainder < 100 { " e " } else { ", " };
        return format!(
            "{}{}{}",
            billion_text,
            connector,
            number_to_words(remainder)
        );
    }

    // Handle millions (10^6)
    if n >= 1_000_000 {
        let millions = n / 1_000_000;
        let remainder = n % 1_000_000;

        let million_text = if millions == 1 {
            "um milhão".to_string()
        } else {
            format!("{} milhões", number_to_words(millions))
        };

        if remainder == 0 {
            return million_text;
        }

        let connector = if remainder < 100 { " e " } else { ", " };
        return format!(
            "{}{}{}",
            million_text,
            connector,
            number_to_words(remainder)
        );
    }

    // Handle thousands (10^3)
    if n >= 1000 {
        let thousands = n / 1000;
        let remainder = n % 1000;

        let thousand_text = if thousands == 1 {
            "mil".to_string()
        } else {
            format!("{} mil", number_to_words(thousands))
        };

        if remainder == 0 {
            return thousand_text;
        }

        // Use "e" for round hundreds (100, 200, 300, etc.), otherwise use ","
        let connector = if remainder % 100 == 0 || remainder < 100 {
            " e "
        } else {
            ", "
        };
        return format!(
            "{}{}{}",
            thousand_text,
            connector,
            number_to_words(remainder)
        );
    }

    // Handle hundreds (100-999)
    if n >= 100 {
        let hundreds_digit = (n / 100) as usize;
        let remainder = n % 100;

        let hundred_text = if n == 100 {
            "cem".to_string()
        } else {
            HUNDREDS[hundreds_digit].to_string()
        };

        if remainder == 0 {
            return hundred_text;
        }

        return format!("{} e {}", hundred_text, number_to_words(remainder));
    }

    // Handle numbers from 20-99
    if n >= 20 {
        let tens_digit = (n / 10) as usize;
        let ones_digit = (n % 10) as usize;

        if ones_digit == 0 {
            return TENS[tens_digit].to_string();
        }

        return format!("{} e {}", TENS[tens_digit], ONES[ones_digit]);
    }

    // Handle teens (11-19)
    if n >= 11 {
        let index = (n - 10) as usize;
        return TENS_TEENS[index].to_string();
    }

    // Handle 1-10
    if n == 10 {
        return "dez".to_string();
    }

    ONES[n as usize].to_string()
}

/// Format a numeric value as Brazilian currency (R$).
///
/// This function takes a numeric value and formats it according to Brazilian
/// currency standards:
/// - Adds "R$" prefix
/// - Uses comma (,) as decimal separator
/// - Uses period (.) as thousands separator
/// - Always shows 2 decimal places
///
/// # Arguments
///
/// * `value` - The numeric value to format (can be positive, negative, or zero).
///
/// # Returns
///
/// A formatted currency string (e.g., "R$ 1.234,56") or None if the value cannot be formatted.
///
/// # Examples
///
/// ```
/// use brazilian_utils::currency::format_currency;
///
/// assert_eq!(format_currency(1234.56), Some("R$ 1.234,56".to_string()));
/// assert_eq!(format_currency(0.0), Some("R$ 0,00".to_string()));
/// assert_eq!(format_currency(-9876.54), Some("R$ -9.876,54".to_string()));
/// ```
pub fn format_currency(value: f64) -> Option<String> {
    if !value.is_finite() {
        return None;
    }

    // Round to 2 decimal places
    let rounded = (value * 100.0).round() / 100.0;

    // Separate integer and decimal parts
    let abs_value = rounded.abs();
    let integer_part = abs_value.floor() as i64;
    let decimal_part = ((abs_value - abs_value.floor()) * 100.0).round() as i64;

    // Check if the value is effectively zero after rounding
    let is_zero = integer_part == 0 && decimal_part == 0;

    // Handle negative sign (but treat -0.00 as 0.00)
    let negative = rounded < 0.0 && !is_zero;

    // Format integer part with thousands separator
    let integer_str = format_with_thousands_separator(integer_part);

    // Format with 2 decimal places
    let formatted = format!("{},{:02}", integer_str, decimal_part);

    // Add currency symbol and negative sign if needed
    if negative {
        Some(format!("R$ -{}", formatted))
    } else {
        Some(format!("R$ {}", formatted))
    }
}

/// Helper function to format an integer with thousands separator (period).
fn format_with_thousands_separator(mut num: i64) -> String {
    if num == 0 {
        return "0".to_string();
    }

    let mut result = Vec::new();
    let mut count = 0;

    while num > 0 {
        if count == 3 {
            result.push('.');
            count = 0;
        }
        result.push(char::from_digit((num % 10) as u32, 10).unwrap());
        num /= 10;
        count += 1;
    }

    result.reverse();
    result.into_iter().collect()
}

/// Converts a Real (BRL) value to its written text representation in Brazilian Portuguese.
///
/// # Arguments
///
/// * `value` - The monetary value in Brazilian Reais to convert
///
/// # Returns
///
/// The value written in full in Brazilian Portuguese.
///
/// # Examples
///
/// ```
/// use brazilian_utils::currency::convert_real_to_text;
///
/// assert_eq!(convert_real_to_text(1.00), "um real");
/// assert_eq!(convert_real_to_text(2.50), "dois reais e cinquenta centavos");
/// assert_eq!(convert_real_to_text(1000000.00), "um milhão de reais");
/// ```
pub fn convert_real_to_text(value: f64) -> String {
    if value == 0.0 || value.abs() < 0.005 {
        return "zero real".to_string();
    }

    let is_negative = value < 0.0;
    let abs_value = value.abs();

    // Split into integer and decimal parts
    let reais = abs_value.floor() as i64;
    let centavos = ((abs_value - reais as f64) * 100.0).round() as i64;

    let mut result = String::new();

    if is_negative {
        result.push_str("menos ");
    }

    if reais > 0 {
        let reais_text = number_to_words(reais);

        // Add "de" before "reais" for large numbers (million and above)
        let currency_name = if reais == 1 {
            "real".to_string()
        } else if reais >= 1_000_000 {
            "de reais".to_string()
        } else {
            "reais".to_string()
        };

        result.push_str(&format!("{} {}", reais_text, currency_name));
    }

    if centavos > 0 {
        if reais > 0 {
            result.push_str(" e ");
        }

        let centavos_text = number_to_words(centavos);
        let centavo_text = if centavos == 1 { "centavo" } else { "centavos" };

        result.push_str(&format!("{} {}", centavos_text, centavo_text));
    }

    result
}

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

    #[test]
    fn test_format_currency_positive_values() {
        assert_eq!(format_currency(1234.56), Some("R$ 1.234,56".to_string()));
        assert_eq!(
            format_currency(123236.70),
            Some("R$ 123.236,70".to_string())
        );
        assert_eq!(format_currency(1259.03), Some("R$ 1.259,03".to_string()));
    }

    #[test]
    fn test_format_currency_zero() {
        assert_eq!(format_currency(0.0), Some("R$ 0,00".to_string()));
    }

    #[test]
    fn test_format_currency_negative_values() {
        assert_eq!(
            format_currency(-123236.70),
            Some("R$ -123.236,70".to_string())
        );
        assert_eq!(format_currency(-9876.54), Some("R$ -9.876,54".to_string()));
    }

    #[test]
    fn test_format_currency_rounding() {
        // Test decimal rounding
        assert_eq!(
            format_currency(123236.7676),
            Some("R$ 123.236,77".to_string())
        );
        assert_eq!(format_currency(10.555), Some("R$ 10,56".to_string()));
    }

    #[test]
    fn test_format_currency_small_values() {
        assert_eq!(format_currency(0.01), Some("R$ 0,01".to_string()));
        assert_eq!(format_currency(0.99), Some("R$ 0,99".to_string()));
        assert_eq!(format_currency(5.50), Some("R$ 5,50".to_string()));
    }

    #[test]
    fn test_format_currency_large_values() {
        assert_eq!(
            format_currency(1_000_000.00),
            Some("R$ 1.000.000,00".to_string())
        );
        assert_eq!(
            format_currency(999_999_999.99),
            Some("R$ 999.999.999,99".to_string())
        );
    }

    #[test]
    fn test_format_currency_invalid_values() {
        assert_eq!(format_currency(f64::INFINITY), None);
        assert_eq!(format_currency(f64::NEG_INFINITY), None);
        assert_eq!(format_currency(f64::NAN), None);
    }

    #[test]
    fn test_format_with_thousands_separator() {
        assert_eq!(format_with_thousands_separator(0), "0");
        assert_eq!(format_with_thousands_separator(123), "123");
        assert_eq!(format_with_thousands_separator(1234), "1.234");
        assert_eq!(format_with_thousands_separator(123456), "123.456");
        assert_eq!(format_with_thousands_separator(1234567), "1.234.567");
        assert_eq!(format_with_thousands_separator(999999999), "999.999.999");
    }

    #[test]
    fn test_format_currency_edge_cases() {
        // Very small positive value
        assert_eq!(format_currency(0.001), Some("R$ 0,00".to_string()));

        // Very small negative value - rounds to 0 but we treat as positive zero
        assert_eq!(format_currency(-0.001), Some("R$ 0,00".to_string()));

        // Values that need rounding
        assert_eq!(format_currency(1.234), Some("R$ 1,23".to_string()));
        assert_eq!(format_currency(1.235), Some("R$ 1,24".to_string()));
    }

    #[test]
    fn test_number_to_words_basic() {
        assert_eq!(number_to_words(0), "zero");
        assert_eq!(number_to_words(1), "um");
        assert_eq!(number_to_words(5), "cinco");
        assert_eq!(number_to_words(10), "dez");
        assert_eq!(number_to_words(15), "quinze");
        assert_eq!(number_to_words(20), "vinte");
        assert_eq!(number_to_words(25), "vinte e cinco");
        assert_eq!(number_to_words(99), "noventa e nove");
    }

    #[test]
    fn test_number_to_words_hundreds() {
        assert_eq!(number_to_words(100), "cem");
        assert_eq!(number_to_words(101), "cento e um");
        assert_eq!(number_to_words(200), "duzentos");
        assert_eq!(number_to_words(555), "quinhentos e cinquenta e cinco");
        assert_eq!(number_to_words(999), "novecentos e noventa e nove");
    }

    #[test]
    fn test_number_to_words_thousands() {
        assert_eq!(number_to_words(1000), "mil");
        assert_eq!(number_to_words(1001), "mil e um");
        assert_eq!(number_to_words(1111), "mil, cento e onze");
        assert_eq!(number_to_words(2000), "dois mil");
        assert_eq!(number_to_words(2500), "dois mil e quinhentos");
        assert_eq!(number_to_words(10000), "dez mil");
        assert_eq!(number_to_words(100000), "cem mil");
    }

    #[test]
    fn test_number_to_words_large_numbers() {
        assert_eq!(number_to_words(1_000_000), "um milhão");
        assert_eq!(number_to_words(2_000_000), "dois milhões");
        assert_eq!(number_to_words(1_000_000_000), "um bilhão");
        assert_eq!(number_to_words(2_000_000_000), "dois bilhões");
        assert_eq!(number_to_words(1_000_000_000_000), "um trilhão");
        assert_eq!(number_to_words(1_000_000_000_000_000), "um quatrilhão");
    }

    #[test]
    fn test_number_to_words_negative() {
        assert_eq!(number_to_words(-1), "menos um");
        assert_eq!(number_to_words(-42), "menos quarenta e dois");
        assert_eq!(number_to_words(-1000), "menos mil");
    }

    #[test]
    fn test_convert_real_to_text_basic() {
        assert_eq!(convert_real_to_text(0.0), "zero real");
        assert_eq!(convert_real_to_text(1.0), "um real");
        assert_eq!(convert_real_to_text(2.0), "dois reais");
        assert_eq!(convert_real_to_text(10.0), "dez reais");
        assert_eq!(convert_real_to_text(100.0), "cem reais");
    }

    #[test]
    fn test_convert_real_to_text_with_cents() {
        assert_eq!(convert_real_to_text(0.01), "um centavo");
        assert_eq!(convert_real_to_text(0.50), "cinquenta centavos");
        assert_eq!(convert_real_to_text(1.50), "um real e cinquenta centavos");
        assert_eq!(
            convert_real_to_text(2.50),
            "dois reais e cinquenta centavos"
        );
        assert_eq!(
            convert_real_to_text(10.99),
            "dez reais e noventa e nove centavos"
        );
    }

    #[test]
    fn test_convert_real_to_text_large_values() {
        assert_eq!(convert_real_to_text(1000.0), "mil reais");
        assert_eq!(convert_real_to_text(1000000.0), "um milhão de reais");
        assert_eq!(convert_real_to_text(2000000.0), "dois milhões de reais");
        assert_eq!(convert_real_to_text(1000000000.0), "um bilhão de reais");
        assert_eq!(
            convert_real_to_text(1000000.50),
            "um milhão de reais e cinquenta centavos"
        );
    }

    #[test]
    fn test_convert_real_to_text_negative() {
        assert_eq!(convert_real_to_text(-1.0), "menos um real");
        assert_eq!(
            convert_real_to_text(-2.50),
            "menos dois reais e cinquenta centavos"
        );
        assert_eq!(convert_real_to_text(-100.0), "menos cem reais");
    }

    #[test]
    fn test_convert_real_to_text_complex() {
        assert_eq!(
            convert_real_to_text(1111.11),
            "mil, cento e onze reais e onze centavos"
        );
        assert_eq!(convert_real_to_text(123456.78), "cento e vinte e três mil, quatrocentos e cinquenta e seis reais e setenta e oito centavos");
    }
}