Struct cedar_policy::Entity
source · pub struct Entity(/* private fields */);
Expand description
Entity datatype
Implementations§
source§impl Entity
impl Entity
sourcepub fn new(
uid: EntityUid,
attrs: HashMap<String, RestrictedExpression>,
parents: HashSet<EntityUid>
) -> Result<Self, EntityAttrEvaluationError>
pub fn new( uid: EntityUid, attrs: HashMap<String, RestrictedExpression>, parents: HashSet<EntityUid> ) -> Result<Self, EntityAttrEvaluationError>
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);
sourcepub fn new_no_attrs(uid: EntityUid, parents: HashSet<EntityUid>) -> Self
pub fn new_no_attrs(uid: EntityUid, parents: HashSet<EntityUid>) -> Self
Create a new Entity
with no attributes.
Unlike Entity::new()
, this constructor cannot error.
(The only source of errors in Entity::new()
are attributes.)
sourcepub fn with_uid(uid: EntityUid) -> Self
pub fn with_uid(uid: EntityUid) -> Self
Create a new Entity
with this Uid, no attributes, and no parents.
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);
sourcepub fn uid(&self) -> EntityUid
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);
sourcepub fn attr(&self, attr: &str) -> Option<Result<EvalResult, impl Diagnostic>>
pub fn attr(&self, attr: &str) -> Option<Result<EvalResult, impl Diagnostic>>
Get the value for the given attribute, or None
if not present.
This can also return Some(Err) if the attribute is not a value (i.e., is unknown due to partial evaluation).
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()).unwrap();
assert_eq!(entity.attr("age").unwrap().unwrap(), EvalResult::Long(21));
assert_eq!(entity.attr("department").unwrap().unwrap(), EvalResult::String("CS".to_string()));
assert!(entity.attr("foo").is_none());
Trait Implementations§
source§impl PartialEq for Entity
impl PartialEq for Entity
impl Eq for Entity
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> 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<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
Compare self to
key
and return true
if they are equal.