Skip to main content

Valor

Struct Valor 

Source
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

Source

pub const MIN_LENGTH: usize = 1

The minimum number of digits in a VALOR.

Source

pub const MAX_LENGTH: usize = 9

The maximum number of digits in a VALOR.

Source

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
§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' }),
);
Source

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());
Source

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");
Source

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");
Source

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");
Source

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);
Source

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());
Source

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);
Source

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);

Trait Implementations§

Source§

impl AsRef<str> for Valor

Source§

fn as_ref(&self) -> &str

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl Clone for Valor

Source§

fn clone(&self) -> Valor

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for Valor

Source§

impl Debug for Valor

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for Valor

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for Valor

Source§

impl FromStr for Valor

Source§

type Err = ValidationError

The associated error which can be returned from parsing.
Source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
Source§

impl Hash for Valor

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for Valor

Source§

fn eq(&self, other: &Valor) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Valor

Auto Trait Implementations§

§

impl Freeze for Valor

§

impl RefUnwindSafe for Valor

§

impl Send for Valor

§

impl Sync for Valor

§

impl Unpin for Valor

§

impl UnsafeUnpin for Valor

§

impl UnwindSafe for Valor

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.