ifc_validate/express/error.rs
1//! Why EXPRESS rule text could not be loaded.
2
3use std::fmt;
4
5/// A line in the EXPRESS source, for messages.
6#[derive(Debug, Clone, Copy, PartialEq, Eq)]
7pub(crate) struct SourcePos {
8 /// One-based line number.
9 pub(crate) line: u32,
10}
11
12/// The EXPRESS source could not be turned into evaluable rules.
13///
14/// Distinct from a finding: this is about the rule text a caller supplied,
15/// not about any model.
16#[derive(Debug, Clone, PartialEq, Eq)]
17#[non_exhaustive]
18pub enum ExpressError {
19 /// The source is not EXPRESS this reader understands.
20 Syntax {
21 /// Where the reader stopped.
22 line: u32,
23 /// What it expected or found.
24 message: String,
25 },
26 /// The source names a schema other than the tables it was paired with,
27 /// e.g. `IFC4.exp` supplied for an IFC4X3 model.
28 SchemaMismatch {
29 /// The `SCHEMA` name in the source.
30 source: String,
31 /// The name of the bundled tables.
32 tables: String,
33 },
34 /// The source parses, but disagrees with the paired tables about an
35 /// entity's attributes, so slots read through it would be wrong.
36 TableMismatch {
37 /// The entity whose layout differs.
38 entity: String,
39 /// How it differs.
40 detail: String,
41 },
42 /// An identifier in a rule or function names nothing in scope.
43 Unresolved {
44 /// The declaration holding the expression (`IfcWall.WR1`, a
45 /// function name).
46 context: String,
47 /// The identifier.
48 name: String,
49 },
50}
51
52impl ExpressError {
53 pub(crate) fn syntax(pos: SourcePos, message: impl Into<String>) -> Self {
54 Self::Syntax {
55 line: pos.line,
56 message: message.into(),
57 }
58 }
59}
60
61impl fmt::Display for ExpressError {
62 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
63 match self {
64 Self::Syntax { line, message } => write!(f, "EXPRESS line {line}: {message}"),
65 Self::SchemaMismatch { source, tables } => write!(
66 f,
67 "the EXPRESS source declares schema {source}, the tables are {tables}"
68 ),
69 Self::TableMismatch { entity, detail } => {
70 write!(
71 f,
72 "the EXPRESS source and the tables disagree on {entity}: {detail}"
73 )
74 }
75 Self::Unresolved { context, name } => {
76 write!(f, "{context}: `{name}` names nothing in scope")
77 }
78 }
79 }
80}
81
82impl std::error::Error for ExpressError {}