pub struct Context { /* private fields */ }
Expand description
A data structure designed to provide a high-level language agnostic calling convention.
Implementations§
Source§impl Context
impl Context
Sourcepub fn overlay(&self, another: &Context) -> Context
pub fn overlay(&self, another: &Context) -> Context
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"));
Sourcepub fn render(&self, values: &Context) -> Context
pub fn render(&self, values: &Context) -> Context
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"));
Sourcepub fn set(&self, key: &str, val: CtxObj) -> Context
pub fn set(&self, key: &str, val: CtxObj) -> Context
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"));
Sourcepub fn set_opt(&self, key: &str, optional: Option<CtxObj>) -> Context
pub fn set_opt(&self, key: &str, optional: Option<CtxObj>) -> Context
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"));
Sourcepub fn get(&self, key: &str) -> Option<&CtxObj>
pub fn get(&self, key: &str) -> Option<&CtxObj>
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);
Sourcepub fn get_clone(&self, key: &str) -> Option<CtxObj>
pub fn get_clone(&self, key: &str) -> Option<CtxObj>
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);
Sourcepub fn unpack<T>(&self, key: &str) -> Result<T, CtxObjUnpackError>where
T: CtxObjUnpack,
pub fn unpack<T>(&self, key: &str) -> Result<T, CtxObjUnpackError>where
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!()
};
Sourcepub fn subcontext(&self, key: &str) -> Option<Context>
pub fn subcontext(&self, key: &str) -> Option<Context>
Enter a nested context.
Example
let eg = Context::from("s:\n a: 1");
assert_eq!(eg.subcontext("s"), Some(Context::from("a: 1")));
Sourcepub fn list_contexts(&self, key: &str) -> Option<Vec<Context>>
pub fn list_contexts(&self, key: &str) -> Option<Vec<Context>>
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")]));
Sourcepub fn hide(&self, key: &str) -> Context
pub fn hide(&self, key: &str) -> Context
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);
Sourcepub fn keys(
&self,
) -> Map<Map<IterArc<'_, String, CtxObj>, fn(&Arc<Entry<String, CtxObj>>) -> (&String, &CtxObj)>, fn((&String, &CtxObj)) -> &String>
pub fn keys( &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§
Source§impl<'de> Deserialize<'de> for Context
impl<'de> Deserialize<'de> for Context
Source§fn deserialize<__D>(
__deserializer: __D,
) -> Result<Context, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(
__deserializer: __D,
) -> Result<Context, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
Source§impl Serialize for Context
impl Serialize for Context
Source§fn serialize<__S>(
&self,
__serializer: __S,
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
fn serialize<__S>(
&self,
__serializer: __S,
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
Serialize this value into the given Serde serializer. Read more
impl StructuralPartialEq for Context
Auto Trait Implementations§
impl Freeze for Context
impl RefUnwindSafe for Context
impl Send for Context
impl Sync for Context
impl Unpin for Context
impl UnwindSafe for Context
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more