Struct cedar_policy::Entities
source · pub struct Entities(/* private fields */);
Expand description
Represents an entity hierarchy, and allows looking up Entity
objects by
Uid.
Implementations§
source§impl Entities
impl Entities
sourcepub fn empty() -> Self
pub fn empty() -> Self
Create a fresh Entities
with no entities
use cedar_policy::Entities;
let entities = Entities::empty();
sourcepub fn get(&self, uid: &EntityUid) -> Option<&Entity>
pub fn get(&self, uid: &EntityUid) -> Option<&Entity>
Get the Entity
with the given Uid, if any
sourcepub fn from_entities(
entities: impl IntoIterator<Item = Entity>,
) -> Result<Self, EntitiesError>
pub fn from_entities( entities: impl IntoIterator<Item = Entity>, ) -> Result<Self, EntitiesError>
Create an Entities
object with the given entities.
It will error if the entities cannot be read or if the entities hierarchy is cyclic
sourcepub fn from_json_str(
json: &str,
schema: Option<&Schema>,
) -> Result<Self, EntitiesError>
pub fn from_json_str( json: &str, schema: Option<&Schema>, ) -> Result<Self, EntitiesError>
Parse an entities JSON file (in &str
form) into an Entities
object
If a schema
is provided, this will inform the parsing: for instance, it
will allow __entity
and __extn
escapes to be implicit, and it will error
if attributes have the wrong types (e.g., string instead of integer).
use std::collections::HashMap;
use std::str::FromStr;
let data =r#"
[
{
"uid": {"type":"User","id":"alice"},
"attrs": {
"age":19,
"ip_addr":{"__extn":{"fn":"ip", "arg":"10.0.1.101"}}
},
"parents": [{"type":"Group","id":"admin"}]
},
{
"uid": {"type":"Groupd","id":"admin"},
"attrs": {},
"parents": []
}
]
"#;
let entities = Entities::from_json_str(data, None).unwrap();
let eid = EntityId::from_str("alice").unwrap();
let type_name: EntityTypeName = EntityTypeName::from_str("User").unwrap();
let euid = EntityUid::from_type_name_and_id(type_name, eid);
let entity = entities.get(&euid).unwrap();
assert_eq!(entity.attr("age").unwrap(), Ok(EvalResult::Long(19)));
let ip = entity.attr("ip_addr").unwrap().unwrap();
assert_eq!(ip, EvalResult::ExtensionValue("10.0.1.101/32".to_string()));
sourcepub fn from_json_value(
json: Value,
schema: Option<&Schema>,
) -> Result<Self, EntitiesError>
pub fn from_json_value( json: Value, schema: Option<&Schema>, ) -> Result<Self, EntitiesError>
Parse an entities JSON file (in serde_json::Value
form) into an
Entities
object
If a schema
is provided, this will inform the parsing: for instance, it
will allow __entity
and __extn
escapes to be implicit, and it will error
if attributes have the wrong types (e.g., string instead of integer).
use std::collections::HashMap;
use std::str::FromStr;
let data =serde_json::json!(
[
{
"uid": {"type":"User","id":"alice"},
"attrs": {
"age":19,
"ip_addr":{"__extn":{"fn":"ip", "arg":"10.0.1.101"}}
},
"parents": [{"type":"Group","id":"admin"}]
},
{
"uid": {"type":"Groupd","id":"admin"},
"attrs": {},
"parents": []
}
]
);
let entities = Entities::from_json_value(data, None).unwrap();
sourcepub fn from_json_file(
json: impl Read,
schema: Option<&Schema>,
) -> Result<Self, EntitiesError>
pub fn from_json_file( json: impl Read, schema: Option<&Schema>, ) -> Result<Self, EntitiesError>
Parse an entities JSON file (in std::io::Read
form) into an Entities
object
If a schema
is provided, this will inform the parsing: for instance, it
will allow __entity
and __extn
escapes to be implicit, and it will error
if attributes have the wrong types (e.g., string instead of integer).
sourcepub fn is_ancestor_of(&self, a: &EntityUid, b: &EntityUid) -> bool
pub fn is_ancestor_of(&self, a: &EntityUid, b: &EntityUid) -> bool
Is entity a
an ancestor of entity b
?
Same semantics as b in a
in the Cedar language
sourcepub fn ancestors<'a>(
&'a self,
euid: &EntityUid,
) -> Option<impl Iterator<Item = &'a EntityUid>>
pub fn ancestors<'a>( &'a self, euid: &EntityUid, ) -> Option<impl Iterator<Item = &'a EntityUid>>
Get an iterator over the ancestors of the given Euid.
Returns None
if the given Euid
does not exist.
sourcepub fn write_to_json(&self, f: impl Write) -> Result<(), EntitiesError>
pub fn write_to_json(&self, f: impl Write) -> Result<(), EntitiesError>
Dump an Entities
object into an entities JSON file.
The resulting JSON will be suitable for parsing in via
from_json_*
, and will be parse-able even with no Schema
.
To read an Entities
object from an entities JSON file, use
from_json_file
.
Trait Implementations§
impl Eq for Entities
impl StructuralPartialEq for Entities
Auto Trait Implementations§
impl Freeze for Entities
impl RefUnwindSafe for Entities
impl Send for Entities
impl Sync for Entities
impl Unpin for Entities
impl UnwindSafe for Entities
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§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)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
key
and return true
if they are equal.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