1use std::fmt;
4
5use ifc_model::{EntityId, ModelError};
6
7use crate::SyntaxLocation;
8use thiserror::Error;
9
10#[derive(Debug, Clone, Error)]
12#[non_exhaustive]
13pub enum StepError {
14 #[error("not a STEP physical file: {0}")]
16 NotStep(String),
17
18 #[error("syntax error at byte {offset}: {detail}")]
20 Syntax {
21 offset: usize,
23 detail: String,
25 },
26
27 #[error("entity record without an id at byte {offset}")]
29 MissingEntityId {
30 offset: usize,
32 },
33
34 #[error("syntax error at byte {offset}: REAL `{token}` has no decimal point (ISO 10303-21 requires one)")]
40 RealWithoutDecimalPoint {
41 offset: usize,
43 token: String,
45 },
46
47 #[error("duplicate instance id {id} at byte {offset}")]
53 DuplicateId {
54 id: EntityId,
56 offset: usize,
58 },
59
60 #[error("entity #{entity} slot {slot} contains a non-finite REAL")]
62 NonFiniteReal {
63 entity: EntityId,
65 slot: usize,
67 },
68
69 #[error("io error: {0}")]
71 Io(String),
72}
73
74impl StepError {
75 pub(crate) fn from_step(input: &[u8], error: openbim_step::StepError) -> Self {
82 if error.is_real_without_point() {
83 let span = error.span();
84 if let Some(token) = input.get(span.start..span.end) {
85 return Self::RealWithoutDecimalPoint {
86 offset: span.start,
87 token: String::from_utf8_lossy(token).into_owned(),
88 };
89 }
90 }
91 error.into()
92 }
93}
94
95impl From<openbim_step::StepError> for StepError {
99 fn from(error: openbim_step::StepError) -> Self {
100 if error.is_not_step() {
101 Self::NotStep(error.detail().to_owned())
102 } else {
103 Self::Syntax {
104 offset: error.span().start,
105 detail: error.detail().to_owned(),
106 }
107 }
108 }
109}
110
111impl From<StepError> for ModelError {
112 fn from(error: StepError) -> Self {
113 match error {
114 StepError::NotStep(detail) => ModelError::WrongFormat {
115 expected: "STEP",
116 detail,
117 },
118 StepError::Syntax { offset, detail } => ModelError::Syntax { offset, detail },
119 StepError::MissingEntityId { offset } => ModelError::Syntax {
120 offset,
121 detail: "entity record without an id".into(),
122 },
123 StepError::RealWithoutDecimalPoint { offset, token } => ModelError::Syntax {
124 offset,
125 detail: format!(
126 "REAL `{token}` has no decimal point (ISO 10303-21 requires one; \
127 a lenient read accepts it)"
128 ),
129 },
130 StepError::DuplicateId { id, .. } => ModelError::DuplicateId(id),
131 StepError::NonFiniteReal { entity, slot } => ModelError::Write(format!(
132 "entity #{entity} slot {slot} contains a non-finite REAL"
133 )),
134 StepError::Io(message) => ModelError::Io(message),
135 }
136 }
137}
138
139#[derive(Clone)]
145#[non_exhaustive]
146pub struct ReadFailure {
147 pub error: ModelError,
149 pub bytes: Vec<u8>,
151}
152
153impl ReadFailure {
154 #[must_use]
156 pub fn location(&self) -> Option<SyntaxLocation> {
157 SyntaxLocation::locate(&self.bytes, &self.error)
158 }
159}
160
161impl fmt::Debug for ReadFailure {
162 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
163 formatter
164 .debug_struct("ReadFailure")
165 .field("error", &self.error)
166 .field("bytes", &format_args!("<{} bytes>", self.bytes.len()))
167 .finish()
168 }
169}
170
171impl fmt::Display for ReadFailure {
172 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
173 self.error.fmt(formatter)
174 }
175}
176
177impl std::error::Error for ReadFailure {
178 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
179 self.error.source()
180 }
181}
182
183impl From<ReadFailure> for ModelError {
184 fn from(failure: ReadFailure) -> Self {
185 failure.error
186 }
187}