Function lka_encode

Source
pub fn lka_encode(
    bytes: impl AsRef<[u8]>,
    options: &str,
    scope: &dyn LkEnv,
) -> String
Expand description

encode bytes as an abe that evaluate back to bytes.

accepts a list of func to try and encode. This function can also be used as the evaluator ‘[/?:..]’.

let bytes= lka_eval("[u32:8]",())?;
assert_eq!(bytes,&[0,0,0,8]);
assert_eq!(lka_encode(&bytes,"/:", &()), r#"\0\0\0\x08"#);
assert_eq!(lka_encode(&bytes,"u32", &()), "[u32:8]");


// the options are a list of '/' separated functions
// In this example 'u32' won't fit, LNS '#' lookup will succeed, if not the encoding would be base64

let public_grp = PUBLIC;
assert_eq!(lka_encode(&*public_grp,"u32/#/b", &()), "[#:pub]");

// We can get meta - encode is also available as a scope during lka_eval

// As the '?' function - with the tail argument being a single reverse option
assert_eq!(lka_eval(r#"[?:\0\0\0[u8:8]:u32]"#,())?,b"[u32:8]");
assert_eq!(lka_eval(r#"[:\0\0\0[u8:8]/?:u32]"#,())?,b"[u32:8]");

// Or as the '?' macro
assert_eq!(lka_eval(r#"[/?:\0\0\0[u8:8]/u32/#/b]"#,())?,b"[u32:8]");

encode doesn’t error, it falls back on plain abtxt. this variant also swallows bad options, see [lk_try_encode] to avoid doing so. *