[][src]Struct playbook_api::Context

pub struct Context { /* fields omitted */ }

A data structure designed to provide a high-level language agnostic calling convention.

Methods

impl Context[src]

pub fn new() -> Context[src]

Create an empty context.

pub fn overlay(&self, another: &Context) -> Context[src]

Overlay another on top of this context.

Example

let eg = Context::from("a: 1\nb: 0");
let another = Context::from("b: 1");
assert_eq!(eg.overlay(&another), Context::from("a: 1\nb: 1"));

pub fn render(&self, values: &Context) -> Context[src]

Use as a template and render it with other values

Example

let template = Context::from("a: 1\nb: 0");
let values = Context::from("b: 1\nc: 2");
assert_eq!(template.render(&values), Context::from("a: 1\nb: 1"));

Note that this is recursive!

let template = Context::from("a: 1\nb:\n  b1: 0");
let values = Context::from("b:\n  b1: 1\n  b2: 2\nc: 2");
assert_eq!(template.render(&values), Context::from("a: 1\nb:\n  b1: 1"));

pub fn set(&self, key: &str, val: CtxObj) -> Context[src]

Set a context element.

Example

let eg = Context::from("a: 1\nb: 0");
assert_eq!(eg.set("b", CtxObj::Int(1)), Context::from("a: 1\nb: 1"));

pub fn set_opt(&self, key: &str, optional: Option<CtxObj>) -> Context[src]

Optionally set a context element. (Syntax sugar to adapt to Option)

Example

let eg = Context::from("a: 1\nb: 0");
assert_eq!(eg.set_opt("b", Some(CtxObj::Int(1))), Context::from("a: 1\nb: 1"));
assert_eq!(eg.set_opt("b", None), Context::from("a: 1\nb: 0"));

pub fn get(&self, key: &str) -> Option<&CtxObj>[src]

Get an element by reference if the key exists.

Example

let eg = Context::from("a: 1");
assert_eq!(eg.get("a").unwrap().clone(), CtxObj::Int(1));
assert_eq!(eg.get("b"), None);

pub fn get_clone(&self, key: &str) -> Option<CtxObj>[src]

Get an element by value if the key exists.

Example

let eg = Context::from("a: 1");
assert_eq!(eg.get_clone("a").unwrap(), CtxObj::Int(1));
assert_eq!(eg.get_clone("b"), None);

pub fn unpack<T>(&self, key: &str) -> Result<T, CtxObjUnpackError> where
    T: CtxObjUnpack
[src]

Unpack an element by key if it exists.

Example

let eg = Context::from("a: 1");
let success: i64 = eg.unpack("a").unwrap();
assert_eq!(success, 1);
 
/*Although, beware of auto-conversion due to polymorphism with the CtxObjUnpack trait.*/
let success: i32 = eg.unpack("a").unwrap();
assert_eq!(success, 1);

This should fail because the key specified does not exist.

let eg = Context::from("a: 1");
let err: i64 = match eg.unpack("b") {
    Ok(v) => v,
    Err(_) => panic!()
};

This should also fail because the type conversion is not feasible.

let eg = Context::from("a: 1");
let err: String = match eg.unpack("b") {
    Ok(v) => v,
    Err(_) => panic!()
};

pub fn subcontext(&self, key: &str) -> Option<Context>[src]

Enter a nested context.

Example

let eg = Context::from("s:\n  a: 1");
assert_eq!(eg.subcontext("s"), Some(Context::from("a: 1")));

pub fn list_contexts(&self, key: &str) -> Option<Vec<Context>>[src]

List nested contexts.

Example

let eg = Context::from("s:\n- a: 1\n- b: 1");
assert_eq!(eg.list_contexts("s"), Some(vec![Context::from("a: 1"), Context::from("b: 1")]));

pub fn hide(&self, key: &str) -> Context[src]

Hide an element by key if it exists.

Example

let eg = Context::from("a: 1\nb: 1");
assert_eq!(eg.hide("b"), Context::from("a: 1"));
assert_eq!(eg.hide("c"), eg);

pub fn keys(
    &self
) -> Map<Map<IterArc<String, CtxObj>, fn(&Arc<Entry<String, CtxObj>>) -> (&String, &CtxObj)>, fn((&String, &CtxObj)) -> &String>
[src]

Get all keys from this context.

Example

let eg = Context::from("a: 1\nb: 1");
let keys: std::collections::HashSet<String> = eg.keys().map(|k| { k.to_owned() }).collect();
assert_eq!(keys, vec![String::from("a"), String::from("b")].into_iter().collect());

Trait Implementations

impl Debug for Context[src]

impl<'a> Index<&'a str> for Context[src]

type Output = CtxObj

The returned type after indexing.

impl Clone for Context[src]

default fn clone_from(&mut self, source: &Self)
1.0.0
[src]

Performs copy-assignment from source. Read more

impl Serialize for Context[src]

impl<'a> From<&'a str> for Context[src]

impl From<Yaml> for Context[src]

impl<'de> Deserialize<'de> for Context[src]

impl Into<Yaml> for Context[src]

impl PartialEq<Context> for Context[src]

impl Display for Context[src]

Auto Trait Implementations

impl Send for Context

impl Sync for Context

Blanket Implementations

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

impl<T> ToString for T where
    T: Display + ?Sized
[src]

impl<T> From for T[src]

impl<T, U> Into for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T> Borrow for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> BorrowMut for T where
    T: ?Sized
[src]

impl<T, U> TryInto for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

impl<T> DeserializeOwned for T where
    T: Deserialize<'de>, 
[src]