ifc_validate/header.rs
1//! Header well-formedness and the schema a file declares.
2//!
3//! # What survives to here, and what does not
4//!
5//! [`ifc_model::header::Header`] is normalized: it holds a description list,
6//! an author list, a schema token list. A STEP `FILE_NAME` entry with too
7//! many parameters, or with an integer where a string belongs, is a *syntax*
8//! defect -- the codec either recovers from it or fails, and by the time a
9//! `Model` exists the original arity is gone.
10//!
11//! So this module deliberately does not attempt to re-derive header arity
12//! errors. It checks what the normalized header can still answer:
13//!
14//! - is a schema declared at all, and is it one this build can validate?
15//! A release known by name but published without an EXPRESS schema
16//! (`IFC4X3_TC1`, `IFC4X3_ADD1`; [`ifc_schema::UnpublishedRelease`]) is
17//! reported as such, `header.schema.unpublished`, not as unknown (#467).
18//! - is the implementation level the Part 21 value?
19//!
20//! A caller that needs header syntax diagnostics wants them from the codec's
21//! own diagnostic channel, which sees the file. Claiming to check them here
22//! would report "clean" for a file this crate never actually inspected.
23
24use ifc_model::Model;
25use ifc_schema::{SchemaVersion, UnpublishedRelease};
26
27use crate::report::{Finding, FindingDetail, Path, Report};
28
29/// Check the declared schema and implementation level.
30pub fn check(model: &Model, report: &mut Report) {
31 let header = model.header();
32 match header.schema_token() {
33 None => report.push(Finding::error(
34 "header.schema.missing",
35 Path::File,
36 "FILE_SCHEMA declares no schema; the file cannot be validated \
37 against anything",
38 )),
39 Some(token) if SchemaVersion::from_header_token(token).is_some() => {}
40 Some(token) => match UnpublishedRelease::from_header_token(token) {
41 // Tables passed by the caller (`validate`) are the caller's
42 // explicit choice; this finding keeps the report from reading
43 // as a check against the declared release.
44 Some(release) => report.push(
45 Finding::warning(
46 "header.schema.unpublished",
47 Path::File,
48 format!(
49 "schema {token:?} is IFC {} ({}), a release buildingSMART \
50 publishes no EXPRESS schema for; it is not checked against \
51 its own tables, and {} is not assumed to match it",
52 release.version_number(),
53 release.identifier(),
54 release.superseded_by().release_id()
55 ),
56 )
57 .with_detail(FindingDetail::UnpublishedSchema(release)),
58 ),
59 None => report.push(Finding::warning(
60 "header.schema.unknown",
61 Path::File,
62 format!(
63 "schema {token:?} is not one this build recognizes; \
64 structural checks will be skipped"
65 ),
66 )),
67 },
68 }
69
70 // Part 21 fixes this to "2;1". A different value is not fatal -- files
71 // carrying it parse fine -- but it signals a producer that is not
72 // following the standard, which usually means other things are off too.
73 let level = &header.implementation_level;
74 if !level.is_empty() && level != "2;1" {
75 report.push(Finding::warning(
76 "header.implementation_level",
77 Path::File,
78 format!("implementation level is {level:?}; ISO 10303-21 specifies \"2;1\""),
79 ));
80 }
81}
82
83/// The schema version a model declares, if this build recognizes it.
84#[must_use]
85pub fn declared_version(model: &Model) -> Option<SchemaVersion> {
86 model
87 .header()
88 .schema_token()
89 .and_then(SchemaVersion::from_header_token)
90}