ifc_validate/error.rs
1//! Why validation could not run.
2//!
3//! Distinct from a [`Finding`](crate::report::Finding): a finding says the
4//! *file* is wrong, this says the *validator* could not proceed. Conflating
5//! them would let "we could not check this" be read as "this is fine".
6
7use std::fmt;
8
9/// Validation could not be performed.
10#[derive(Debug, Clone, PartialEq, Eq)]
11#[non_exhaustive]
12pub enum ValidateError {
13 /// The `FILE_SCHEMA` token is not a schema version this crate recognises.
14 ///
15 /// Carries the token as written, so the caller can report it verbatim.
16 UnknownSchema(String),
17 /// A recognised schema version for which this build bundles no tables.
18 ///
19 /// Separate from [`Self::UnknownSchema`] because the remedy differs: an
20 /// unknown token usually means a malformed or non-IFC file, while an
21 /// unbundled schema is a known gap in this build. Reporting both as
22 /// "unknown" would hide which one a user is hitting.
23 UnbundledSchema(String),
24 /// A release known by name whose normative EXPRESS schema is not
25 /// published (`IFC4X3_TC1`, `IFC4X3_ADD1`; #467), so no build has its
26 /// tables. Carries the token as written.
27 ///
28 /// Not [`Self::UnknownSchema`]: the token is a real IFC release, and it
29 /// is not read with a neighbouring release's tables by assumption. A
30 /// caller that accepts that risk passes the tables it chooses to
31 /// [`crate::validate`] itself; the report then carries the
32 /// `header.schema.unpublished` warning.
33 UnpublishedSchema(String),
34 /// The file declares no schema at all.
35 NoSchemaDeclared,
36}
37
38impl fmt::Display for ValidateError {
39 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
40 match self {
41 Self::UnknownSchema(token) => {
42 write!(f, "unrecognised schema token {token:?}")
43 }
44 Self::UnbundledSchema(token) => {
45 write!(f, "no schema tables bundled for {token:?}")
46 }
47 Self::UnpublishedSchema(token) => write!(
48 f,
49 "{token:?} is a release buildingSMART publishes no EXPRESS schema for"
50 ),
51 Self::NoSchemaDeclared => f.write_str("the file declares no schema"),
52 }
53 }
54}
55
56impl std::error::Error for ValidateError {}
57
58#[cfg(test)]
59mod tests {
60 use super::*;
61
62 /// The two refusals must not read alike: one is a bad token, the other a
63 /// known schema this build cannot check.
64 #[test]
65 fn the_two_schema_refusals_are_distinguishable() {
66 let unknown = ValidateError::UnknownSchema("STEP".into()).to_string();
67 let unbundled = ValidateError::UnbundledSchema("IFC_FUTURE_PROFILE".into()).to_string();
68 assert_ne!(unknown, unbundled);
69 assert!(unbundled.contains("IFC_FUTURE_PROFILE"));
70 assert!(unknown.contains("STEP"));
71 }
72}