scuffle_cedar_policy/
traits.rs1use crate::entity_type_name::EntityTypeName;
2use crate::{CedarActionRequestError, Entity, EntityUid};
3
4pub trait CedarId {
6 fn into_smol_string(self) -> smol_str::SmolStr;
8}
9
10impl CedarId for smol_str::SmolStr {
11 fn into_smol_string(self) -> smol_str::SmolStr {
12 self
13 }
14}
15
16impl CedarId for String {
17 fn into_smol_string(self) -> smol_str::SmolStr {
18 self.into()
19 }
20}
21
22pub trait CedarEntity {
24 type TagType: serde::Serialize;
27 type Id: CedarId;
29 type Attrs: serde::Serialize;
31
32 const TYPE_NAME: EntityTypeName;
34
35 fn entity_type_name() -> &'static cedar_policy::EntityTypeName;
37}
38
39pub trait CedarEnumEntity: CedarEntity<Id = Self, Attrs = crate::NoAttributes> {
41 fn into_entity(self) -> Entity<Self>
43 where
44 Self: Sized;
45}
46
47pub trait CedarAction<Principal, Resource>: CedarActionEntity {
49 type Context: serde::Serialize;
52
53 fn request(
55 principal: EntityUid<Principal>,
56 resource: EntityUid<Resource>,
57 ctx: &Self::Context,
58 schema: Option<&cedar_policy::Schema>,
59 ) -> Result<cedar_policy::Request, CedarActionRequestError>
60 where
61 Principal: CedarEntity,
62 Resource: CedarEntity,
63 {
64 let ctx_json = serde_json::to_value(ctx)?;
65 let a_euid = Self::action_entity_uid();
66 let context = cedar_policy::Context::from_json_value(ctx_json, schema.map(|s| (s, a_euid)))?;
67 Ok(cedar_policy::Request::new(
68 principal.into(),
69 a_euid.clone(),
70 resource.into(),
71 context,
72 schema,
73 )?)
74 }
75}
76
77pub trait CedarActionEntity {
79 fn action_entity_uid() -> &'static cedar_policy::EntityUid;
81}
82
83pub trait CedarChild<Parent> {}