[−][src]Struct playbook_api::Context
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]
T: CtxObjUnpack,
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]
&self
) -> Map<Map<IterArc<String, CtxObj>, fn(&Arc<Entry<String, CtxObj>>) -> (&String, &CtxObj)>, fn((&String, &CtxObj)) -> &String>
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]
impl Clone for Context
[src]
fn clone(&self) -> 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]
fn serialize<__S>(
&self,
__serializer: __S
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error> where
__S: Serializer,
[src]
&self,
__serializer: __S
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error> where
__S: Serializer,
impl<'a> From<&'a str> for Context
[src]
impl From<Yaml> for Context
[src]
impl<'de> Deserialize<'de> for Context
[src]
fn deserialize<__D>(
__deserializer: __D
) -> Result<Context, <__D as Deserializer<'de>>::Error> where
__D: Deserializer<'de>,
[src]
__deserializer: __D
) -> Result<Context, <__D as Deserializer<'de>>::Error> where
__D: Deserializer<'de>,
impl Into<Yaml> for Context
[src]
impl PartialEq<Context> for Context
[src]
impl Display for Context
[src]
Auto Trait Implementations
Blanket Implementations
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
impl<T> ToString for T where
T: Display + ?Sized,
[src]
T: Display + ?Sized,
impl<T> From for T
[src]
impl<T, U> Into for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T> Borrow for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> BorrowMut for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T, U> TryInto for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<T> DeserializeOwned for T where
T: Deserialize<'de>,
[src]
T: Deserialize<'de>,