Functional Weave
Code in Rust

health.nhs-number-format@1.0.0

impl/rust.rs

1,058 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_nhs_number::validate_nhs_number;  ← from validation.nhs-number ^2.0.0 · built alongside by fune

/// The NHS number in the 3-3-4 display format NHS systems use, after
/// a full check with validation.nhs-number. An invalid number is an error, not
/// a formatted string: a well-spaced wrong number looks more trustworthy than
/// a badly spaced right one. The error names the reason but never echoes the
/// number, which is a patient identifier and has no place in a log.
///
/// # Panics
/// Panics when the number is not a valid NHS number.
pub fn format_nhs_number(value: &str) -> String {
    let result = validate_nhs_number(value);
    match (result.valid, result.formatted) {
        (true, Some(formatted)) => formatted,
        _ => panic!("not a valid NHS number ({})", result.reason.unwrap_or_default()),
    }
}

pub fn fune_vector(args: &[Value]) -> Value {
    // A non-string arrives as an empty string, which validation.nhs-number
    // reports as empty: the answer TypeScript and Python give too.
    Value::str(&format_nhs_number(args[0].as_str()))
}