pub struct Input { /* private fields */ }Expand description
The Input interface allows the creation of inputs to a policy without
requiring serialization to JSON. The interface is that of a stack,
in which values are pushed and then various operations are used to turn
terminal types into more complex ones like objects and arrays. When used,
the Input will provide the top of the stack to any downstream consumer (such as
Interpreter::set_input()).
§Examples
§Object
let input = Input::new().str("a").int(10).objectitem()
.str("b").str("20").objectitem()
.str("c").float(30.0).objectitem()
.str("d").bool(true).objectitem()
.object(4).validate()
.expect("Unable to create input");
let rego = Interpreter::new();
rego.set_input(&input).expect("Unable to set input");
let result = rego.query("x=input.a").expect("Failed query");
let x = result.binding("x").expect("cannot get x");
println!("x = {}", x.json().unwrap());§Array
let input = Input::new().int(1).int(2).int(3).int(4).array(4)
.validate().expect("Unable to create input");
let rego = Interpreter::new();
rego.set_input(&input).expect("Unable to set input");
let result = rego.query("x=input").expect("Failed query");
let x = result.binding("x").expect("cannot get x");
println!("x = {}", x.json().unwrap());Implementations§
source§impl Input
impl Input
pub fn new() -> Self
sourcepub fn objectitem(self) -> Self
pub fn objectitem(self) -> Self
Take the top two values on the stack and turn them into an object item. The penultimate value on the stack will be used as the key, and the top of the stack will be the value for that key. Objects are constructed from object items.
§Example
let input = Input::new().str("a").int(10).objectitem().object(1)
.validate().expect("Unable to create input");
let rego = Interpreter::new();
rego.set_input(&input).expect("Unable to set input");
let result = rego.query("x=input").expect("Failed query");
let x = result.binding("x").expect("cannot get x");
println!("x = {}", x.json().unwrap());sourcepub fn object(self, size: regoSize) -> Self
pub fn object(self, size: regoSize) -> Self
Take the top size values on the stack and turn them into an object.
Note that all of these values must be object items in order for this to be valid.
sourcepub fn array(self, size: regoSize) -> Self
pub fn array(self, size: regoSize) -> Self
Take the top size values on the stack and turn them into an array.
Stack order will be used.
§Example
let input = Input::new().int(1).int(2).int(3).int(4).array(4)
.validate().expect("Unable to create input");
let rego = Interpreter::new();
rego.set_input(&input).expect("Unable to set input");
let result = rego.query("x=input").expect("Failed query");
let x = result.binding("x").expect("cannot get x");
println!("x = {}", x.json().unwrap());sourcepub fn set(self, size: regoSize) -> Self
pub fn set(self, size: regoSize) -> Self
Take the top size values on the stack and turn them into a set.
Identical items will be de-duplicated.
§Example
let input = Input::new().int(1).int(2).int(2).int(3).set(4)
.validate().expect("Unable to create input");
let rego = Interpreter::new();
rego.set_input(&input).expect("Unable to set input");
let result = rego.query("x=input").expect("Failed query");
let x = result.binding("x").expect("cannot get x");
println!("x = {}", x.json().unwrap());