//! `hex::` — hexadecimal encode/decode (lowercase output, case-
//! insensitive input). String and Blob inputs are both accepted on
//! encode; decode always returns `Blob`.

use rhai::{Blob, Engine as RhaiEngine, EvalAltResult, Module};

pub fn register(engine: &mut RhaiEngine) {
    let mut module = Module::new();

    module.set_native_fn("encode", |s: &str| -> Result<String, Box<EvalAltResult>> {
        Ok(hex::encode(s.as_bytes()))
    });
    module.set_native_fn("encode", |b: Blob| -> Result<String, Box<EvalAltResult>> {
        Ok(hex::encode(&b))
    });
    module.set_native_fn("decode", |s: &str| -> Result<Blob, Box<EvalAltResult>> {
        hex::decode(s).map_err(|e| format!("hex::decode: {e}").into())
    });

    engine.register_static_module("hex", module.into());
}
