Functional Weave
Code in Rust

validation.gs1-check-digit@1.0.0

impl/rust/is_gs1_check_digit_valid.rs

841 bytes · the Rust implementation · view raw

Imports name this capability’s declared dependencies, which fune builds next to it in your project; each one links to its page.

use super::funejson::Value;  ← the fune runtime: the JSON value the test vectors use; fune build keeps it only where a signature takes one
use super::validation_gs1_check_digit_gs1_check_digit::gs1_check_digit;  ← gs1CheckDigit, another function of this group · built into the same file, even by a slim install

/// Whether the last digit of `key` is the GS1 check digit of the digits before
/// it. Only the arithmetic: the length, the prefix and an all-zero key are the
/// caller's to judge. Validators answer rather than panic, so anything that is
/// not two or more ASCII digits is false.
pub fn is_gs1_check_digit_valid(key: &str) -> bool {
    if key.len() < 2 || !key.bytes().all(|b| b.is_ascii_digit()) {
        return false;
    }
    let last = (key.as_bytes()[key.len() - 1] - b'0') as i64;
    gs1_check_digit(&key[..key.len() - 1]) == last
}

pub fn fune_vector(args: &[Value]) -> Value {
    // A non-string argument arrives as an empty string: false, as in the other two.
    Value::Bool(is_gs1_check_digit_valid(args[0].as_str()))
}