Hermes

A very convenient string parsing crate that can parse function and variable references in strings into corresponding values.

String Syntax Example

rust let input = "${hostname()}+\"dd\"+true+${name}+${invalid}+${random_str(${len})}+${multiply(1,-2.5,3)}";

The string can contain function calls, variable references, booleans, numbers, and strings, connected by "+".

How to use

You need to initialize a Cache struct to store all your variables and functions. Then, you can call parse or parse_to_string function to parse the a string.

See example below: ```rust use hermes::{cache::{ ExpType, Cache, CacheVariable }, error::Error};

fn main() { // new a cache with 10 size capacity let mut cache = Cache::new(10); // add two variables to cache cache.addvariable(CacheVariable::new("name".tostring(), ExpType::STRING("liudao".tostring()))); cache.addvariable(CacheVariable::new("len".to_string(), ExpType::INT(10)));

// add a custom function to cache
cache.add_function("multiply", Box::new(|params: Vec<ExpType>| -> Result<ExpType, Error>{
    Ok(params.into_iter().fold(ExpType::FLOAT(1.0f32), |acc, item| {
        let acc_num = if let ExpType::FLOAT(float) = acc {
            float
        } else {
            0.0f32
        };
        match item {
            ExpType::INT(i) => ExpType::FLOAT(acc_num * i as f32),
            ExpType::FLOAT(f) => ExpType::FLOAT(acc_num * f),
            _ => acc,
        }
    }))

}));

// define a complex str
let input = "${hostname()}+\"dd\"+true+${name}+${invalid}+${random_str(${len})}+${multiply(1,-2.5,3)}}";

// parse the str
println!("parse result is: {}", hermes::lexer::parse_to_string(input, &mut cache));

} ```

Output: bash parse result is: MacBook-Pro.localddtrueliudaojdTXX8f16d-7.5

Inner Function