DammTable

Struct DammTable 

Source
pub struct DammTable { /* private fields */ }
Expand description

Damm quasi-group table for check digit calculation.

The table has the following properties:

  • Each row is a permutation of 0..size
  • Each column is a permutation of 0..size
  • Diagonal is all zeros: table[i][i] = 0
  • Anti-symmetric: table[a][b] != table[b][a] when a != b

Implementations§

Source§

impl DammTable

Source

pub fn new(size: usize) -> Self

Generate a quasi-group table for the given alphabet size.

The generated table guarantees:

  • 100% detection of single-character errors
  • 100% detection of adjacent transposition errors
§Arguments
  • size - The alphabet size (10-62)
§Examples
use promocrypt_core::DammTable;

let table = DammTable::new(26);
assert_eq!(table.size(), 26);
Source

pub fn size(&self) -> usize

Get the size of the table (alphabet size).

Source

pub fn get(&self, row: usize, col: usize) -> u8

Get table value at position.

Source

pub fn calculate(&self, chars: &[char], alphabet: &Alphabet) -> char

Calculate check digit for a sequence of characters (check digit at end).

§Arguments
  • chars - The characters to calculate check digit for (without existing check digit)
  • alphabet - The alphabet used for character→index mapping
§Returns

The check digit character from the alphabet.

Source

pub fn calculate_for_start(&self, chars: &[char], alphabet: &Alphabet) -> char

Calculate check digit for start position.

Finds a check digit X such that processing [X, chars…] results in 0.

§Arguments
  • chars - The characters (check digit will be prepended)
  • alphabet - The alphabet used for character→index mapping
§Returns

The check digit character from the alphabet.

Source

pub fn calculate_for_position( &self, chars: &[char], alphabet: &Alphabet, position: usize, ) -> char

Calculate check digit for arbitrary position in the code.

Finds a check digit X such that inserting X at position in chars results in a valid code (Damm validation returns 0).

§Arguments
  • chars - The characters (check digit will be inserted at position)
  • alphabet - The alphabet used for character→index mapping
  • position - Position where check digit will be inserted (0-based)
§Returns

The check digit character from the alphabet.

Source

pub fn calculate_str(&self, s: &str, alphabet: &Alphabet) -> char

Calculate check digit for a string.

Source

pub fn validate(&self, code: &str, alphabet: &Alphabet) -> bool

Validate a code including its check digit.

The check digit is valid if processing the entire code (including check digit) results in 0.

§Arguments
  • code - The full code including check digit
  • alphabet - The alphabet used
§Returns

true if the check digit is valid, false otherwise.

Source

pub fn to_bytes(&self) -> Vec<u8>

Serialize the table to bytes for storage.

Source

pub fn from_bytes(bytes: &[u8]) -> Result<Self>

Deserialize table from bytes.

Source

pub fn to_base64(&self) -> String

Serialize to base64 string for JSON storage.

Source

pub fn from_base64(s: &str) -> Result<Self>

Deserialize from base64 string.

Trait Implementations§

Source§

impl Clone for DammTable

Source§

fn clone(&self) -> DammTable

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for DammTable

Source§

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

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

impl<'de> Deserialize<'de> for DammTable

Source§

fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl PartialEq for DammTable

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Serialize for DammTable

Custom serialization for DammTable using base64

Source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl Eq for DammTable

Source§

impl StructuralPartialEq for DammTable

Auto Trait Implementations§

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> Same for T

Source§

type Output = T

Should always be Self
Source§

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

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,