Skip to main content

ifc_resource/actor/
organization.rs

1//! `IfcOrganization` and `IfcOrganizationRelationship` — bounded
2//! organization projections (IFC2X3, IFC4, IFC4X3).
3
4use ifc_model::EntityId;
5
6use crate::actor::role::ActorRole;
7use crate::error::ResourceResult;
8use crate::view::{Record, ResourceView};
9
10/// A borrowed, schema-resolved `IfcOrganization` projection.
11#[derive(Debug, Clone, Copy)]
12pub struct Organization<'m, 's> {
13    record: Record<'m, 's>,
14}
15
16impl<'m, 's> Organization<'m, 's> {
17    pub(crate) fn from_record(record: Record<'m, 's>) -> ResourceResult<Self> {
18        Ok(Self { record })
19    }
20
21    /// The entity id of the projected `IfcOrganization`.
22    #[must_use]
23    pub fn id(&self) -> EntityId {
24        self.record.id
25    }
26
27    /// The `Identification` attribute, when authored. Under IFC2X3 this
28    /// reads `Id`, the `IfcIdentifier` IFC2X3 declares in the same position.
29    pub fn identification(&self) -> ResourceResult<Option<&'m str>> {
30        if self.record.is_ifc2x3() {
31            self.record.optional_text("Id")
32        } else {
33            self.record.optional_text("Identification")
34        }
35    }
36
37    /// The `Name` attribute.
38    pub fn name(&self) -> ResourceResult<&'m str> {
39        self.record.required_text("Name")
40    }
41
42    /// The `Description` attribute, when authored.
43    pub fn description(&self) -> ResourceResult<Option<&'m str>> {
44        self.record.optional_text("Description")
45    }
46
47    /// The `Roles` attribute: entity references, when authored.
48    pub fn role_ids(&self) -> ResourceResult<Vec<EntityId>> {
49        self.record.refs("Roles", "IfcActorRole", 1, true, false)
50    }
51
52    /// The `Roles` attribute, resolved to projected `IfcActorRole` values.
53    pub fn roles(&self) -> ResourceResult<Vec<ActorRole<'m, 's>>> {
54        self.role_ids()?
55            .into_iter()
56            .map(|id| {
57                Record::new(self.record.model, self.record.schema, id, "IfcActorRole")
58                    .and_then(ActorRole::from_record)
59            })
60            .collect()
61    }
62
63    /// The `Addresses` attribute, when authored.
64    pub fn address_ids(&self) -> ResourceResult<Vec<EntityId>> {
65        self.record.refs("Addresses", "IfcAddress", 1, true, false)
66    }
67}
68
69/// Borrowed `IfcOrganizationRelationship` projection: a named relationship
70/// between one relating organization and one or more related organizations.
71#[derive(Debug, Clone, Copy)]
72pub struct OrganizationRelationship<'m, 's> {
73    record: Record<'m, 's>,
74}
75
76impl<'m, 's> OrganizationRelationship<'m, 's> {
77    pub(crate) fn from_record(record: Record<'m, 's>) -> ResourceResult<Self> {
78        Ok(Self { record })
79    }
80
81    /// The entity id of the projected `IfcOrganizationRelationship`.
82    #[must_use]
83    pub fn id(&self) -> EntityId {
84        self.record.id
85    }
86
87    /// The `Name` attribute, when authored.
88    pub fn name(&self) -> ResourceResult<Option<&'m str>> {
89        self.record.optional_text("Name")
90    }
91
92    /// The `Description` attribute, when authored.
93    pub fn description(&self) -> ResourceResult<Option<&'m str>> {
94        self.record.optional_text("Description")
95    }
96
97    /// The `RelatingOrganization` attribute.
98    pub fn relating_organization(&self) -> ResourceResult<EntityId> {
99        self.record
100            .required_ref("RelatingOrganization", "IfcOrganization")
101    }
102
103    /// The `RelatedOrganizations` attribute.
104    pub fn related_organizations(&self) -> ResourceResult<Vec<EntityId>> {
105        self.record
106            .refs("RelatedOrganizations", "IfcOrganization", 1, false, true)
107    }
108}
109
110impl<'m, 's> ResourceView<'m, 's> {
111    /// Projects an `IfcOrganizationRelationship` by entity id.
112    pub fn organization_relationship(
113        &self,
114        id: EntityId,
115    ) -> ResourceResult<OrganizationRelationship<'m, 's>> {
116        OrganizationRelationship::from_record(self.record(id, "IfcOrganizationRelationship")?)
117    }
118}