1use ifc_model::{Entity, EntityId, Transaction, Value};
26
27use crate::error::ScheduleAuthoringError;
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) -> ScheduleAuthoringError {
137 ScheduleAuthoringError::InvalidValue {
138 entity,
139 attribute,
140 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<(), ScheduleAuthoringError> {
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<(), ScheduleAuthoringError> {
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(crate) fn text(self) -> Option<&'a str> {
227 match self {
228 Self::Text(text) => Some(text),
229 _ => None,
230 }
231 }
232
233 pub(crate) fn check(self) -> Result<(), ScheduleAuthoringError> {
236 match self {
237 Self::Text(_) => Ok(()),
238 Self::Date(date) => date.check(),
239 Self::Time(time) => time.check(),
240 Self::DateAndTime(date, time) => {
241 date.check()?;
242 time.check()
243 }
244 }
245 }
246
247 pub(crate) fn provisional(self, placeholder: EntityId) -> Value {
250 match self {
251 Self::Text(text) => Value::Text(text.into()),
252 _ => Value::Ref(placeholder),
253 }
254 }
255
256 pub(crate) fn stage(self, tx: &mut Transaction) -> Option<EntityId> {
259 match self {
260 Self::Text(_) => None,
261 Self::Date(date) => Some(date.stage(tx)),
262 Self::Time(time) => Some(time.stage(tx)),
263 Self::DateAndTime(date, time) => {
264 let date = date.stage(tx);
265 let time = time.stage(tx);
266 Some(tx.create(Entity::new(
267 "IFCDATEANDTIME",
268 vec![Value::Ref(date), Value::Ref(time)],
269 )))
270 }
271 }
272 }
273}
274
275pub(crate) const PLACEHOLDERS: [EntityId; 3] = [
278 EntityId(u64::MAX),
279 EntityId(u64::MAX - 1),
280 EntityId(u64::MAX - 2),
281];
282
283pub(crate) fn patch(
285 tx: &mut Transaction,
286 record: &mut Entity,
287 dates: &[Option<DateTimeValue<'_>>],
288) {
289 for (date, placeholder) in dates.iter().zip(PLACEHOLDERS) {
290 let Some(staged) = date.and_then(|date| date.stage(tx)) else {
291 continue;
292 };
293 for slot in &mut record.attributes {
294 if *slot == Value::Ref(placeholder) {
295 *slot = Value::Ref(staged);
296 }
297 }
298 }
299}