ifc_lite_landxml/parser/
version.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5pub const LANDXML_10_NAMESPACE: &str = "http://www.landxml.org/schema/LandXML-1.0";
6pub const LANDXML_11_NAMESPACE: &str = "http://www.landxml.org/schema/LandXML-1.1";
7pub const LANDXML_12_NAMESPACE: &str = "http://www.landxml.org/schema/LandXML-1.2";
8
9fn is_known_landxml_version(version: Option<&str>) -> bool {
10    matches!(version, Some("1.0" | "1.1" | "1.2"))
11}
12
13/// Classify a LandXML root by its namespace-selected grammar and declared
14/// version provenance. Producers sometimes pair their grammar namespace with
15/// another known declaration; retain that declaration and let the caller
16/// record an explicit compatibility diagnostic.
17pub fn classify_landxml_version(
18    namespace: Option<&str>,
19    version: Option<&str>,
20) -> crate::LandXmlVersionCapability {
21    use crate::LandXmlVersionCapability as Capability;
22    match namespace {
23        Some(LANDXML_10_NAMESPACE) if version == Some("1.0") => Capability::LandXml10Tin,
24        Some(LANDXML_10_NAMESPACE) if is_known_landxml_version(version) => {
25            Capability::LandXml10VersionMismatch
26        }
27        Some(LANDXML_10_NAMESPACE) => Capability::LandXml10Unsupported,
28        Some(LANDXML_11_NAMESPACE) if version == Some("1.1") => Capability::LandXml11Tin,
29        Some(LANDXML_11_NAMESPACE) if is_known_landxml_version(version) => {
30            Capability::LandXml11VersionMismatch
31        }
32        Some(LANDXML_11_NAMESPACE) => Capability::LandXml11Unsupported,
33        Some(LANDXML_12_NAMESPACE) if version == Some("1.2") => Capability::LandXml12Tin,
34        Some(LANDXML_12_NAMESPACE) if is_known_landxml_version(version) => {
35            Capability::LandXml12VersionMismatch
36        }
37        Some(LANDXML_12_NAMESPACE) => Capability::LandXml12Unsupported,
38        _ => Capability::NotLandXml,
39    }
40}
41
42/// Preserve a producer's known version mismatch as source metadata. The
43/// namespace still selects the grammar used to parse descendant QNames.
44pub(crate) fn compatibility_version_diagnostic(
45    capability: crate::LandXmlVersionCapability,
46    version: &str,
47) -> Option<crate::LandXmlCapabilityDiagnostic> {
48    capability.has_compatibility_version_mismatch().then(|| {
49        crate::LandXmlCapabilityDiagnostic {
50            code: crate::LandXmlCapabilityDiagnosticCode::SchemaVersionMismatch,
51            source_id: None,
52            source_path: "LandXML".to_owned(),
53            message: format!(
54                "{} namespace is parsed as its declared grammar while preserving version=\"{version}\" provenance",
55                capability.schema().expect("mismatch capability has a schema"),
56            ),
57        }
58    })
59}