ifc_lite_geometry/representation/
refusal.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
5/// Why the walk declined to enter, or to continue past, a mapped item. All
6/// but the items of an entered level are refused before `enter_map`.
7#[derive(Debug)]
8pub enum WalkRefusal {
9    /// The mapped item is already on the path.
10    Cyclic,
11    /// The depth cap, or a nested map under [`super::NestedMaps::Refuse`].
12    TooDeep { max_depth: u32 },
13    /// The `IfcMappedItem` has no `MappingSource` attribute.
14    MissingSource,
15    /// `MappingSource` does not resolve.
16    UnresolvedSource,
17    /// The map has no `MappedRepresentation` attribute.
18    MissingMappedRepresentation,
19    /// `MappedRepresentation` does not resolve.
20    UnresolvedMappedRepresentation,
21    /// The mapped representation has no `Items` attribute.
22    MissingItems,
23    /// The mapped-level or total Items-member allowance was exhausted.
24    #[non_exhaustive]
25    Budget { max_levels: u32, details: WalkBudgetRefusal },
26    /// The decoder failed on one of the references.
27    Decode(ifc_lite_core::Error),
28}
29
30/// Opaque, cold-allocated work-budget evidence. Its representation is private
31/// so future diagnostic fields do not change visitor matches or common Results.
32#[derive(Debug)]
33pub struct WalkBudgetRefusal(Box<(&'static str, u64)>);
34
35impl WalkBudgetRefusal {
36    pub fn budget(&self) -> &'static str { self.0.0 }
37    pub fn reached(&self) -> u64 { self.0.1 }
38}
39
40// Refusal details must fit within the decoder error this carrier already held.
41const _: () = assert!(
42    std::mem::size_of::<WalkRefusal>() <= std::mem::size_of::<ifc_lite_core::Error>()
43);
44
45impl WalkRefusal {
46    #[cold]
47    #[inline(never)]
48    pub(crate) fn work_budget(max_levels: u32, budget: &'static str, reached: u64) -> Self {
49        Self::Budget { max_levels, details: WalkBudgetRefusal(Box::new((budget, reached))) }
50    }
51
52    /// Convert to a geometry error, preserving historical strict messages for
53    /// existing refusals. `item` is the refused mapped item's id, or the holding
54    /// representation's id for a root-list budget refusal.
55    pub fn into_error(self, item: u32) -> crate::Error {
56        let msg = match self {
57            WalkRefusal::Cyclic => format!("Detected cyclic IfcMappedItem reference at #{item}"),
58            WalkRefusal::TooDeep { max_depth } => format!(
59                "MappedItem nesting exceeded maximum depth of {max_depth} at #{item}"
60            ),
61            WalkRefusal::Budget { max_levels, .. } => format!(
62                "Representation expansion exceeded the work budget of {max_levels} levels or item references at #{item}"
63            ),
64            WalkRefusal::MissingSource => "MappedItem missing MappingSource".to_string(),
65            WalkRefusal::UnresolvedSource => "Failed to resolve MappingSource".to_string(),
66            WalkRefusal::MissingMappedRepresentation => {
67                "RepresentationMap missing MappedRepresentation".to_string()
68            }
69            WalkRefusal::UnresolvedMappedRepresentation => {
70                "Failed to resolve MappedRepresentation".to_string()
71            }
72            WalkRefusal::MissingItems => "Representation missing Items".to_string(),
73            WalkRefusal::Decode(error) => return crate::Error::CoreError(error),
74        };
75        crate::Error::geometry(msg)
76    }
77}