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 (and no tags).
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 or tags.
Unlike Entity::new(), this constructor cannot error.
(The only source of errors in Entity::new() are attributes.)
Create a new Entity with this Uid, attributes, parents, and tags.
Attribute and tag values are specified here as “restricted expressions”.
See docs on RestrictedExpression.
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 deep_eq(&self, other: &Self) -> bool
pub fn deep_eq(&self, other: &Self) -> bool
Test if two entities are structurally equal. That is, not only do they have the same UID, but they also have the same attributes and ancestors.
Note that ancestor equality is determined by examining the ancestors
entities provided when constructing these objects, without computing
their transitive closure. For accurate comparison, entities should be
constructed with the transitive closure precomputed or be drawn from an
Entities object which will perform this computation.
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, PartialValueToValueError>>
pub fn attr( &self, attr: &str, ) -> Option<Result<EvalResult, PartialValueToValueError>>
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());Sourcepub fn tag(
&self,
tag: &str,
) -> Option<Result<EvalResult, PartialValueToValueError>>
pub fn tag( &self, tag: &str, ) -> Option<Result<EvalResult, PartialValueToValueError>>
Get the value for the given tag, or None if not present.
This can also return Some(Err) if the tag is not a value (i.e., is unknown due to partial evaluation).
Sourcepub fn into_inner(
self,
) -> (EntityUid, HashMap<String, RestrictedExpression>, HashSet<EntityUid>)
pub fn into_inner( self, ) -> (EntityUid, HashMap<String, RestrictedExpression>, HashSet<EntityUid>)
Consume the entity and return the entity’s owned Uid, attributes and parents.
Sourcepub fn from_json_value(
value: Value,
schema: Option<&Schema>,
) -> Result<Self, EntitiesError>
pub fn from_json_value( value: Value, schema: Option<&Schema>, ) -> Result<Self, EntitiesError>
Parse an entity from an in-memory JSON value
If a schema is provided, it is handled identically to Entities::from_json_str
Sourcepub fn from_json_str(
src: impl AsRef<str>,
schema: Option<&Schema>,
) -> Result<Self, EntitiesError>
pub fn from_json_str( src: impl AsRef<str>, schema: Option<&Schema>, ) -> Result<Self, EntitiesError>
Parse an entity from a JSON string
If a schema is provided, it is handled identically to Entities::from_json_str
Sourcepub fn from_json_file(
f: impl Read,
schema: Option<&Schema>,
) -> Result<Self, EntitiesError>
pub fn from_json_file( f: impl Read, schema: Option<&Schema>, ) -> Result<Self, EntitiesError>
Parse an entity from a JSON reader
If a schema is provided, it is handled identically to Entities::from_json_str
Sourcepub fn write_to_json(&self, f: impl Write) -> Result<(), EntitiesError>
pub fn write_to_json(&self, f: impl Write) -> Result<(), EntitiesError>
Dump an Entity object into an entity 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 Entity object from JSON , use
Self::from_json_file, Self::from_json_value, or Self::from_json_str.
Sourcepub fn to_json_value(&self) -> Result<Value, EntitiesError>
pub fn to_json_value(&self) -> Result<Value, EntitiesError>
Dump an Entity object into an in-memory JSON object.
The resulting JSON will be suitable for parsing in via
from_json_*, and will be parse-able even with no Schema.
To read an Entity object from JSON , use
Self::from_json_file, Self::from_json_value, or Self::from_json_str.
Sourcepub fn to_json_string(&self) -> Result<String, EntitiesError>
pub fn to_json_string(&self) -> Result<String, EntitiesError>
Dump an Entity object into a JSON string.
The resulting JSON will be suitable for parsing in via
from_json_*, and will be parse-able even with no Schema.
To read an Entity object from JSON , use
Self::from_json_file, Self::from_json_value, or Self::from_json_str.
Trait Implementations§
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
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