cedar_policy_core/entities/json/
schema.rs1use super::SchemaType;
18use crate::ast::{Entity, EntityType, EntityUID};
19use crate::entities::{Name, UnreservedId};
20use smol_str::SmolStr;
21use std::collections::{HashMap, HashSet};
22use std::sync::Arc;
23
24pub trait Schema {
26 type EntityTypeDescription: EntityTypeDescription;
28
29 type ActionEntityIterator: IntoIterator<Item = Arc<Entity>>;
31
32 fn entity_type(&self, entity_type: &EntityType) -> Option<Self::EntityTypeDescription>;
36
37 fn action(&self, action: &EntityUID) -> Option<Arc<Entity>>;
41
42 fn entity_types_with_basename<'a>(
45 &'a self,
46 basename: &'a UnreservedId,
47 ) -> Box<dyn Iterator<Item = EntityType> + 'a>;
48
49 fn action_entities(&self) -> Self::ActionEntityIterator;
51}
52
53#[derive(Debug, Clone)]
55pub struct NoEntitiesSchema;
56impl Schema for NoEntitiesSchema {
57 type EntityTypeDescription = NullEntityTypeDescription;
58 type ActionEntityIterator = std::iter::Empty<Arc<Entity>>;
59 fn entity_type(&self, _entity_type: &EntityType) -> Option<NullEntityTypeDescription> {
60 None
61 }
62 fn action(&self, _action: &EntityUID) -> Option<Arc<Entity>> {
63 None
64 }
65 fn entity_types_with_basename<'a>(
66 &'a self,
67 _basename: &'a UnreservedId,
68 ) -> Box<dyn Iterator<Item = EntityType> + 'a> {
69 Box::new(std::iter::empty())
70 }
71 fn action_entities(&self) -> std::iter::Empty<Arc<Entity>> {
72 std::iter::empty()
73 }
74}
75
76#[derive(Debug, Clone)]
86pub struct AllEntitiesNoAttrsSchema;
87impl Schema for AllEntitiesNoAttrsSchema {
88 type EntityTypeDescription = NullEntityTypeDescription;
89 type ActionEntityIterator = std::iter::Empty<Arc<Entity>>;
90 fn entity_type(&self, entity_type: &EntityType) -> Option<NullEntityTypeDescription> {
91 Some(NullEntityTypeDescription {
92 ty: entity_type.clone(),
93 })
94 }
95 fn action(&self, action: &EntityUID) -> Option<Arc<Entity>> {
96 Some(Arc::new(Entity::new_with_attr_partial_value(
97 action.clone(),
98 HashMap::new(),
99 HashSet::new(),
100 )))
101 }
102 fn entity_types_with_basename<'a>(
103 &'a self,
104 basename: &'a UnreservedId,
105 ) -> Box<dyn Iterator<Item = EntityType> + 'a> {
106 Box::new(std::iter::once(EntityType::from(Name::unqualified_name(
107 basename.clone(),
108 ))))
109 }
110 fn action_entities(&self) -> std::iter::Empty<Arc<Entity>> {
111 std::iter::empty()
112 }
113}
114
115pub trait EntityTypeDescription {
117 fn entity_type(&self) -> EntityType;
119
120 fn attr_type(&self, attr: &str) -> Option<SchemaType>;
124
125 fn required_attrs<'s>(&'s self) -> Box<dyn Iterator<Item = SmolStr> + 's>;
127
128 fn allowed_parent_types(&self) -> Arc<HashSet<EntityType>>;
130
131 fn open_attributes(&self) -> bool;
134}
135
136#[derive(Debug, Clone)]
139pub struct NullEntityTypeDescription {
140 ty: EntityType,
142}
143impl EntityTypeDescription for NullEntityTypeDescription {
144 fn entity_type(&self) -> EntityType {
145 self.ty.clone()
146 }
147 fn attr_type(&self, _attr: &str) -> Option<SchemaType> {
148 None
149 }
150 fn required_attrs(&self) -> Box<dyn Iterator<Item = SmolStr>> {
151 Box::new(std::iter::empty())
152 }
153 fn allowed_parent_types(&self) -> Arc<HashSet<EntityType>> {
154 Arc::new(HashSet::new())
155 }
156 fn open_attributes(&self) -> bool {
157 false
158 }
159}