ifc_schedule/authoring/
timing.rs1use ifc_model::{Edit, Entity, EntityId, Model, Transaction, Value};
7
8use super::{optional_text, ScheduleAuthoringResult};
9use crate::error::ScheduleAuthoringError;
10use crate::task::definition::time_slot;
11
12#[derive(Debug, Clone, Copy, Default)]
18#[non_exhaustive]
19pub struct TaskTimeDraft<'a> {
20 pub name: Option<&'a str>,
22 pub duration_type: Option<&'a str>,
24 pub schedule_duration: Option<&'a str>,
26 pub schedule_start: Option<&'a str>,
28 pub schedule_finish: Option<&'a str>,
30 pub actual_start: Option<&'a str>,
32 pub actual_finish: Option<&'a str>,
34 pub is_critical: Option<bool>,
36 pub completion: Option<f64>,
38}
39
40fn task_time_attributes(
46 entity: &'static str,
47 draft: TaskTimeDraft<'_>,
48) -> ScheduleAuthoringResult<Vec<Value>> {
49 if let Some(completion) = draft.completion {
50 if !completion.is_finite() || !(0.0..=100.0).contains(&completion) {
51 return Err(ScheduleAuthoringError::InvalidValue {
52 entity,
53 attribute: "Completion",
54 expected: "a percentage in 0..=100",
55 });
56 }
57 }
58 if let Some(kind) = draft.duration_type {
59 if !["WORKTIME", "ELAPSEDTIME", "NOTDEFINED"]
60 .iter()
61 .any(|known| known.eq_ignore_ascii_case(kind))
62 {
63 return Err(ScheduleAuthoringError::InvalidValue {
64 entity,
65 attribute: "DurationType",
66 expected: "WORKTIME, ELAPSEDTIME or NOTDEFINED",
67 });
68 }
69 }
70 let mut attributes = vec![Value::Null; time_slot::COMPLETION + 1];
71 attributes[0] = optional_text(draft.name);
72 attributes[time_slot::DURATION_TYPE] = draft
73 .duration_type
74 .map_or(Value::Null, |t| Value::Enum(t.into()));
75 attributes[time_slot::SCHEDULE_DURATION] = optional_text(draft.schedule_duration);
76 attributes[time_slot::SCHEDULE_START] = optional_text(draft.schedule_start);
77 attributes[time_slot::SCHEDULE_FINISH] = optional_text(draft.schedule_finish);
78 attributes[time_slot::ACTUAL_START] = optional_text(draft.actual_start);
79 attributes[time_slot::ACTUAL_FINISH] = optional_text(draft.actual_finish);
80 attributes[time_slot::IS_CRITICAL] = draft.is_critical.map_or(Value::Null, Value::Bool);
81 attributes[time_slot::COMPLETION] = draft.completion.map_or(Value::Null, Value::Real);
82 Ok(attributes)
83}
84
85pub fn create_task_time(
91 tx: &mut Transaction,
92 draft: TaskTimeDraft<'_>,
93) -> ScheduleAuthoringResult<EntityId> {
94 let attributes = task_time_attributes("IFCTASKTIME", draft)?;
95 Ok(tx.create(Entity::new("IFCTASKTIME", attributes)))
96}
97
98pub fn create_task_time_recurring(
117 tx: &mut Transaction,
118 model: &Model,
119 draft: TaskTimeDraft<'_>,
120 recurrence: EntityId,
121) -> ScheduleAuthoringResult<EntityId> {
122 const ENTITY: &str = "IFCTASKTIMERECURRING";
123 const RECURRENCE_SLOT: usize = 20;
124 crate::release::bind(model)?.require_entity(ENTITY)?;
125 let pattern_is_valid = tx
126 .edits()
127 .iter()
128 .rev()
129 .find_map(|edit| match edit {
130 Edit::Create { id, entity } if *id == recurrence => {
131 Some(entity.type_name.as_ref().to_owned())
132 }
133 _ => None,
134 })
135 .or_else(|| {
136 model
137 .get(recurrence)
138 .map(|entity| entity.type_name.as_ref().to_owned())
139 })
140 .is_some_and(|name| name.eq_ignore_ascii_case("IFCRECURRENCEPATTERN"));
141 if !pattern_is_valid {
142 return Err(ScheduleAuthoringError::InvalidValue {
143 entity: ENTITY,
144 attribute: "Recurrence",
145 expected: "an IfcRecurrencePattern",
146 });
147 }
148 let mut attributes = task_time_attributes(ENTITY, draft)?;
149 attributes.resize(RECURRENCE_SLOT + 1, Value::Null);
150 attributes[RECURRENCE_SLOT] = Value::Ref(recurrence);
151 Ok(tx.create(Entity::new(ENTITY, attributes)))
152}
153
154pub fn create_time_period(
170 tx: &mut Transaction,
171 start_time: &str,
172 end_time: &str,
173) -> ScheduleAuthoringResult<EntityId> {
174 const ENTITY: &str = "IFCTIMEPERIOD";
175 for (attribute, value) in [("StartTime", start_time), ("EndTime", end_time)] {
176 if value.trim().is_empty() {
177 return Err(ScheduleAuthoringError::InvalidValue {
178 entity: ENTITY,
179 attribute,
180 expected: "a non-empty ISO 8601 time",
181 });
182 }
183 }
184 Ok(tx.create(Entity::new(
185 ENTITY,
186 vec![Value::Text(start_time.into()), Value::Text(end_time.into())],
187 )))
188}
189
190pub fn create_time_period_in(
202 tx: &mut Transaction,
203 model: &Model,
204 start_time: &str,
205 end_time: &str,
206) -> ScheduleAuthoringResult<EntityId> {
207 crate::release::bind(model)?.require_entity("IFCTIMEPERIOD")?;
208 create_time_period(tx, start_time, end_time)
209}