luhncalc 1.1.0

Validate digit sequences with the Luhn algorithm and calculate the next check digit.
Documentation

luhncalc

crates.io version CI downloads docs.rs docs

luhncalc is a small crate that works both as a command-line utility and as a Rust library for validating digit sequences with the Luhn algorithm and calculating the next check digit.

Command line usage

Validate the following invalid digit sequence 1234567890:

> luhncalc 1234567890

Sequence:       1234567890
Valid sequence: false
Next digit:     3

Validate the following valid digit sequence 12345678903:

> luhncalc 12345678903

Sequence:       12345678903
Valid sequence: true
Next digit:     1

Library usage

Add the crate to your project:

[dependencies]
luhncalc = "1"

Use one of the public functions from Rust code:

use luhncalc::{analyze, calculate_check_digit, validate};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    assert_eq!(validate("12345678903")?, true);
    assert_eq!(calculate_check_digit("1234567890")?, '3');

    let analysis = analyze("12345678903")?;
    assert_eq!(analysis.valid, true);
    assert_eq!(analysis.next_check_digit, '1');

    Ok(())
}

The library returns an error when the digit sequence is empty or contains anything other than ASCII digits 0-9.

License

This project is licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-20 license, shall be dual licensed as above, without any additional terms or conditions.