ifc_schedule/authoring/
procedure.rs1use ifc_model::guid::Guid;
8use ifc_model::{Entity, EntityId, Transaction, Value};
9
10use super::{optional_text, ScheduleAuthoringError, ScheduleAuthoringResult};
11const PROCEDURE_KIND: &[&str] = &[
16 "ADVICE_CAUTION",
17 "ADVICE_NOTE",
18 "ADVICE_WARNING",
19 "CALIBRATION",
20 "DIAGNOSTIC",
21 "SHUTDOWN",
22 "STARTUP",
23 "USERDEFINED",
24 "NOTDEFINED",
25];
26
27#[derive(Debug, Clone, Copy, Default)]
29#[non_exhaustive]
30pub struct ProcedureDraft<'a> {
31 pub global_id: &'a str,
33 pub name: Option<&'a str>,
35 pub description: Option<&'a str>,
37 pub object_type: Option<&'a str>,
39 pub identification: Option<&'a str>,
41 pub long_description: Option<&'a str>,
43 pub predefined_type: Option<&'a str>,
45 pub user_defined_procedure_type: Option<&'a str>,
51}
52
53pub fn create_procedure(
72 tx: &mut Transaction,
73 draft: ProcedureDraft<'_>,
74) -> ScheduleAuthoringResult<EntityId> {
75 check(&draft)?;
76 if draft.user_defined_procedure_type.is_some() {
77 return Err(ScheduleAuthoringError::InvalidValue {
78 entity: ENTITY,
79 attribute: "UserDefinedProcedureType",
80 expected: "no value: IFC4 and IFC4X3 declare no UserDefinedProcedureType",
81 });
82 }
83 let mut attributes = vec![Value::Null; 8];
84 attributes[0] = Value::Text(draft.global_id.into());
85 attributes[2] = optional_text(draft.name);
86 attributes[3] = optional_text(draft.description);
87 attributes[4] = optional_text(draft.object_type);
88 attributes[5] = optional_text(draft.identification);
89 attributes[6] = optional_text(draft.long_description);
90 attributes[7] = draft
91 .predefined_type
92 .map_or(Value::Null, |t| Value::Enum(t.into()));
93 Ok(tx.create(Entity::new(ENTITY, attributes)))
94}
95
96pub(super) const ENTITY: &str = "IFCPROCEDURE";
98
99pub(super) fn check(draft: &ProcedureDraft<'_>) -> ScheduleAuthoringResult<()> {
101 if Guid::parse(draft.global_id).is_none() {
102 return Err(ScheduleAuthoringError::InvalidValue {
103 entity: ENTITY,
104 attribute: "GlobalId",
105 expected: "an IFC compressed GUID",
106 });
107 }
108 if draft.name.is_none_or(|value| value.trim().is_empty()) {
109 return Err(ScheduleAuthoringError::InvalidValue {
110 entity: ENTITY,
111 attribute: "Name",
112 expected: "a name, required by HasName",
113 });
114 }
115 if let Some(token) = draft.predefined_type {
116 if !PROCEDURE_KIND.contains(&token) {
117 return Err(ScheduleAuthoringError::InvalidValue {
118 entity: ENTITY,
119 attribute: "PredefinedType",
120 expected: "a token of IfcProcedureTypeEnum",
121 });
122 }
123 if token == "USERDEFINED"
124 && draft
125 .object_type
126 .is_none_or(|value| value.trim().is_empty())
127 {
128 return Err(ScheduleAuthoringError::InvalidValue {
129 entity: ENTITY,
130 attribute: "ObjectType",
131 expected: "a name, required by USERDEFINED",
132 });
133 }
134 }
135 Ok(())
136}