use std::collections::HashMap;
use crate::ids::{EntityId, GroupId, ModuleId};
use crate::model::entity::StoredEntity;
use crate::model::link::EntityRef;
use crate::model::manual::ReferenceManual;
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct IndexedEntity {
pub module_id: ModuleId,
pub contract_family: String,
pub group_id: GroupId,
pub entity: StoredEntity,
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct ResolvedManual {
pub entities: Vec<IndexedEntity>,
pub by_ref: HashMap<EntityRef, EntityId>,
}
impl ResolvedManual {
pub fn from_reference_manual(manual: &ReferenceManual) -> Self {
let mut indexed = Vec::new();
for module in &manual.modules {
for contract in &module.contracts {
for group in &contract.groups {
for entity in &group.entities {
indexed.push(IndexedEntity {
module_id: module.id.clone(),
contract_family: contract.family.clone(),
group_id: group.id.clone(),
entity: entity.clone(),
});
}
}
}
}
Self::new(indexed)
}
pub fn new(entities: Vec<IndexedEntity>) -> Self {
let mut by_ref = HashMap::new();
for indexed in &entities {
let entity_ref = EntityRef {
module: indexed.module_id.as_str().to_string(),
group: indexed.group_id.as_str().to_string(),
category: indexed.entity.category.clone(),
name: indexed.entity.name.clone(),
};
let entity_id = EntityId::new(
indexed.group_id.clone(),
indexed.entity.category.clone(),
indexed.entity.name.clone(),
);
by_ref.insert(entity_ref, entity_id);
}
Self { entities, by_ref }
}
}