Skip to main content

Module rg

Module rg 

Source
Expand description

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.

Structs§

Rg
A validated RG stored as ASCII bytes (digits, plus optional trailing 'X' for SP), tagged with its issuing UF.

Enums§

RgError

Functions§

compute_check_digit
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.
format_rg
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).
generate
Generate a random valid RG. SP only; other UFs return RgError::UnsupportedUfForGeneration.
is_valid
Lenient validation - strips symbols, then checks length and (for SP) the check digit.
is_valid_strict
Strict validation - input must be either the canonical formatted mask or the unformatted body. No leading/trailing whitespace, no extra symbols.
parse_strict
Parse a raw RG string into a validated Rg.
remove_symbols
Strip dots, dashes, slashes and whitespace. For SP, preserve a trailing 'X' (case-insensitive, normalized to uppercase). For other UFs, drop non-digits.