pub struct Context { /* private fields */ }
Expand description
Context is a collection of attributes that can be referenced in flag evaluations and analytics events. These attributes are described by one or more Kinds.
For example, a context might represent the user of a service, the description of an organization, IoT device metadata, or any combination of those at once.
To create a context of a single kind, such as a user, you may use crate::ContextBuilder.
To create a context with multiple kinds, use crate::MultiContextBuilder.
Implementations§
Source§impl Context
impl Context
Sourcepub fn without_anonymous_contexts(&self) -> Result<Context, String>
pub fn without_anonymous_contexts(&self) -> Result<Context, String>
For a multi-kind context:
A multi-kind context is made up of two or more single-kind contexts. This method will first discard any single-kind contexts which are anonymous. It will then create a new multi-kind context from the remaining single-kind contexts. This may result in an invalid context (e.g. all single-kind contexts are anonymous).
For a single-kind context:
If the context is not anonymous, this method will return the current context as is and unmodified.
If the context is anonymous, this method will return an Err(String)
result.
Sourcepub fn get_value(&self, reference: &Reference) -> Option<AttributeValue>
pub fn get_value(&self, reference: &Reference) -> Option<AttributeValue>
Looks up the value of any attribute of the context, or a value contained within an attribute, based on the given reference.
This lookup includes only attributes that are addressable in evaluations– not metadata such as private attributes.
This method implements the same behavior that the SDK uses to resolve attribute references during a flag evaluation. In a single context, the reference can represent a simple attribute name– either a built-in one like “name” or “key”, or a custom attribute that was set by methods like crate::ContextBuilder::set_string– or, it can be a slash-delimited path.
For a multi-context, the only supported attribute name is “kind”. Use Context::as_kind to inspect a context for a particular Kind and then get its attributes.
Sourcepub fn key(&self) -> &str
pub fn key(&self) -> &str
Returns the “key” attribute.
For a single context, this value is set by the crate::ContextBuilder::new or crate::ContextBuilder::key methods.
For a multi-context, there is no single key, so Context::key returns an empty string; use Context::as_kind to inspect a context for a particular kind and call Context::key on it.
Sourcepub fn canonical_key(&self) -> &str
pub fn canonical_key(&self) -> &str
Returns the canonical key.
- For a single context of kind “user”, the canonical key is equivalent to the key.
- For other kinds of single contexts, the canonical key is “kind:key”.
- For a multi-context, the canonical key is the concatenation of its constituent contexts’
canonical keys with
:
according to (2) (including kind “user”).
Sourcepub fn as_kind(&self, kind: &Kind) -> Option<&Context>
pub fn as_kind(&self, kind: &Kind) -> Option<&Context>
If the specified kind exists within the context, returns a reference to it. Otherwise, returns None.
Sourcepub fn context_keys(&self) -> HashMap<&Kind, &str>
pub fn context_keys(&self) -> HashMap<&Kind, &str>
Returns a map of all (kind, key) pairs contained in this context.
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>,
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,
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
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>
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>
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