stdbr-core 0.0.5

Standard library for Brazil
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
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
//! RG (Registro Geral) - per-UF identity card validation, formatting and generation.
//!
//! RG is issued independently by each Brazilian state and there is no single
//! national algorithm. Only **SP** has a widely-adopted, documented mod-11
//! check digit algorithm — implemented here in full. For every other UF this
//! module performs **structural validation only** (length range + digit
//! charset) and treats the input as opaque digits.
//!
//! # SP algorithm
//!
//! 8-digit body `d1..d8`. Weights `9,8,7,6,5,4,3,2` applied left-to-right.
//! `sum = Σ d_i * w_i`. Check digit = `sum mod 11`; remainder
//! `10` is rendered as the ASCII character `'X'`. Canonical formatted form is
//! `XX.XXX.XXX-X`.
//!
//! # Other UFs
//!
//! `is_valid`/`is_valid_strict` only enforce length (5..=14 digits). Generation
//! returns `RgError::UnsupportedUfForGeneration`. Promote a UF from structural
//! to full validation by extending `uf_spec` once an authoritative algorithm
//! is verified.

use alloc::string::String;
use alloc::vec::Vec;
use core::fmt;

use crate::rand::{simple_seed, xorshift64};
use crate::uf::State;

const RG_MAX_LEN: usize = 14;
const SP_BODY_LEN: u8 = 9;
const SP_FORMATTED_LEN: u8 = 12;
const SP_BASE_LEN: usize = 8;
const SP_WEIGHTS: [u32; SP_BASE_LEN] = [9, 8, 7, 6, 5, 4, 3, 2];
const SP_FORMATTED_DIGIT_POS: [usize; 9] = [0, 1, 3, 4, 5, 7, 8, 9, 11];

const STRUCTURAL_MIN_LEN: u8 = 5;
const STRUCTURAL_MAX_LEN: u8 = 14;

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum RgError {
    InvalidLength,
    InvalidCharacter,
    InvalidFormat,
    InvalidCheckDigit,
    UnsupportedUfForGeneration,
}

impl fmt::Display for RgError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(match self {
            Self::InvalidLength => "RG length is outside the accepted range for this UF",
            Self::InvalidCharacter => "RG contains invalid characters",
            Self::InvalidFormat => "RG format does not match the canonical mask for this UF",
            Self::InvalidCheckDigit => "RG check digit is invalid",
            Self::UnsupportedUfForGeneration => {
                "RG generation is not supported for this UF (no verified algorithm)"
            }
        })
    }
}

/// Per-UF formatting and validation spec.
#[derive(Clone, Copy)]
struct UfSpec {
    body_len: Option<u8>,
    has_check_digit: bool,
    allow_x_terminator: bool,
    separators: &'static [(u8, char)],
    formatted_len: Option<u8>,
}

const STRUCTURAL_DEFAULT: UfSpec = UfSpec {
    body_len: None,
    has_check_digit: false,
    allow_x_terminator: false,
    separators: &[],
    formatted_len: None,
};

const SP_SPEC: UfSpec = UfSpec {
    body_len: Some(SP_BODY_LEN),
    has_check_digit: true,
    allow_x_terminator: true,
    separators: &[(2, '.'), (5, '.'), (8, '-')],
    formatted_len: Some(SP_FORMATTED_LEN),
};

const fn uf_spec(uf: State) -> UfSpec {
    match uf {
        State::SP => SP_SPEC,
        _ => STRUCTURAL_DEFAULT,
    }
}

/// A validated RG stored as ASCII bytes (digits, plus optional trailing `'X'`
/// for SP), tagged with its issuing UF.
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub struct Rg {
    bytes: [u8; RG_MAX_LEN],
    len: u8,
    uf: State,
}

impl Rg {
    /// Unformatted body as `&str` (digits, optionally trailing `'X'`).
    pub fn as_str(&self) -> &str {
        // SAFETY: constructors guarantee ASCII digits/`X` only.
        unsafe { core::str::from_utf8_unchecked(&self.bytes[..self.len as usize]) }
    }

    /// Issuing state.
    pub fn uf(&self) -> State {
        self.uf
    }

    /// Formatted per the UF mask. For UFs without a known mask, returns the
    /// unformatted body.
    pub fn formatted(&self) -> String {
        format_with_spec(self.as_str(), uf_spec(self.uf)).unwrap_or_else(|| self.as_str().into())
    }

    /// Masked representation — shows the first 2 digits and masks the rest.
    ///
    /// SP: `"294653272"` → `"29.***.***-*"` (formatted with separators).
    /// Other UFs: `"1234567"` → `"12*****"` (no separators).
    pub fn masked(&self) -> String {
        let s = self.as_str();
        let spec = uf_spec(self.uf);
        if spec.has_check_digit && s.len() == SP_BODY_LEN as usize {
            let mut out = String::with_capacity(SP_FORMATTED_LEN as usize);
            out.push_str(&s[..2]);
            out.push('.');
            out.push_str("***");
            out.push('.');
            out.push_str("***");
            out.push('-');
            out.push('*');
            out
        } else {
            let mut out = String::with_capacity(s.len());
            for (i, _) in s.chars().enumerate() {
                if i < 2 {
                    out.push(s.as_bytes()[i] as char);
                } else {
                    out.push('*');
                }
            }
            out
        }
    }

    /// Body without the check digit.
    ///
    /// SP: returns the 8-digit base (without DV).
    /// Other UFs: returns `as_str()` (no DV is identifiable).
    pub fn body(&self) -> &str {
        let spec = uf_spec(self.uf);
        if spec.has_check_digit && self.len as usize == SP_BODY_LEN as usize {
            // SAFETY: constructors guarantee ASCII content.
            unsafe { core::str::from_utf8_unchecked(&self.bytes[..SP_BASE_LEN]) }
        } else {
            self.as_str()
        }
    }

    /// Check digit when the UF has a verified algorithm. `Some(0..=9)` for
    /// digits, `Some(10)` for the SP `'X'` terminator, `None` otherwise.
    pub fn check_digit(&self) -> Option<u8> {
        let spec = uf_spec(self.uf);
        if !spec.has_check_digit {
            return None;
        }
        let last = self.bytes[self.len as usize - 1];
        if last == b'X' {
            Some(10)
        } else {
            Some(last - b'0')
        }
    }
}

impl AsRef<str> for Rg {
    fn as_ref(&self) -> &str {
        self.as_str()
    }
}

impl fmt::Display for Rg {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(&self.formatted())
    }
}

impl fmt::Debug for Rg {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "Rg({}, {})", self.uf.abbreviation(), self.formatted())
    }
}

/// Strip dots, dashes, slashes and whitespace. For SP, preserve a trailing
/// `'X'` (case-insensitive, normalized to uppercase). For other UFs, drop
/// non-digits.
pub fn remove_symbols(rg: &str, uf: State) -> String {
    let spec = uf_spec(uf);
    let mut out = String::with_capacity(rg.len());
    for c in rg.chars() {
        if c.is_ascii_digit() {
            out.push(c);
        } else if spec.allow_x_terminator && (c == 'X' || c == 'x') {
            out.push('X');
        }
    }
    out
}

/// Lenient validation - strips symbols, then checks length and (for SP) the
/// check digit.
pub fn is_valid(rg: &str, uf: State) -> bool {
    let spec = uf_spec(uf);
    let raw = remove_symbols(rg, uf);
    if !validate_body_length(&raw, spec) {
        return false;
    }
    if !validate_charset(&raw, spec) {
        return false;
    }
    if spec.has_check_digit {
        return sp_check_digit_ok(&raw);
    }
    true
}

/// Strict validation - input must be either the canonical formatted mask or
/// the unformatted body. No leading/trailing whitespace, no extra symbols.
pub fn is_valid_strict(rg: &str, uf: State) -> Result<(), RgError> {
    parse_strict(rg, uf).map(|_| ())
}

/// Insert per-UF separators. Returns `None` if the input length doesn't match
/// the UF body length (or, for variable-length UFs, the structural range).
pub fn format_rg(rg: &str, uf: State) -> Option<String> {
    let spec = uf_spec(uf);
    let raw = remove_symbols(rg, uf);
    if !validate_body_length(&raw, spec) {
        return None;
    }
    Some(format_with_spec(&raw, spec).unwrap_or(raw))
}

/// SP-only: compute the check digit for an 8-digit base. Returns `Some(0..=9)`
/// or `Some(10)` (caller renders as `'X'`); `None` for non-SP or wrong length.
pub fn compute_check_digit(base: &str, uf: State) -> Option<u8> {
    if !matches!(uf, State::SP) {
        return None;
    }
    let raw: Vec<u8> = base.bytes().filter(u8::is_ascii_digit).collect();
    if raw.len() != SP_BASE_LEN {
        return None;
    }
    Some(sp_check_digit(&raw))
}

/// Parse a raw RG string into a validated [`Rg`].
pub fn parse_strict(raw: &str, uf: State) -> Result<Rg, RgError> {
    let spec = uf_spec(uf);
    let bytes = raw.as_bytes();

    let body = if let Some(len) = spec.formatted_len.filter(|&l| bytes.len() == l as usize) {
        let _ = len;
        parse_formatted_sp(bytes)?
    } else if let Some(body_len) = spec.body_len {
        if bytes.len() != body_len as usize {
            return Err(RgError::InvalidLength);
        }
        parse_unformatted(bytes, spec)?
    } else {
        if bytes.len() < STRUCTURAL_MIN_LEN as usize || bytes.len() > STRUCTURAL_MAX_LEN as usize {
            return Err(RgError::InvalidLength);
        }
        parse_unformatted(bytes, spec)?
    };

    if spec.has_check_digit && !sp_check_digit_ok_bytes(&body) {
        return Err(RgError::InvalidCheckDigit);
    }

    Ok(Rg::from_body(&body, uf))
}

/// Generate a random valid RG. SP only; other UFs return
/// `RgError::UnsupportedUfForGeneration`.
pub fn generate(uf: State) -> Result<Rg, RgError> {
    if !matches!(uf, State::SP) {
        return Err(RgError::UnsupportedUfForGeneration);
    }
    let mut seed = simple_seed();
    let mut digits = [0u8; SP_BASE_LEN];
    for d in &mut digits {
        seed = xorshift64(seed);
        *d = (seed % 10) as u8;
    }
    let mut body = [0u8; 9];
    for (i, &d) in digits.iter().enumerate() {
        body[i] = d + b'0';
    }
    let dv = sp_check_digit(&digits);
    body[8] = if dv == 10 { b'X' } else { b'0' + dv };
    Ok(Rg::from_body(&body, State::SP))
}

impl Rg {
    fn from_body(body: &[u8], uf: State) -> Self {
        let mut bytes = [0u8; RG_MAX_LEN];
        bytes[..body.len()].copy_from_slice(body);
        Self {
            bytes,
            len: body.len() as u8,
            uf,
        }
    }
}

fn validate_body_length(raw: &str, spec: UfSpec) -> bool {
    match spec.body_len {
        Some(n) => raw.len() == n as usize,
        None => {
            let n = raw.len();
            n >= STRUCTURAL_MIN_LEN as usize && n <= STRUCTURAL_MAX_LEN as usize
        }
    }
}

fn validate_charset(raw: &str, spec: UfSpec) -> bool {
    let bytes = raw.as_bytes();
    if bytes.is_empty() {
        return false;
    }
    if spec.allow_x_terminator {
        let (last_idx, rest) = (bytes.len() - 1, &bytes[..bytes.len() - 1]);
        if !rest.iter().all(u8::is_ascii_digit) {
            return false;
        }
        let last = bytes[last_idx];
        last.is_ascii_digit() || last == b'X'
    } else {
        bytes.iter().all(u8::is_ascii_digit)
    }
}

fn parse_unformatted(bytes: &[u8], spec: UfSpec) -> Result<Vec<u8>, RgError> {
    if !validate_charset(
        // SAFETY: caller already verified ASCII boundaries via spec body_len/range checks.
        unsafe { core::str::from_utf8_unchecked(bytes) },
        spec,
    ) {
        return Err(RgError::InvalidCharacter);
    }
    Ok(bytes.to_vec())
}

fn parse_formatted_sp(bytes: &[u8]) -> Result<Vec<u8>, RgError> {
    // Format mask: `XX.XXX.XXX-X` — separators at offsets 2, 6, 10.
    if bytes[2] != b'.' || bytes[6] != b'.' || bytes[10] != b'-' {
        return Err(RgError::InvalidFormat);
    }
    let mut out = Vec::with_capacity(SP_BODY_LEN as usize);
    for (i, &idx) in SP_FORMATTED_DIGIT_POS.iter().enumerate() {
        let b = bytes[idx];
        let last = i == SP_FORMATTED_DIGIT_POS.len() - 1;
        if b.is_ascii_digit() || (last && b == b'X') {
            out.push(b);
        } else if last && b == b'x' {
            out.push(b'X');
        } else {
            return Err(RgError::InvalidCharacter);
        }
    }
    Ok(out)
}

fn format_with_spec(body: &str, spec: UfSpec) -> Option<String> {
    if spec.separators.is_empty() {
        return None;
    }
    let body_len = spec.body_len? as usize;
    if body.len() != body_len {
        return None;
    }
    let total = body_len + spec.separators.len();
    let mut out = String::with_capacity(total);
    let mut sep_iter = spec.separators.iter().peekable();
    for (i, ch) in body.chars().enumerate() {
        while let Some(&&(pos, sep_ch)) = sep_iter.peek() {
            if pos as usize == i && i != 0 {
                out.push(sep_ch);
                sep_iter.next();
            } else {
                break;
            }
        }
        out.push(ch);
    }
    Some(out)
}

fn sp_check_digit(digits: &[u8]) -> u8 {
    let sum: u32 = digits
        .iter()
        .zip(SP_WEIGHTS.iter())
        .map(|(&d, &w)| u32::from(d) * w)
        .sum();
    (sum % 11) as u8
}

fn sp_check_digit_ok(body: &str) -> bool {
    sp_check_digit_ok_bytes(body.as_bytes())
}

fn sp_check_digit_ok_bytes(bytes: &[u8]) -> bool {
    if bytes.len() != SP_BODY_LEN as usize {
        return false;
    }
    let mut digits = [0u8; SP_BASE_LEN];
    for (i, &b) in bytes[..SP_BASE_LEN].iter().enumerate() {
        if !b.is_ascii_digit() {
            return false;
        }
        digits[i] = b - b'0';
    }
    let expected = sp_check_digit(&digits);
    let last = bytes[SP_BASE_LEN];
    let actual = if last == b'X' {
        10
    } else if last.is_ascii_digit() {
        last - b'0'
    } else {
        return false;
    };
    actual == expected
}

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

    #[test]
    fn real_rg_29465327_2() {
        // Source: bosontreinamentos.com.br
        assert!(is_valid("294653272", State::SP));
        assert!(is_valid("29.465.327-2", State::SP));
        let parsed = parse_strict("294653272", State::SP).unwrap();
        assert_eq!(parsed.as_str(), "294653272");
        assert_eq!(parsed.check_digit(), Some(2));
        assert_eq!(parsed.formatted(), "29.465.327-2");
        let parsed_fmt = parse_strict("29.465.327-2", State::SP).unwrap();
        assert_eq!(parsed_fmt, parsed);
    }

    #[test]
    fn real_rg_39406714_9() {
        // Source: dev.to/shadowlik
        assert!(is_valid("394067149", State::SP));
        assert!(is_valid("39.406.714-9", State::SP));
        let parsed = parse_strict("394067149", State::SP).unwrap();
        assert_eq!(parsed.as_str(), "394067149");
        assert_eq!(parsed.check_digit(), Some(9));
        assert_eq!(parsed.formatted(), "39.406.714-9");
        let parsed_fmt = parse_strict("39.406.714-9", State::SP).unwrap();
        assert_eq!(parsed_fmt, parsed);
    }

    #[test]
    fn compute_check_digit_real_rgs() {
        assert_eq!(compute_check_digit("29465327", State::SP), Some(2));
        assert_eq!(compute_check_digit("39406714", State::SP), Some(9));
    }

    #[test]
    fn format_real_rgs() {
        assert_eq!(
            format_rg("294653272", State::SP),
            Some("29.465.327-2".into())
        );
        assert_eq!(
            format_rg("394067149", State::SP),
            Some("39.406.714-9".into())
        );
    }

    #[test]
    fn sp_check_digit_known_values() {
        // 12345678: sum = 9+16+21+24+25+24+21+16 = 156; 156 mod 11 = 2.
        assert_eq!(sp_check_digit(&[1, 2, 3, 4, 5, 6, 7, 8]), 2);
        // 44444444: sum = 4*44 = 176; 176 mod 11 = 0.
        assert_eq!(sp_check_digit(&[4, 4, 4, 4, 4, 4, 4, 4]), 0);
        // 11111111: sum = 1*44 = 44; 44 mod 11 = 0.
        assert_eq!(sp_check_digit(&[1, 1, 1, 1, 1, 1, 1, 1]), 0);
        // 60000000: sum = 6*9 = 54; 54 mod 11 = 10 → 'X'.
        assert_eq!(sp_check_digit(&[6, 0, 0, 0, 0, 0, 0, 0]), 10);
    }

    #[test]
    fn is_valid_sp_accepts_valid() {
        assert!(is_valid("123456782", State::SP));
        assert!(is_valid("12.345.678-2", State::SP));
    }

    #[test]
    fn is_valid_sp_rejects_wrong_dv() {
        assert!(!is_valid("123456789", State::SP));
        assert!(!is_valid("12.345.678-9", State::SP));
        assert!(!is_valid("294653271", State::SP));
    }

    #[test]
    fn is_valid_sp_x_terminator() {
        assert!(is_valid("60000000X", State::SP));
        assert!(is_valid("60.000.000-X", State::SP));
        assert!(is_valid("60.000.000-x", State::SP));
    }

    #[test]
    fn is_valid_sp_lenient_strips_garbage() {
        let s = "123456782";
        let garbage = alloc::format!("{}!{}@{}#{}", &s[0..2], &s[2..5], &s[5..8], &s[8..9]);
        assert!(is_valid(&garbage, State::SP));
    }

    #[test]
    fn is_valid_sp_rejects_wrong_length() {
        assert!(!is_valid("", State::SP));
        assert!(!is_valid("12345", State::SP));
        assert!(!is_valid("1234567890", State::SP));
    }

    #[test]
    fn structural_other_uf_accepts_any_digits_in_range() {
        assert!(is_valid("12345", State::RJ));
        assert!(is_valid("1234567890", State::MG));
        assert!(is_valid("12345678901234", State::PR));
    }

    #[test]
    fn structural_other_uf_rejects_too_short_or_long() {
        assert!(!is_valid("1234", State::RJ));
        assert!(!is_valid("123456789012345", State::RJ));
    }

    #[test]
    fn structural_other_uf_rejects_letters() {
        assert!(!is_valid("abcdef", State::RJ));
        assert!(!is_valid("", State::RJ));
    }

    #[test]
    fn strict_sp_accepts_unformatted() {
        assert!(is_valid_strict("123456782", State::SP).is_ok());
        assert!(is_valid_strict("294653272", State::SP).is_ok());
    }

    #[test]
    fn strict_sp_accepts_formatted() {
        assert!(is_valid_strict("12.345.678-2", State::SP).is_ok());
        assert!(is_valid_strict("29.465.327-2", State::SP).is_ok());
    }

    #[test]
    fn strict_sp_rejects_misplaced_separators() {
        assert_eq!(
            is_valid_strict("123.45.678-2", State::SP),
            Err(RgError::InvalidFormat)
        );
        assert_eq!(
            is_valid_strict("12.345.6782-", State::SP),
            Err(RgError::InvalidFormat)
        );
    }

    #[test]
    fn strict_sp_rejects_garbage() {
        assert_eq!(
            is_valid_strict("12.345.678!2", State::SP),
            Err(RgError::InvalidFormat)
        );
    }

    #[test]
    fn strict_sp_rejects_bad_dv() {
        assert_eq!(
            is_valid_strict("123456789", State::SP),
            Err(RgError::InvalidCheckDigit)
        );
    }

    #[test]
    fn strict_sp_rejects_x_in_middle() {
        assert_eq!(
            is_valid_strict("1234X6782", State::SP),
            Err(RgError::InvalidCharacter)
        );
    }

    #[test]
    fn strict_other_uf_accepts_digits_only() {
        assert!(is_valid_strict("1234567", State::RJ).is_ok());
    }

    #[test]
    fn strict_other_uf_rejects_separators() {
        assert!(is_valid_strict("12.345.67", State::RJ).is_err());
    }

    #[test]
    fn parse_sp_roundtrip() {
        let parsed = parse_strict("123456782", State::SP).unwrap();
        assert_eq!(parsed.as_str(), "123456782");
        let parsed_fmt = parse_strict("12.345.678-2", State::SP).unwrap();
        assert_eq!(parsed_fmt, parsed);
    }

    #[test]
    fn parse_sp_x_terminator() {
        let parsed = parse_strict("60.000.000-X", State::SP).unwrap();
        assert_eq!(parsed.as_str(), "60000000X");
        assert_eq!(parsed.check_digit(), Some(10));
    }

    #[test]
    fn parse_other_uf_returns_digits() {
        let parsed = parse_strict("1234567", State::RJ).unwrap();
        assert_eq!(parsed.as_str(), "1234567");
        assert_eq!(parsed.uf(), State::RJ);
        assert_eq!(parsed.check_digit(), None);
    }

    #[test]
    fn format_sp_inserts_separators() {
        assert_eq!(
            format_rg("123456782", State::SP),
            Some("12.345.678-2".into())
        );
        assert_eq!(
            format_rg("60000000X", State::SP),
            Some("60.000.000-X".into())
        );
    }

    #[test]
    fn format_sp_passes_through_already_formatted() {
        assert_eq!(
            format_rg("12.345.678-2", State::SP),
            Some("12.345.678-2".into())
        );
    }

    #[test]
    fn format_other_uf_returns_digits_unchanged() {
        assert_eq!(format_rg("1234567", State::RJ), Some("1234567".into()));
    }

    #[test]
    fn format_returns_none_on_bad_length() {
        assert_eq!(format_rg("12", State::SP), None);
        assert_eq!(format_rg("12", State::RJ), None);
    }

    #[test]
    fn remove_symbols_sp_keeps_x() {
        assert_eq!(remove_symbols("60.000.000-X", State::SP), "60000000X");
        assert_eq!(remove_symbols("60.000.000-x", State::SP), "60000000X");
    }

    #[test]
    fn remove_symbols_other_uf_drops_letters() {
        assert_eq!(remove_symbols("12.345-67", State::RJ), "1234567");
        assert_eq!(remove_symbols("X1234567", State::RJ), "1234567");
    }

    #[test]
    fn compute_check_digit_sp() {
        assert_eq!(compute_check_digit("12345678", State::SP), Some(2));
        assert_eq!(compute_check_digit("44444444", State::SP), Some(0));
        assert_eq!(compute_check_digit("60000000", State::SP), Some(10));
    }

    #[test]
    fn compute_check_digit_rejects_other_uf() {
        assert_eq!(compute_check_digit("1234567", State::RJ), None);
    }

    #[test]
    fn compute_check_digit_rejects_bad_length() {
        assert_eq!(compute_check_digit("1234567", State::SP), None);
        assert_eq!(compute_check_digit("123456789", State::SP), None);
    }

    #[test]
    fn generate_produces_valid() {
        for _ in 0..100 {
            let rg = generate(State::SP).unwrap();
            assert!(is_valid(rg.as_str(), State::SP));
            let parsed = parse_strict(rg.as_str(), State::SP).unwrap();
            assert_eq!(parsed, rg);
        }
    }

    #[test]
    fn generate_format_roundtrip() {
        for _ in 0..100 {
            let rg = generate(State::SP).unwrap();
            let formatted = rg.formatted();
            let parsed = parse_strict(&formatted, State::SP).unwrap();
            assert_eq!(parsed, rg);
        }
    }

    #[test]
    fn generate_ok_others_err() {
        assert!(generate(State::SP).is_ok());
        assert_eq!(
            generate(State::RJ),
            Err(RgError::UnsupportedUfForGeneration)
        );
    }

    #[test]
    fn masked_sp_real_rgs() {
        let rg = parse_strict("294653272", State::SP).unwrap();
        assert_eq!(rg.masked(), "29.***.***-*");
        let rg = parse_strict("60000000X", State::SP).unwrap();
        assert_eq!(rg.masked(), "60.***.***-*");
    }

    #[test]
    fn masked_other_uf() {
        let rg = parse_strict("1234567", State::RJ).unwrap();
        assert_eq!(rg.masked(), "12*****");
    }

    #[test]
    fn masked_generated_sp() {
        let rg = generate(State::SP).unwrap();
        let m = rg.masked();
        assert!(m.starts_with(&rg.as_str()[..2]));
        assert_eq!(m, alloc::format!("{}.***.***-*", &rg.as_str()[..2]));
    }

    #[test]
    fn body_sp_real_rgs() {
        let rg = parse_strict("294653272", State::SP).unwrap();
        assert_eq!(rg.body(), "29465327");
        let rg = parse_strict("60000000X", State::SP).unwrap();
        assert_eq!(rg.body(), "60000000");
    }

    #[test]
    fn body_other_uf_returns_full() {
        let rg = parse_strict("1234567", State::RJ).unwrap();
        assert_eq!(rg.body(), "1234567");
    }

    #[test]
    fn rg_is_copy() {
        let rg = generate(State::SP).unwrap();
        let copy = rg;
        assert_eq!(rg, copy);
    }

    #[test]
    fn rg_as_ref_str() {
        let rg = generate(State::SP).unwrap();
        let r: &str = rg.as_ref();
        assert_eq!(r, rg.as_str());
    }

    #[test]
    fn debug_format_includes_uf() {
        let rg = parse_strict("294653272", State::SP).unwrap();
        let dbg = alloc::format!("{rg:?}");
        assert!(dbg.starts_with("Rg(SP, "));
        assert!(dbg.ends_with(')'));
    }

    #[test]
    fn display_uses_formatted() {
        let rg = parse_strict("294653272", State::SP).unwrap();
        assert_eq!(rg.to_string(), "29.465.327-2");
    }

    #[test]
    fn other_uf_display_passes_through() {
        let rg = parse_strict("1234567", State::RJ).unwrap();
        assert_eq!(rg.to_string(), "1234567");
    }
}