use crate::assignment::{validate_assignment, AssociationSchema};
use crate::view::{
borrowed_entity, optional_text, required_ref, required_refs, required_text, ClassificationView,
};
use crate::ClassificationResult;
use ifc_model::EntityId;
borrowed_entity!(LibraryAssignment, "IFCRELASSOCIATESLIBRARY");
impl<'m> LibraryAssignment<'m> {
pub fn global_id(self) -> ClassificationResult<&'m str> {
required_text(
"IFCRELASSOCIATESLIBRARY",
self.id(),
self.entity(),
self.text_slot("GlobalId")?,
"GlobalId",
)
}
pub fn name(self) -> ClassificationResult<Option<&'m str>> {
optional_text(
"IFCRELASSOCIATESLIBRARY",
self.id(),
self.entity(),
self.text_slot("Name")?,
"Name",
)
}
pub fn description(self) -> ClassificationResult<Option<&'m str>> {
optional_text(
"IFCRELASSOCIATESLIBRARY",
self.id(),
self.entity(),
self.text_slot("Description")?,
"Description",
)
}
pub fn related_object_ids(self) -> ClassificationResult<Vec<EntityId>> {
required_refs(
"IFCRELASSOCIATESLIBRARY",
self.id(),
self.entity(),
self.slot("RelatedObjects")?,
"RelatedObjects",
)
}
pub fn relating_library_id(self) -> ClassificationResult<EntityId> {
required_ref(
"IFCRELASSOCIATESLIBRARY",
self.id(),
self.entity(),
self.slot("RelatingLibrary")?,
"RelatingLibrary",
)
}
}
impl<'m> ClassificationView<'m> {
pub fn library_assignments(self) -> impl Iterator<Item = LibraryAssignment<'m>> + 'm {
self.model()
.of_type("IFCRELASSOCIATESLIBRARY")
.map(move |(id, e)| LibraryAssignment::from_known(id, e, self.release()))
}
pub fn library_assignments_for(
self,
object: EntityId,
) -> ClassificationResult<Vec<LibraryAssignment<'m>>> {
if self.model().get(object).is_none() {
return Err(crate::ClassificationError::UnknownEntity { id: object });
}
let mut out = Vec::new();
for assignment in self.library_assignments() {
let related = assignment.related_object_ids()?;
if related.contains(&object) {
let target = assignment.relating_library_id()?;
validate_assignment(
self,
assignment.id(),
&related,
target,
AssociationSchema {
relation: "IFCRELASSOCIATESLIBRARY",
target_attribute: "RelatingLibrary",
},
)?;
out.push(assignment);
}
}
Ok(out)
}
}