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
sourceimpl<'a> Evaluator<'a>
impl<'a> Evaluator<'a>
sourcepub fn new(value_context: &'a DataValueMap<&str>) -> Self
pub fn new(value_context: &'a DataValueMap<&str>) -> Self
Create from script and a value context
sourcepub fn format_script(&self, script: &str) -> EvalResult<String>
pub fn format_script(&self, script: &str) -> EvalResult<String>
Get format applied value-context to the script.
If you want to know syntax, you will see Evaluator’s document.
sourcepub fn eval_int(&self, script: &str) -> EvalResult<SbrdInt>
pub fn eval_int(&self, script: &str) -> EvalResult<SbrdInt>
Evaluate the script applied the context, as SbrdInt
sourcepub fn eval_real(&self, script: &str) -> EvalResult<SbrdReal>
pub fn eval_real(&self, script: &str) -> EvalResult<SbrdReal>
Evaluate the script applied the context, as SbrdReal
sourcepub fn eval_bool(&self, script: &str) -> EvalResult<SbrdBool>
pub fn eval_bool(&self, script: &str) -> EvalResult<SbrdBool>
Evaluate the script applied the context, as SbrdBool
sourcepub fn eval_string(&self, script: &str) -> EvalResult<SbrdString>
pub fn eval_string(&self, script: &str) -> EvalResult<SbrdString>
Evaluate the script applied the context, as SbrdString
Trait Implementations
impl<'a> StructuralPartialEq for Evaluator<'a>
Auto Trait Implementations
impl<'a> RefUnwindSafe for Evaluator<'a>
impl<'a> Send for Evaluator<'a>
impl<'a> Sync for Evaluator<'a>
impl<'a> Unpin for Evaluator<'a>
impl<'a> UnwindSafe for Evaluator<'a>
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcefn clone_into(&self, target: &mut T)
fn clone_into(&self, target: &mut T)
toowned_clone_into)Uses borrowed data to replace owned data, usually by cloning. Read more