Function linkspace::abe::lk_eval

source ·
pub fn lk_eval<'o>(
    expr: &str,
    udata: impl Into<UserData<'o>>
) -> LkResult<Vec<u8>>
Expand description

Evaluate an expression and return the bytes

Print a list of active scopes with help by using lk_eval("[help]")

Optionally add a pkt in the scope. See lk_tokenize_abe for different delimiter behavior See lk_eval_loose that is less strict on its input

assert_eq!( b"abc" as &[u8]    , lk_eval( "abc" ,())?, );
assert_eq!( &[0u8,1,255] as &[u8], lk_eval( r#"\0\x01\xff"# ,())?,);

// calling functions such as 'u8'
assert_eq!( b"abc" as &[u8]    , lk_eval( "ab[u8:99]" ,())?, );

assert_eq!(
           b"The '' function returns its first argument" as &[u8],
   lk_eval( "The '' function returns its first [:argument]", ())?
);

assert_eq!(
           b"Bytes are joined" as &[u8],
   lk_eval( "Bytes[: are][: joined]" , ())?
);

let result = lk_eval( "Nest expressions [u8:65] == [u8:[:65]] == \x41" , ())?;
assert_eq!(result,   b"Nest expressions A == A == A");

let result = lk_eval( "Use result as first argument with '/' [u8:65] == [:65/u8] == \x41" , ())?;
assert_eq!(result,   b"Use result as first argument with '/' A == A == A");


let result = lk_eval( "You can provide an argv [0] [1]" , &[b"like" as &[u8], b"this"])?;
assert_eq!(result,   b"You can provide an argv like this");

let lp : NetPktBox = lk_linkpoint(&[],ab(b"mydomain"),PUBLIC,RootedSpace::empty(),&[],None)?;
let pkt: &dyn NetPkt = &lp;

assert_eq!( lk_eval( "[hash]" , pkt)?,&*pkt.hash());
let by_arg   = lk_eval( "[hash:str]", pkt)?;
let by_apply = lk_eval( "[hash/?b]",  pkt)?;
let as_field = pkt.hash().b64().into_bytes();
assert_eq!( by_arg, by_apply);
assert_eq!( by_arg, as_field);

// or provide both at once with (pkt,&[b"argv"])
// More options are available in [varscope]

// escaped characters
assert_eq!( lk_eval( r#"\n\t\:\/\\\[\]"# ,())?,  &[b'\n',b'\t',b':',b'/',b'\\',b'[',b']'] );