ifc_cost/schedule.rs
1//! `IfcCostSchedule` — the document containing cost items.
2//!
3//! # Read by name in the declared release (#212)
4//!
5//! The attribute layout differs between releases, from the EXPRESS sources:
6//!
7//! ```text
8//! IFC2X3_TC1 (13) 0 GlobalId 1 OwnerHistory 2 Name 3 Description
9//! 4 ObjectType 5 SubmittedBy 6 PreparedBy
10//! 7 SubmittedOn (IfcDateTimeSelect) 8 Status
11//! 9 TargetUsers 10 UpdateDate (IfcDateTimeSelect)
12//! 11 ID 12 PredefinedType
13//! IFC4 / IFC4X3 (10) 0 GlobalId 1 OwnerHistory 2 Name 3 Description
14//! 4 ObjectType 5 Identification 6 PredefinedType
15//! 7 Status 8 SubmittedOn (IfcDateTime)
16//! 9 UpdateDate (IfcDateTime)
17//! ```
18//!
19//! Reading the IFC4 positions from an IFC2X3 record returns `PreparedBy`
20//! as the predefined type and the `SubmittedOn` record as the status. Every
21//! accessor here looks its attribute up by name in the model's declared
22//! release instead. `Identification` is IFC2X3's `ID`: the IFC4 ADD2 TC1
23//! documentation of `IfcCostSchedule` states "Attribute ID renamed to
24//! Identification and promoted to supertype IfcControl".
25
26use ifc_model::{Entity, EntityId, Value};
27
28use crate::error::CostError;
29use crate::release::{AuthoredDateTime, ReadRelease};
30use crate::SchemaVersion;
31
32const ENTITY: &str = "IFCCOSTSCHEDULE";
33
34/// A borrowed view of an `IfcCostSchedule` entity, bound to a release.
35#[derive(Debug, Clone, Copy)]
36pub struct CostSchedule<'m> {
37 id: EntityId,
38 entity: &'m Entity,
39 release: ReadRelease,
40}
41
42impl<'m> CostSchedule<'m> {
43 /// Wrap an entity known to be an `IfcCostSchedule`, read against
44 /// `release`.
45 ///
46 /// [`crate::CostView::schedules`] binds the model's declared release;
47 /// use this when the release is known some other way.
48 ///
49 /// # Errors
50 ///
51 /// [`CostError::UnsupportedSchema`] for a release the cost readers are
52 /// not verified against (IFC4X1, IFC4X2).
53 pub fn new(
54 id: EntityId,
55 entity: &'m Entity,
56 release: SchemaVersion,
57 ) -> Result<Self, CostError> {
58 Ok(Self::bound(id, entity, ReadRelease::of_version(release)?))
59 }
60
61 pub(crate) const fn bound(id: EntityId, entity: &'m Entity, release: ReadRelease) -> Self {
62 Self {
63 id,
64 entity,
65 release,
66 }
67 }
68
69 /// The entity id in the file.
70 pub fn id(&self) -> EntityId {
71 self.id
72 }
73
74 /// The release this schedule is read against.
75 pub fn release(&self) -> SchemaVersion {
76 self.release.version()
77 }
78
79 fn text(&self, attribute: &str) -> Option<&'m str> {
80 self.release.text(ENTITY, self.entity, attribute)
81 }
82
83 /// The `GlobalId` string.
84 pub fn global_id(&self) -> Option<&'m str> {
85 self.text("GlobalId")
86 }
87
88 /// The schedule name.
89 pub fn name(&self) -> Option<&'m str> {
90 self.text("Name")
91 }
92
93 /// The user-facing identification code: `Identification`, or IFC2X3's
94 /// `ID`, which IFC4 renamed to it.
95 pub fn identification(&self) -> Option<&'m str> {
96 match self.release.version() {
97 SchemaVersion::Ifc2x3 => self.text("ID"),
98 _ => self.text("Identification"),
99 }
100 }
101
102 /// The authored status label, e.g. `Draft`.
103 pub fn status(&self) -> Option<&'m str> {
104 self.text("Status")
105 }
106
107 /// `SubmittedOn`, as authored: IFC4/IFC4X3 ISO 8601 text or an IFC2X3
108 /// date record.
109 pub fn submitted_on(&self) -> Option<AuthoredDateTime<'m>> {
110 self.release.date_time(ENTITY, self.entity, "SubmittedOn")
111 }
112
113 /// `UpdateDate`, as authored: IFC4/IFC4X3 ISO 8601 text or an IFC2X3
114 /// date record.
115 pub fn update_date(&self) -> Option<AuthoredDateTime<'m>> {
116 self.release.date_time(ENTITY, self.entity, "UpdateDate")
117 }
118
119 /// The predefined type token, e.g. `BUDGET`, without its dots.
120 pub fn predefined_type(&self) -> Option<&'m str> {
121 match self.release.value(ENTITY, self.entity, "PredefinedType")? {
122 Value::Enum(e) => Some(e),
123 _ => None,
124 }
125 }
126}