1use ifc_model::{Entity, EntityId, Transaction, Value};
26
27use super::CostAuthoringError;
28
29#[derive(Debug, Clone, Copy, PartialEq)]
34#[non_exhaustive]
35pub enum DateTimeValue<'a> {
36 Text(&'a str),
38 Date(CalendarDate),
40 Time(LocalTime),
42 DateAndTime(CalendarDate, LocalTime),
44}
45
46impl Default for DateTimeValue<'_> {
47 fn default() -> Self {
48 Self::Text("")
49 }
50}
51
52impl<'a> From<&'a str> for DateTimeValue<'a> {
53 fn from(text: &'a str) -> Self {
54 Self::Text(text)
55 }
56}
57
58impl From<CalendarDate> for DateTimeValue<'_> {
59 fn from(date: CalendarDate) -> Self {
60 Self::Date(date)
61 }
62}
63
64impl From<LocalTime> for DateTimeValue<'_> {
65 fn from(time: LocalTime) -> Self {
66 Self::Time(time)
67 }
68}
69
70#[derive(Debug, Clone, Copy, PartialEq, Eq)]
72#[non_exhaustive]
73pub struct CalendarDate {
74 pub year: i64,
76 pub month: i64,
78 pub day: i64,
80}
81
82impl CalendarDate {
83 #[must_use]
85 pub const fn new(year: i64, month: i64, day: i64) -> Self {
86 Self { year, month, day }
87 }
88}
89
90#[derive(Debug, Clone, Copy, PartialEq)]
95#[non_exhaustive]
96pub struct LocalTime {
97 pub hour: i64,
99 pub minute: Option<i64>,
101 pub second: Option<f64>,
104}
105
106impl LocalTime {
107 #[must_use]
109 pub const fn new(hour: i64) -> Self {
110 Self {
111 hour,
112 minute: None,
113 second: None,
114 }
115 }
116
117 #[must_use]
119 pub const fn minute(mut self, value: i64) -> Self {
120 self.minute = Some(value);
121 self
122 }
123
124 #[must_use]
126 pub const fn second(mut self, value: f64) -> Self {
127 self.second = Some(value);
128 self
129 }
130}
131
132fn invalid(
133 entity: &'static str,
134 attribute: &'static str,
135 expected: &'static str,
136) -> CostAuthoringError {
137 CostAuthoringError::InvalidValue {
138 entity,
139 attribute,
140 reason: format!("expected {expected}"),
141 }
142}
143
144fn leap_year(year: i64) -> bool {
145 (year % 4 == 0 && year % 100 != 0) || year % 400 == 0
146}
147
148impl CalendarDate {
149 fn check(self) -> Result<(), CostAuthoringError> {
150 const ENTITY: &str = "IFCCALENDARDATE";
151 if !(1..=12).contains(&self.month) {
152 return Err(invalid(ENTITY, "MonthComponent", "a month in 1..=12"));
153 }
154 let last = match self.month {
155 4 | 6 | 9 | 11 => 30,
156 2 if leap_year(self.year) => 29,
157 2 => 28,
158 _ => 31,
159 };
160 if !(1..=last).contains(&self.day) {
161 return Err(invalid(
162 ENTITY,
163 "DayComponent",
164 "a day that exists in the month (IfcValidCalendarDate)",
165 ));
166 }
167 Ok(())
168 }
169
170 fn stage(self, tx: &mut Transaction) -> EntityId {
171 tx.create(Entity::new(
172 "IFCCALENDARDATE",
173 vec![
174 Value::Integer(self.day),
175 Value::Integer(self.month),
176 Value::Integer(self.year),
177 ],
178 ))
179 }
180}
181
182impl LocalTime {
183 fn check(self) -> Result<(), CostAuthoringError> {
184 const ENTITY: &str = "IFCLOCALTIME";
185 if !(0..24).contains(&self.hour) {
186 return Err(invalid(ENTITY, "HourComponent", "an hour in 0..24"));
187 }
188 if self.minute.is_some_and(|m| !(0..=59).contains(&m)) {
189 return Err(invalid(ENTITY, "MinuteComponent", "a minute in 0..=59"));
190 }
191 if let Some(second) = self.second {
192 if !second.is_finite() || !(0.0..60.0).contains(&second) {
193 return Err(invalid(
194 ENTITY,
195 "SecondComponent",
196 "a finite second in 0.0..60.0",
197 ));
198 }
199 if self.minute.is_none() {
200 return Err(invalid(
201 ENTITY,
202 "MinuteComponent",
203 "a minute when a second is given (IfcValidTime)",
204 ));
205 }
206 }
207 Ok(())
208 }
209
210 fn stage(self, tx: &mut Transaction) -> EntityId {
211 tx.create(Entity::new(
212 "IFCLOCALTIME",
213 vec![
214 Value::Integer(self.hour),
215 self.minute.map_or(Value::Null, Value::Integer),
216 self.second.map_or(Value::Null, Value::Real),
217 Value::Null,
218 Value::Null,
219 ],
220 ))
221 }
222}
223
224impl<'a> DateTimeValue<'a> {
225 pub(super) fn check(self) -> Result<(), CostAuthoringError> {
228 match self {
229 Self::Text(_) => Ok(()),
230 Self::Date(date) => date.check(),
231 Self::Time(time) => time.check(),
232 Self::DateAndTime(date, time) => {
233 date.check()?;
234 time.check()
235 }
236 }
237 }
238
239 pub(super) const fn record_type(self) -> Option<&'static str> {
241 match self {
242 Self::Text(_) => None,
243 Self::Date(_) => Some("IFCCALENDARDATE"),
244 Self::Time(_) => Some("IFCLOCALTIME"),
245 Self::DateAndTime(..) => Some("IFCDATEANDTIME"),
246 }
247 }
248
249 pub(super) fn provisional(self, placeholder: EntityId) -> Value {
252 match self {
253 Self::Text(text) => Value::Text(text.into()),
254 _ => Value::Ref(placeholder),
255 }
256 }
257
258 pub(super) fn stage(self, tx: &mut Transaction) -> Option<EntityId> {
261 match self {
262 Self::Text(_) => None,
263 Self::Date(date) => Some(date.stage(tx)),
264 Self::Time(time) => Some(time.stage(tx)),
265 Self::DateAndTime(date, time) => {
266 let date = date.stage(tx);
267 let time = time.stage(tx);
268 Some(tx.create(Entity::new(
269 "IFCDATEANDTIME",
270 vec![Value::Ref(date), Value::Ref(time)],
271 )))
272 }
273 }
274 }
275}
276
277pub(super) const PLACEHOLDERS: [EntityId; 3] = [
280 EntityId(u64::MAX),
281 EntityId(u64::MAX - 1),
282 EntityId(u64::MAX - 2),
283];
284
285pub(super) fn patch(
287 tx: &mut Transaction,
288 record: &mut Entity,
289 dates: &[Option<DateTimeValue<'_>>],
290) {
291 for (date, placeholder) in dates.iter().zip(PLACEHOLDERS) {
292 let Some(staged) = date.and_then(|date| date.stage(tx)) else {
293 continue;
294 };
295 for slot in &mut record.attributes {
296 if *slot == Value::Ref(placeholder) {
297 *slot = Value::Ref(staged);
298 }
299 }
300 }
301}