pub struct Valor { /* private fields */ }Expand description
A validated Valorennummer (SIX Financial Information).
A Valor can only be created by Valor::parse (or the explicitly
unchecked Valor::from_bytes_unchecked), so a value of this type is a
proof that it holds between 1 and 9 ASCII decimal digits. It stores the
identifier inline as [u8; 9] plus a length, is Copy, and allocates
nothing.
Because the VALOR is variable-length, the unused tail bytes of the array
are always kept zeroed, so the derived PartialEq, Eq, and Hash are
correct: two Valor values compare equal exactly when their digits do.
§Examples
use regit_identifiers::Valor;
let valor = Valor::parse("1213853").unwrap();
assert_eq!(valor.as_str(), "1213853");
assert_eq!(valor.len(), 7);
assert_eq!(valor.as_u64(), 1_213_853);Implementations§
Source§impl Valor
impl Valor
Sourcepub const MIN_LENGTH: usize = 1
pub const MIN_LENGTH: usize = 1
The minimum number of digits in a VALOR.
Sourcepub const MAX_LENGTH: usize = 9
pub const MAX_LENGTH: usize = 9
The maximum number of digits in a VALOR.
Sourcepub fn parse(s: &str) -> Result<Self, ValidationError>
pub fn parse(s: &str) -> Result<Self, ValidationError>
Parses and fully validates a VALOR.
Validation is strict and, in order: the input must be 1 to 9 characters; every character must be an ASCII decimal digit. There is no check digit.
§Errors
ValidationError::Structurewith rule"VALOR must be 1 to 9 digits"if the input is empty or longer than nine characters.ValidationError::InvalidCharacterif a character is not an ASCII digit (this also rejects any non-ASCII character).
§Examples
use regit_identifiers::Valor;
use regit_identifiers::errors::ValidationError;
assert!(Valor::parse("1213853").is_ok());
// A non-digit character is rejected, not silently accepted.
assert_eq!(
Valor::parse("1213853A"),
Err(ValidationError::InvalidCharacter { position: 8, found: 'A' }),
);Sourcepub fn validate(s: &str) -> Result<(), ValidationError>
pub fn validate(s: &str) -> Result<(), ValidationError>
Validates a VALOR without constructing one.
Equivalent to Valor::parse(s).map(|_| ()); use it when only the
verdict is needed.
§Errors
Returns the same ValidationError variants as Valor::parse.
§Examples
use regit_identifiers::Valor;
assert!(Valor::validate("908440").is_ok());
assert!(Valor::validate("1234567890").is_err());Sourcepub const fn from_bytes_unchecked(bytes: [u8; 9], len: u8) -> Self
pub const fn from_bytes_unchecked(bytes: [u8; 9], len: u8) -> Self
Wraps raw bytes as a Valor without any validation.
The caller asserts that the first len bytes of bytes are ASCII
decimal digits, that len is in 1..=9, and that the remaining tail
bytes are zero. This exists for reconstructing a Valor from bytes
that were validated earlier; prefer Valor::parse for any untrusted
input.
§Examples
use regit_identifiers::Valor;
let valor = Valor::from_bytes_unchecked(*b"908440\0\0\0", 6);
assert_eq!(valor.as_str(), "908440");Sourcepub fn as_str(&self) -> &str
pub fn as_str(&self) -> &str
Returns the VALOR as a string slice.
§Examples
use regit_identifiers::Valor;
assert_eq!(Valor::parse("24476758").unwrap().as_str(), "24476758");Sourcepub fn as_bytes(&self) -> &[u8]
pub fn as_bytes(&self) -> &[u8]
Returns the VALOR as its raw ASCII digit bytes.
The slice has Valor::len elements; the zeroed tail of the backing
array is not included.
§Examples
use regit_identifiers::Valor;
assert_eq!(Valor::parse("908440").unwrap().as_bytes(), b"908440");Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of digits in the VALOR, always in 1..=9.
§Examples
use regit_identifiers::Valor;
assert_eq!(Valor::parse("1213853").unwrap().len(), 7);Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Always false — a valid VALOR is never empty (it has 1 to 9
digits). Provided for API consistency with Valor::len.
§Examples
use regit_identifiers::Valor;
assert!(!Valor::parse("1213853").unwrap().is_empty());Sourcepub fn as_u64(&self) -> u64
pub fn as_u64(&self) -> u64
Returns the VALOR’s numeric value.
A VALOR is at most nine decimal digits, so its value always fits in a
u64 (and indeed in a u32); leading zeros are absorbed.
§Examples
use regit_identifiers::Valor;
assert_eq!(Valor::parse("1213853").unwrap().as_u64(), 1_213_853);
assert_eq!(Valor::parse("000123").unwrap().as_u64(), 123);Sourcepub fn as_u32(&self) -> u32
pub fn as_u32(&self) -> u32
Returns the VALOR’s numeric value as a u32.
A nine-digit decimal fits comfortably in a u32 (the maximum
999_999_999 is well under 2^32 ≈ 4.29 × 10^9); this is the
narrower companion of Valor::as_u64.
§Examples
use regit_identifiers::Valor;
assert_eq!(Valor::parse("1213853").unwrap().as_u32(), 1_213_853);
assert_eq!(Valor::parse("999999999").unwrap().as_u32(), 999_999_999);