qualia_core_db/bundle/stream/plan/
readiness.rs1use super::inventory::{EntryFamily, Inventory, Obligation, MAX_PLAN_ENTRIES};
4#[derive(Clone, Copy, Debug, PartialEq, Eq)]
5pub enum Readiness {
6 Ready,
7 Incomplete { missing_mandatory: u32 },
8 Opaque,
9 Refused,
10}
11pub fn evaluate(
12 inventory: &Inventory<'_>,
13 entry_id: u32,
14 expected_family: Option<EntryFamily>,
15 present: &[u32],
16) -> Readiness {
17 let Some(root) = inventory.entry(entry_id) else {
18 return Readiness::Refused;
19 };
20 if let Some(family) = expected_family {
21 if family != root.family() {
22 return Readiness::Refused;
23 }
24 }
25 if root.family() == EntryFamily::Opaque {
26 return match root.obligation() {
27 Obligation::Optional => Readiness::Opaque,
28 Obligation::Mandatory => Readiness::Refused,
29 };
30 }
31 let mut stack = [0u32; MAX_PLAN_ENTRIES];
32 let mut sp = 0usize;
33 let mut seen = [false; MAX_PLAN_ENTRIES];
34 let mut missing: Option<u32> = None;
35 match push(inventory, &mut stack, &mut sp, &mut seen, entry_id) {
36 Push::Ok => {}
37 Push::Unknown(id) => {
38 return Readiness::Incomplete {
39 missing_mandatory: id,
40 }
41 }
42 Push::Full => return Readiness::Refused,
43 }
44 while sp > 0 {
45 sp -= 1;
46 let id = stack[sp];
47 let Some(entry) = inventory.entry(id) else {
48 return Readiness::Refused;
49 };
50 if entry.family() == EntryFamily::Opaque {
51 return Readiness::Refused;
52 }
53 for edge in inventory.edges() {
54 if edge.from_id() != id || edge.obligation() != Obligation::Mandatory {
55 continue;
56 }
57 let dep = edge.to_id();
58 match inventory.entry(dep) {
59 None => {
60 return Readiness::Incomplete {
61 missing_mandatory: dep,
62 }
63 }
64 Some(dep_entry) if dep_entry.family() == EntryFamily::Opaque => {
65 return Readiness::Refused;
66 }
67 Some(_) if !contains(present, dep) => {
68 missing = Some(min_id(missing, dep));
69 }
70 Some(_) => match push(inventory, &mut stack, &mut sp, &mut seen, dep) {
71 Push::Ok => {}
72 Push::Unknown(id) => {
73 return Readiness::Incomplete {
74 missing_mandatory: id,
75 };
76 }
77 Push::Full => return Readiness::Refused,
78 },
79 }
80 }
81 }
82 if let Some(missing_mandatory) = missing {
83 return Readiness::Incomplete { missing_mandatory };
84 }
85 if !contains(present, entry_id) {
86 return match root.obligation() {
87 Obligation::Mandatory => Readiness::Incomplete {
88 missing_mandatory: entry_id,
89 },
90 Obligation::Optional => Readiness::Opaque,
91 };
92 }
93 Readiness::Ready
94}
95enum Push {
96 Ok,
97 Unknown(u32),
98 Full,
99}
100fn push(
101 inventory: &Inventory<'_>,
102 stack: &mut [u32; MAX_PLAN_ENTRIES],
103 sp: &mut usize,
104 seen: &mut [bool; MAX_PLAN_ENTRIES],
105 id: u32,
106) -> Push {
107 let Some(idx) = inventory.index_of(id) else {
108 return Push::Unknown(id);
109 };
110 if seen[idx] {
111 return Push::Ok;
112 }
113 if *sp >= MAX_PLAN_ENTRIES {
114 return Push::Full;
115 }
116 seen[idx] = true;
117 stack[*sp] = id;
118 *sp += 1;
119 Push::Ok
120}
121fn contains(ids: &[u32], id: u32) -> bool {
122 ids.iter().any(|x| *x == id)
123}
124fn min_id(current: Option<u32>, id: u32) -> u32 {
125 match current {
126 Some(c) if c < id => c,
127 _ => id,
128 }
129}