use ifc_model::guid::Guid;
use ifc_model::{Entity, EntityId, Transaction, Value};
use super::{optional_text, ScheduleAuthoringError, ScheduleAuthoringResult};
const PROCEDURE_KIND: &[&str] = &[
"ADVICE_CAUTION",
"ADVICE_NOTE",
"ADVICE_WARNING",
"CALIBRATION",
"DIAGNOSTIC",
"SHUTDOWN",
"STARTUP",
"USERDEFINED",
"NOTDEFINED",
];
#[derive(Debug, Clone, Copy, Default)]
pub struct ProcedureDraft<'a> {
pub global_id: &'a str,
pub name: Option<&'a str>,
pub description: Option<&'a str>,
pub object_type: Option<&'a str>,
pub identification: Option<&'a str>,
pub long_description: Option<&'a str>,
pub predefined_type: Option<&'a str>,
}
pub fn create_procedure(
tx: &mut Transaction,
draft: ProcedureDraft<'_>,
) -> ScheduleAuthoringResult<EntityId> {
check(&draft)?;
let mut attributes = vec![Value::Null; 8];
attributes[0] = Value::Text(draft.global_id.into());
attributes[2] = optional_text(draft.name);
attributes[3] = optional_text(draft.description);
attributes[4] = optional_text(draft.object_type);
attributes[5] = optional_text(draft.identification);
attributes[6] = optional_text(draft.long_description);
attributes[7] = draft
.predefined_type
.map_or(Value::Null, |t| Value::Enum(t.into()));
Ok(tx.create(Entity::new(ENTITY, attributes)))
}
pub(super) const ENTITY: &str = "IFCPROCEDURE";
pub(super) fn check(draft: &ProcedureDraft<'_>) -> ScheduleAuthoringResult<()> {
if Guid::parse(draft.global_id).is_none() {
return Err(ScheduleAuthoringError::InvalidValue {
entity: ENTITY,
attribute: "GlobalId",
expected: "an IFC compressed GUID",
});
}
if draft.name.is_none_or(|value| value.trim().is_empty()) {
return Err(ScheduleAuthoringError::InvalidValue {
entity: ENTITY,
attribute: "Name",
expected: "a name, required by HasName",
});
}
if let Some(token) = draft.predefined_type {
if !PROCEDURE_KIND.contains(&token) {
return Err(ScheduleAuthoringError::InvalidValue {
entity: ENTITY,
attribute: "PredefinedType",
expected: "a token of IfcProcedureTypeEnum",
});
}
if token == "USERDEFINED"
&& draft
.object_type
.is_none_or(|value| value.trim().is_empty())
{
return Err(ScheduleAuthoringError::InvalidValue {
entity: ENTITY,
attribute: "ObjectType",
expected: "a name, required by USERDEFINED",
});
}
}
Ok(())
}