Skip to content

Commit

Permalink
converting from Entities to Hierarchy
Browse files Browse the repository at this point in the history
  • Loading branch information
cdisselkoen committed Aug 16, 2023
1 parent 48e7655 commit 31d0a3c
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions cedar-policy-generators/src/hierarchy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,31 @@ impl TryFrom<Hierarchy> for Entities {
}
}

impl From<Entities> for Hierarchy {
fn from(entities: Entities) -> Hierarchy {
let mut uids = Vec::new();
let mut uids_by_type: HashMap<ast::Name, HashSet<ast::EntityUID>> = HashMap::new();
for e in entities.iter() {
let etype = match e.uid().entity_type() {
ast::EntityType::Concrete(name) => name.clone(),
ast::EntityType::Unspecified => {
panic!("didn't expect unspecified entity in Entities")
}
};
uids_by_type.entry(etype).or_default().insert(e.uid());
uids.push(e.uid());
}
Hierarchy {
uids,
uids_by_type: uids_by_type
.into_iter()
.map(|(k, v)| (k, EntityUIDs::from_iter(v.into_iter())))
.collect(),
entities: entities.into_iter().map(|e| (e.uid(), e)).collect(),
}
}
}

/// Struct for generating hierarchies; contains options and settings
pub struct HierarchyGenerator<'a, 'u> {
/// Mode for hierarchy generation, e.g., whether to conform to a schema
Expand Down

0 comments on commit 31d0a3c

Please sign in to comment.