use ifc_model::{EntityId, ModelError};
use thiserror::Error;
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum StepError {
#[error("not a STEP physical file: {0}")]
NotStep(String),
#[error("syntax error at byte {offset}: {detail}")]
Syntax {
offset: usize,
detail: String,
},
#[error("entity record without an id at byte {offset}")]
MissingEntityId {
offset: usize,
},
#[error("entity #{entity} slot {slot} contains a non-finite REAL")]
NonFiniteReal {
entity: EntityId,
slot: usize,
},
#[error("io error: {0}")]
Io(String),
}
impl From<openbim_step::StepError> for StepError {
fn from(error: openbim_step::StepError) -> Self {
if error.is_not_step() {
Self::NotStep(error.detail().to_owned())
} else {
Self::Syntax {
offset: error.span().start,
detail: error.detail().to_owned(),
}
}
}
}
impl From<StepError> for ModelError {
fn from(error: StepError) -> Self {
match error {
StepError::NotStep(detail) => ModelError::WrongFormat {
expected: "STEP",
detail,
},
StepError::Syntax { offset, detail } => ModelError::Syntax { offset, detail },
StepError::MissingEntityId { offset } => ModelError::Syntax {
offset,
detail: "entity record without an id".into(),
},
StepError::NonFiniteReal { entity, slot } => ModelError::Write(format!(
"entity #{entity} slot {slot} contains a non-finite REAL"
)),
StepError::Io(message) => ModelError::Io(message),
}
}
}