pub struct Evaluator<'a> { /* private fields */ }
Expand description

Evaluator for script and format. Script and format is processed by replacing a replace-key-syntax for the key with value based on each entry (key, value) of context. Replace-key-syntax is “{key}” and “{key:<format-option>}”. It specified by Rust format syntax with the key as name position. But not support index position, variable, padding with character and Pointer format ({:p}). Debug format is not supported in release build. If you want to know, you will see Rust-format syntax and DataValue::format.

All values, variables and functions are available as described in the evalexpr except the regex functions. If you’ll know syntax and available them more, you can see this document.

Examples

fn main(){
    use sbrd_gen::eval::Evaluator;
    use sbrd_gen::value::{DataValue, DataValueMap};

    let mut value_context = DataValueMap::new();
    value_context.insert("Key-Int", DataValue::Int(12));
    value_context.insert("キー Real", DataValue::Real(12.345));
    value_context.insert("Key:String", DataValue::String("aiueoあいうえお".to_string()));
    value_context.insert("Key Bool:", DataValue::Bool(true));
    value_context.insert("key Null ", DataValue::Null);
    let evaluator = Evaluator::new(&value_context);

    assert_eq!(Ok("no key".to_string()), evaluator.format_script("no key"));
    assert_eq!(Ok("12".to_string()), evaluator.format_script("{Key-Int}"));
    assert_eq!(Ok("{Key-Int}".to_string()), evaluator.format_script("{{Key-Int}}"));
    assert_eq!(Ok("Rate= +12.35".to_string()), evaluator.format_script("Rate={キー Real:+7.2}"));
    assert_eq!(Ok("Rate=+012.35".to_string()), evaluator.format_script("Rate={キー Real:+07.2}"));
    assert_eq!(Ok(" aiueoあいうえお ".to_string()), evaluator.format_script("{Key:String:^12}"));
    assert_eq!(Ok("true    ".to_string()), evaluator.format_script("{Key Bool::<8}"));
    assert_eq!(Ok("null".to_string()), evaluator.format_script("{key Null :<10}"));
}

Implementations

Create from script and a value context

Get format applied value-context to the script.

If you want to know syntax, you will see Evaluator’s document.

Evaluate the script applied the context, as SbrdInt

Evaluate the script applied the context, as SbrdReal

Evaluate the script applied the context, as SbrdBool

Evaluate the script applied the context, as SbrdString

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.