ifc_lite_geometry/placement/
validated_chain.rs1use super::chain_walk::{climb, Climb};
5use super::validate::check_frames;
6use super::{unsupported, ChainPolicy, Link, PlacementResult};
7use ifc_lite_core::{execution_owner::ExecutionIdentity, EntityDecoder};
8use rustc_hash::FxHashMap;
9use smallvec::SmallVec;
10
11pub struct ChainValidator<'decoder, 'source> {
17 decoder: &'decoder mut EntityDecoder<'source>,
18 policy: ChainPolicy,
19 identity: ExecutionIdentity,
20 verified: FxHashMap<u32, u32>,
21}
22
23impl<'decoder, 'source> ChainValidator<'decoder, 'source> {
24 pub fn new(decoder: &'decoder mut EntityDecoder<'source>, policy: ChainPolicy) -> Self {
25 let identity = decoder.execution_identity();
26 Self {
27 decoder,
28 policy,
29 identity,
30 verified: FxHashMap::default(),
31 }
32 }
33
34 pub fn decoder(&mut self) -> &mut EntityDecoder<'source> {
38 self.decoder
39 }
40
41 pub fn validate(&mut self, id: u32) -> PlacementResult<(u32, SmallVec<[Link; 8]>)> {
46 let identity = self.decoder.execution_identity();
47 if identity != self.identity {
48 self.verified.clear();
49 self.identity = identity;
50 }
51 if let Some(root) = self.verified.get(&id) {
52 return Ok((*root, SmallVec::new()));
53 }
54 let start = self.decoder.decode_by_id(id)?;
55 let mut climbed = Climb::new();
56 climb(
57 &mut climbed,
58 &start,
59 self.decoder,
60 false,
61 false,
62 Some(&self.verified),
63 )?;
64 if let Some((at, found)) = climbed.stopped_at {
65 return Err(unsupported(self.decoder, at, found));
66 }
67 check_frames(&climbed.frames, self.decoder, &self.policy)?;
68 let root = climbed.verified_root.unwrap_or_else(|| {
70 climbed
71 .frames
72 .last()
73 .expect("nonempty placement chain")
74 .entity
75 .id
76 });
77 let links: SmallVec<[Link; 8]> = climbed
78 .frames
79 .iter()
80 .map(|frame| Link {
81 id: frame.entity.id,
82 kind: frame.kind,
83 })
84 .collect();
85 for link in &links {
86 self.verified.insert(link.id, root);
87 }
88 Ok((root, links))
89 }
90}