pub struct Reference { /* private fields */ }
Expand description

Represents an attribute name or path expression identifying a value within a crate::Context.

This can be used to retrieve a value with crate::Context::get_value, or to identify an attribute or nested value that should be considered private with crate::ContextBuilder::add_private_attribute (the SDK configuration can also have a list of private attribute references).

This is represented as a separate type, rather than just a string, so that validation and parsing can be done ahead of time if an attribute reference will be used repeatedly later (such as in flag evaluations).

If the string starts with ‘/’, then this is treated as a slash-delimited path reference where the first component is the name of an attribute, and subsequent components are the names of nested JSON object properties. In this syntax, the escape sequences “~0” and “1” represent ‘’ and ‘/’ respectively within a path component.

If the string does not start with ‘/’, then it is treated as the literal name of an attribute.

§Example

// Given the following JSON representation of a context:
{
  "kind": "user",
  "key": "123",
  "name": "xyz",
  "address": {
    "street": "99 Main St.",
    "city": "Westview"
  },
  "a/b": "ok"
}

assert_eq!(context.get_value(&Reference::new("name")),
    Some(AttributeValue::String("xyz".to_owned())));
assert_eq!(context.get_value(&Reference::new("/address/street")),
    Some(AttributeValue::String("99 Main St.".to_owned())));
assert_eq!(context.get_value(&Reference::new("a/b")),
    Some(AttributeValue::String("ok".to_owned())));
assert_eq!(context.get_value(&Reference::new("/a~1b")),
    Some(AttributeValue::String("ok".to_owned())));

Implementations§

source§

impl Reference

source

pub fn new<S: AsRef<str>>(value: S) -> Self

Construct a new context attribute reference.

This constructor always returns a reference that preserves the original string, even if validation fails, so that serializing the reference to JSON will produce the original string.

source

pub fn is_valid(&self) -> bool

Returns true if the reference is valid.

source

pub fn error(&self) -> String

If the reference is invalid, this method returns an error description; otherwise, it returns an empty string.

source

pub fn depth(&self) -> usize

Returns the number of path components in the reference.

For a simple attribute reference such as “name” with no leading slash, this returns 1.

For an attribute reference with a leading slash, it is the number of slash-delimited path components after the initial slash.

§Example
assert_eq!(Reference::new("a").depth(), 1);
assert_eq!(Reference::new("/a/b").depth(), 2);
source

pub fn component(&self, index: usize) -> Option<&str>

Retrieves a single path component from the attribute reference.

Returns the attribute name for a simple attribute reference such as “name” with no leading slash, if index is zero.

Returns the specified path component if index is less than Reference::depth, and the reference begins with a slash.

If index is out of range, it returns None.

§Examples
assert_eq!(Reference::new("a").component(0), Some("a"));
assert_eq!(Reference::new("/a/b").component(1), Some("b"));
assert_eq!(Reference::new("/a/b").component(2), None);

Trait Implementations§

source§

impl Clone for Reference

source§

fn clone(&self) -> Reference

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Reference

source§

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

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

impl Default for Reference

source§

fn default() -> Self

A default Reference is empty and invalid.

source§

impl<'de> Deserialize<'de> for Reference

source§

fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
source§

impl Display for Reference

Displays the input string used to construct the Reference.

source§

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

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

impl From<Reference> for String

source§

fn from(r: Reference) -> Self

Converts to this type from the input type.
source§

impl<S> From<S> for Reference
where S: AsRef<str>,

source§

fn from(reference: S) -> Self

Converts to this type from the input type.
source§

impl Hash for Reference

source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl PartialEq for Reference

source§

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

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

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Serialize for Reference

source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
source§

impl Eq for Reference

source§

impl StructuralPartialEq for Reference

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

source§

default unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
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> Same for T

§

type Output = T

Should always be Self
source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
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.
source§

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