Skip to main content

ifc_schedule/
error.rs

1//! Malformed schedule data and traversal refusals.
2//!
3//! These describe a file that states something contradictory, not a failure of
4//! this crate to read it. They are returned alongside results so one bad task
5//! does not hide an otherwise readable schedule.
6
7use ifc_model::EntityId;
8use ifc_schema::SchemaVersion;
9
10pub use crate::sequence::SequenceCycle;
11pub use crate::task::TaskTimeAnomaly;
12
13/// An authored value rejected before it reached the model.
14///
15/// Distinct from the anomalies above: those describe a file already
16/// written, this describes a draft refused so the file never says it.
17#[derive(Debug, Clone, PartialEq, Eq)]
18#[non_exhaustive]
19pub enum ScheduleAuthoringError {
20    /// An attribute value is not valid for its slot.
21    InvalidValue {
22        /// The entity type being authored.
23        entity: &'static str,
24        /// The attribute that failed.
25        attribute: &'static str,
26        /// What was expected instead.
27        expected: &'static str,
28    },
29    /// The model's header declares several schemas; release-bound
30    /// authoring binds to exactly one.
31    MultipleSchemas {
32        /// Number of `FILE_SCHEMA` declarations.
33        schemas: usize,
34    },
35    /// The model's header declares one schema with no bundled table, so no
36    /// layout can be trusted.
37    UnsupportedSchema {
38        /// The `FILE_SCHEMA` token as written.
39        schema: String,
40    },
41    /// The model's release does not declare this entity, such as
42    /// `IfcEvent` or `IfcWorkCalendar` in IFC2X3.
43    EntityNotInSchema {
44        /// The entity type being authored.
45        entity: &'static str,
46        /// The release the model declares.
47        schema: SchemaVersion,
48    },
49    /// A draft supplied a value for an attribute the model's release does
50    /// not declare, such as an IFC2X3 `IfcTask.PredefinedType`. It is
51    /// refused rather than dropped.
52    AuthoringNotInSchema {
53        /// The entity type being authored.
54        entity: &'static str,
55        /// The attribute, by its IFC4 name.
56        attribute: &'static str,
57        /// The release the model declares.
58        schema: SchemaVersion,
59    },
60    /// A draft supplied a value the release's declaration cannot hold, such
61    /// as text where IFC2X3 declares an `IfcDateTimeSelect`, a reference
62    /// where it declares an `IfcTimeMeasure`, or a token outside its
63    /// enumeration.
64    AuthoringValueType {
65        /// The entity type being authored.
66        entity: &'static str,
67        /// The attribute, by its IFC4 name.
68        attribute: &'static str,
69        /// The type the release declares.
70        declared: &'static str,
71        /// The release the model declares.
72        schema: SchemaVersion,
73    },
74    /// The release requires an attribute the call leaves unset, such as the
75    /// IFC2X3 `IfcRoot.OwnerHistory` (#202) or `IfcTask.TaskId`. It is
76    /// refused rather than written as `$`.
77    AuthoringRequired {
78        /// The entity type being authored.
79        entity: &'static str,
80        /// The attribute, by the release's own name.
81        attribute: &'static str,
82        /// The release the model declares.
83        schema: SchemaVersion,
84    },
85    /// A reference, such as a caller-supplied `OwnerHistory`, names an
86    /// entity that is neither in the model nor staged.
87    MissingReference {
88        /// The entity type being authored.
89        entity: &'static str,
90        /// The reference attribute.
91        attribute: &'static str,
92        /// The missing entity.
93        target: EntityId,
94    },
95    /// A reference, such as a caller-supplied `OwnerHistory`, names an
96    /// entity of a type the release does not accept there.
97    WrongReferenceType {
98        /// The entity type being authored.
99        entity: &'static str,
100        /// The reference attribute.
101        attribute: &'static str,
102        /// The referenced entity.
103        target: EntityId,
104        /// Its type.
105        actual: String,
106        /// The type the release accepts.
107        expected: &'static str,
108    },
109}
110
111impl std::fmt::Display for ScheduleAuthoringError {
112    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
113        match self {
114            Self::InvalidValue {
115                entity,
116                attribute,
117                expected,
118            } => write!(f, "{entity}.{attribute}: expected {expected}"),
119            Self::MultipleSchemas { schemas } => write!(
120                f,
121                "the header declares {schemas} schemas; authoring binds to exactly one"
122            ),
123            Self::UnsupportedSchema { schema } => {
124                write!(
125                    f,
126                    "the header declares {schema}, which has no bundled table"
127                )
128            }
129            Self::EntityNotInSchema { entity, schema } => {
130                write!(f, "{entity} is not an entity of {schema:?}")
131            }
132            Self::AuthoringNotInSchema {
133                entity,
134                attribute,
135                schema,
136            } => write!(
137                f,
138                "cannot author {entity}.{attribute}: not defined by {schema:?}"
139            ),
140            Self::AuthoringValueType {
141                entity,
142                attribute,
143                declared,
144                schema,
145            } => write!(
146                f,
147                "cannot author {entity}.{attribute}: {schema:?} declares it {declared}"
148            ),
149            Self::AuthoringRequired {
150                entity,
151                attribute,
152                schema,
153            } => write!(f, "cannot author {entity}: {schema:?} requires {attribute}"),
154            Self::MissingReference {
155                entity,
156                attribute,
157                target,
158            } => write!(f, "{entity}.{attribute} references missing {target}"),
159            Self::WrongReferenceType {
160                entity,
161                attribute,
162                target,
163                actual,
164                expected,
165            } => write!(
166                f,
167                "{entity}.{attribute} references {target} of type {actual}; expected {expected}"
168            ),
169        }
170    }
171}
172
173impl std::error::Error for ScheduleAuthoringError {}
174
175/// Why a schedule read could not answer (#212).
176///
177/// The readers bind the release the model's header declares and find every
178/// attribute by name in its table. A header they cannot bind is refused,
179/// never read through another release's positions.
180#[derive(Debug, Clone, PartialEq, Eq)]
181#[non_exhaustive]
182pub enum ScheduleReadError {
183    /// The header declares several schemas; a read binds to exactly one.
184    MultipleSchemas {
185        /// Number of `FILE_SCHEMA` declarations.
186        schemas: usize,
187    },
188    /// The header declares one schema the readers are not verified against
189    /// (anything but IFC2X3, IFC4 and IFC4X3, so IFC4X1 and IFC4X2 too).
190    UnsupportedSchema {
191        /// The `FILE_SCHEMA` token as written, or the release identifier.
192        schema: String,
193    },
194    /// The sequence graph loops, so no ordering or downstream walk exists.
195    Cycle(SequenceCycle),
196    /// A sequence walk from `start` reached a chain longer than `limit`
197    /// processes ([`MAX_SEQUENCE_DEPTH`](crate::sequence::MAX_SEQUENCE_DEPTH))
198    /// and stopped (#236). The walk is refused rather than returned
199    /// truncated, so a partial answer never reads as a complete one.
200    SequenceDepthExceeded {
201        /// The process the walk started from.
202        start: EntityId,
203        /// The depth budget that was reached.
204        limit: usize,
205    },
206}
207
208impl From<SequenceCycle> for ScheduleReadError {
209    fn from(cycle: SequenceCycle) -> Self {
210        Self::Cycle(cycle)
211    }
212}
213
214impl std::fmt::Display for ScheduleReadError {
215    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
216        match self {
217            Self::MultipleSchemas { schemas } => write!(
218                f,
219                "the header declares {schemas} schemas; a read binds to exactly one"
220            ),
221            Self::UnsupportedSchema { schema } => write!(
222                f,
223                "the header declares {schema}, which the schedule readers are not verified against"
224            ),
225            Self::Cycle(cycle) => write!(
226                f,
227                "the sequence graph returns to {} after {} steps",
228                cycle.repeated,
229                cycle.path.len()
230            ),
231            Self::SequenceDepthExceeded { start, limit } => write!(
232                f,
233                "the sequence walk from {start} exceeds the depth budget of {limit} processes"
234            ),
235        }
236    }
237}
238
239impl std::error::Error for ScheduleReadError {}