# luhncalc
[![crates.io version][1]][2] [![CI][3]][4]
[![downloads][5]][6] [![docs.rs docs][7]][8]
`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`:
```bash
> luhncalc 1234567890
Sequence: 1234567890
Valid sequence: false
Next digit: 3
```
Validate the following valid digit sequence `12345678903`:
```bash
> luhncalc 12345678903
Sequence: 12345678903
Valid sequence: true
Next digit: 1
```
# Library usage
Add the crate to your project:
```toml
[dependencies]
luhncalc = "1"
```
Use one of the public functions from Rust code:
```rust
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
- [the Apache License, Version 2.0](./LICENSE-APACHE)
- [the MIT license](./LICENSE-MIT)
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.
[1]: https://img.shields.io/crates/v/luhncalc.svg?style=flat-square
[2]: https://crates.io/crates/luhncalc
[3]: https://img.shields.io/github/actions/workflow/status/Poremski/luhncalc/ci.yml?branch=main&style=flat-square
[4]: https://github.com/Poremski/luhncalc/actions/workflows/ci.yml
[5]: https://img.shields.io/crates/d/luhncalc.svg?style=flat-square
[6]: https://crates.io/crates/luhncalc
[7]: https://img.shields.io/badge/docs-latest-blue.svg?style=flat-square
[8]: https://docs.rs/luhncalc
[releases]: https://github.com/Poremski/luhncalc/releases