Skip to main content

substitute

Function substitute 

Source
pub fn substitute(tree: &Value, cond: &Condition) -> Result<Value>
Expand description

Replace the variables of the condition in a JSON tree.

  • a string that is exactly $name or ${name} becomes the value of name, with its type (number, string or boolean);
  • ${name} inside a longer string is replaced by the text of the value;
  • $$ escapes a dollar sign.

§Errors

Error::Config for a variable that the condition does not define.

§Examples

use comnum_sim::grid::{Condition, Value};
use serde_json::json;
let mut cond = Condition::new();
cond.set("a", Value::I64(4));
cond.set("b", Value::Str("QAM".into()));
let v = comnum_spec::substitute(&json!({"m": "$a", "name": "${a}_${b}", "cost": "$$5"}), &cond)?;
assert_eq!(v, json!({"m": 4, "name": "4_QAM", "cost": "$5"}));