Calculator

Trait Calculator 

Source
pub trait Calculator<T>
where T: Display + PartialEq,
{ // Required methods fn name(&self) -> &'static str; fn calculate(&self, s: &str) -> Result<T, CheckDigitError>; // Provided methods fn number_of_check_digit_chars(&self) -> usize { ... } fn create(&self, s: &str) -> Result<String, CheckDigitError> { ... } fn validate<S>(&self, s: S) -> Result<(), CheckDigitError> where Self: Sized, S: AsRef<str> { ... } fn is_valid<S>(&self, s: S) -> bool where Self: Sized, S: AsRef<str> { ... } }
Expand description

Trait for types that implement check digit algorithms.

Required Methods§

Source

fn name(&self) -> &'static str

Return the name of this algorithm.

Source

fn calculate(&self, s: &str) -> Result<T, CheckDigitError>

Calculate a check digit for the provided string.

Provided Methods§

Source

fn number_of_check_digit_chars(&self) -> usize

Return the number of characters used as the check digit. Currently it is assumed that these are the n right-most characters in the input string.

Source

fn create(&self, s: &str) -> Result<String, CheckDigitError>

Create a new string with the original data plus check digit.

Source

fn validate<S>(&self, s: S) -> Result<(), CheckDigitError>
where Self: Sized, S: AsRef<str>,

Validate that the string is valid and that it contains a valid check digit.

Source

fn is_valid<S>(&self, s: S) -> bool
where Self: Sized, S: AsRef<str>,

Returns true if the provided string includes a valid check digit, else false. The default implementation relies on the validate method.

Implementors§

Source§

impl Calculator<u8> for codes_check_digits::gs1::CheckDigitAlgorithm

Source§

impl Calculator<u8> for codes_check_digits::iso_7064::CheckDigitAlgorithm

Source§

impl Calculator<u8> for codes_check_digits::luhn::CheckDigitAlgorithm

Source§

impl Calculator<u8> for codes_check_digits::sedol::CheckDigitAlgorithm