Function linkspace::abe::lk_encode

source ·
pub fn lk_encode(bytes: impl AsRef<[u8]>, options: &str) -> 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= lk_eval("[u32:8]",())?;
assert_eq!(bytes,&[0,0,0,8]);
assert_eq!(lk_encode(&bytes,"/:"), r#"\0\0\0\x08"#);
assert_eq!(lk_encode(&bytes,"u32"), "[u32:8]");


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

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

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

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

// Or as the '?' macro
assert_eq!(lk_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. *