Struct regorust::Input

source ·
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

source

pub fn new() -> Self

source

pub fn int(self, value: i64) -> Self

Push an integer onto the stack.

source

pub fn float(self, value: f64) -> Self

Push a float onto the stack.

source

pub fn str(self, value: &str) -> Self

Push a string onto the stack.

source

pub fn bool(self, value: bool) -> Self

Push a boolean onto the stack.

source

pub fn null(self) -> Self

Push a null onto the stack.

source

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());
source

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.

source

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());
source

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());
source

pub fn validate(self) -> Result<Self, &'static str>

Trait Implementations§

source§

impl Debug for Input

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Drop for Input

source§

fn drop(&mut self)

Executes the destructor for this type. Read more
source§

impl PartialEq for Input

source§

fn eq(&self, other: &Self) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.

Auto Trait Implementations§

§

impl Freeze for Input

§

impl RefUnwindSafe for Input

§

impl !Send for Input

§

impl !Sync for Input

§

impl Unpin for Input

§

impl UnwindSafe for Input

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

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

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.