Struct cedar_policy::Entity

source ·
pub struct Entity(/* private fields */);
Expand description

Entity datatype

Implementations§

source§

impl Entity

source

pub fn new( uid: EntityUid, attrs: HashMap<String, RestrictedExpression>, parents: HashSet<EntityUid>, ) -> Self

Create a new Entity with this Uid, attributes, and parents.

Attribute values are specified here as “restricted expressions”. See docs on RestrictedExpression

let eid = EntityId::from_str("alice").unwrap();
let type_name = EntityTypeName::from_str("User").unwrap();
let euid = EntityUid::from_type_name_and_id(type_name, eid);
let attrs = HashMap::from([
    ("age".to_string(), RestrictedExpression::from_str("21").unwrap()),
    ("department".to_string(), RestrictedExpression::from_str("\"CS\"").unwrap()),
]);
let parent_eid = EntityId::from_str("admin").unwrap();
let parent_type_name = EntityTypeName::from_str("Group").unwrap();
let parent_euid = EntityUid::from_type_name_and_id(parent_type_name, parent_eid);
let parents = HashSet::from([parent_euid]);
let entity = Entity::new(euid, attrs, parents);
source

pub fn with_uid(uid: EntityUid) -> Self

Create a new Entity with this Uid, no attributes, and no parents.

use cedar_policy::{Entity, EntityId, EntityTypeName, EntityUid};
let eid = EntityId::from_str("alice").unwrap();
let type_name = EntityTypeName::from_str("User").unwrap();
let euid = EntityUid::from_type_name_and_id(type_name, eid);
let alice = Entity::with_uid(euid);
source

pub fn uid(&self) -> EntityUid

Get the Uid of this entity

let type_name = EntityTypeName::from_str("User").unwrap();
let euid = EntityUid::from_type_name_and_id(type_name, eid);
let alice = Entity::with_uid(euid.clone());
assert_eq!(alice.uid(), euid);
source

pub fn attr(&self, attr: &str) -> Option<Result<EvalResult, EvaluationError>>

Get the value for the given attribute, or None if not present.

This can also return Some(Err) if the attribute had an illegal value.

use cedar_policy::{Entity, EntityId, EntityTypeName, EntityUid, EvalResult,
    RestrictedExpression};
use std::collections::{HashMap, HashSet};
use std::str::FromStr;
let eid = EntityId::from_str("alice").unwrap();
let type_name = EntityTypeName::from_str("User").unwrap();
let euid = EntityUid::from_type_name_and_id(type_name, eid);
let attrs = HashMap::from([
    ("age".to_string(), RestrictedExpression::from_str("21").unwrap()),
    ("department".to_string(), RestrictedExpression::from_str("\"CS\"").unwrap()),
]);
let entity = Entity::new(euid, attrs, HashSet::new());
assert_eq!(entity.attr("age").unwrap(), Ok(EvalResult::Long(21)));
assert_eq!(entity.attr("department").unwrap(), Ok(EvalResult::String("CS".to_string())));

Trait Implementations§

source§

impl Clone for Entity

source§

fn clone(&self) -> Entity

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 Entity

source§

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

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

impl Display for Entity

source§

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

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

impl PartialEq for Entity

source§

fn eq(&self, other: &Entity) -> 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 RefCast for Entity

source§

type From = Entity

source§

fn ref_cast(_from: &Self::From) -> &Self

source§

fn ref_cast_mut(_from: &mut Self::From) -> &mut Self

source§

impl Eq for Entity

source§

impl StructuralPartialEq for Entity

Auto Trait Implementations§

§

impl Freeze for Entity

§

impl RefUnwindSafe for Entity

§

impl Send for Entity

§

impl Sync for Entity

§

impl Unpin for Entity

§

impl UnwindSafe for Entity

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§

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> IntoEither for T

source§

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 more
source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

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
source§

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

source§

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> ToSmolStr for T
where T: Display + ?Sized,

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>,

source§

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>,

source§

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.