Skip to main content

Crate dni

Crate dni 

Source
Expand description

§dni — validate Spanish DNI and NIE numbers

Validate Spanish identification numbers: the DNI (Documento Nacional de Identidad, for citizens) and the NIE (Número de Identidad de Extranjero, for foreigners). A faithful Rust port of the algorithms used by python-stdnum.

use dni::{is_valid, calc_check_digit};

// DNI: 8 digits + a check letter.
assert!(is_valid("12345678Z"));
assert!(!is_valid("12345678A"));
assert_eq!(calc_check_digit("12345678").unwrap(), 'Z');

// NIE: X/Y/Z + 7 digits + a check letter.
assert!(dni::nie::is_valid("X1234567L"));

Surrounding whitespace and -/space separators are accepted, and input is upper-cased.

Zero dependencies and #![no_std].

Modules§

nie
NIE (Número de Identidad de Extranjero) — Spanish foreigner numbers.

Enums§

Error
Why a number is not valid.

Functions§

calc_check_digit
Calculate the DNI check letter for the eight-digit number (without the check letter).
compact
Strip -/space separators and surrounding whitespace, and upper-case.
is_valid
Whether number is a valid DNI.
validate
Validate a DNI, returning the compacted number on success.