scuffle_cedar_policy/
entity_ref.rs1use std::marker::PhantomData;
2
3use serde::ser::SerializeMap;
4
5use crate::{CedarEntity, CedarId};
6
7pub struct EntityUid<E> {
9 id: smol_str::SmolStr,
10 _marker: PhantomData<E>,
11}
12
13impl<E> serde::Serialize for EntityUid<E>
14where
15 E: CedarEntity,
16{
17 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
18 where
19 S: serde::Serializer,
20 {
21 let mut map = serializer.serialize_map(Some(2))?;
22
23 map.serialize_entry("type", E::TYPE_NAME.as_str())?;
24 map.serialize_entry("id", self.id.as_str())?;
25
26 map.end()
27 }
28}
29
30impl<E: CedarEntity> EntityUid<E> {
31 pub fn new(id: impl Into<E::Id>) -> Self {
33 Self::new_from_str(id.into().into_smol_string())
34 }
35
36 pub(crate) fn new_from_str(id: impl Into<smol_str::SmolStr>) -> Self {
37 EntityUid {
38 id: id.into(),
39 _marker: PhantomData,
40 }
41 }
42}
43
44impl<E: CedarEntity> From<EntityUid<E>> for cedar_policy::EntityUid {
45 fn from(value: EntityUid<E>) -> Self {
46 Self::from_type_name_and_id(E::entity_type_name().clone(), cedar_policy::EntityId::new(value.id))
47 }
48}