qualia_core_db/net/qdnf/clinical/
pairing.rs1use super::{admit_outcome, first_empty, require_active_mutual, MAX_SLOTS};
6use crate::net::qdnf::authority::{ContactState, PolicyOutcome, TemporalGrant};
7use crate::net::qdnf::errors::QdnfError;
8use crate::net::qdnf::types::StrongDigest;
9
10#[inline]
11pub const fn public_patient_index_allowed() -> bool {
13 false
14}
15
16#[inline]
17pub const fn vip_association_indexed() -> bool {
19 false
20}
21
22#[inline]
23pub const fn diagnosis_discovery_allowed() -> bool {
25 false
26}
27
28#[repr(C)]
30#[derive(Clone, Copy, Debug, PartialEq, Eq)]
31pub struct ClinicalPair {
32 pub patient: StrongDigest,
33 pub clinician: StrongDigest,
34 pub patient_contact: ContactState,
35 pub clinician_contact: ContactState,
36 pub route: StrongDigest,
37}
38
39#[derive(Clone, Copy, Debug)]
41pub struct PairingTable {
42 slots: [Option<ClinicalPair>; MAX_SLOTS],
43}
44
45impl PairingTable {
46 pub const fn new() -> Self {
48 Self {
49 slots: [None; MAX_SLOTS],
50 }
51 }
52
53 fn find(&self, patient: StrongDigest, clinician: StrongDigest) -> Option<usize> {
54 let mut i = 0usize;
55 while i < MAX_SLOTS {
56 if let Some(p) = self.slots[i] {
57 if p.patient == patient && p.clinician == clinician {
58 return Some(i);
59 }
60 }
61 i += 1;
62 }
63 None
64 }
65
66 pub fn admit(&mut self, pair: ClinicalPair) -> Result<usize, QdnfError> {
68 if pair.patient == StrongDigest::ZERO || pair.clinician == StrongDigest::ZERO {
69 return Err(QdnfError::Malformed);
70 }
71 if pair.patient == pair.clinician {
72 return Err(QdnfError::Conflict);
73 }
74 if let Some(i) = self.find(pair.patient, pair.clinician) {
75 self.slots[i] = Some(pair);
76 return Ok(i);
77 }
78 let i = first_empty(&self.slots).ok_or(QdnfError::Capacity)?;
79 self.slots[i] = Some(pair);
80 Ok(i)
81 }
82
83 pub fn get(
85 &self,
86 patient: StrongDigest,
87 clinician: StrongDigest,
88 ) -> Result<ClinicalPair, QdnfError> {
89 let i = self
90 .find(patient, clinician)
91 .ok_or(QdnfError::Unauthorized)?;
92 self.slots[i].ok_or(QdnfError::Unauthorized)
93 }
94
95 pub fn set_contacts(
97 &mut self,
98 patient: StrongDigest,
99 clinician: StrongDigest,
100 patient_contact: ContactState,
101 clinician_contact: ContactState,
102 ) -> Result<(), QdnfError> {
103 let i = self
104 .find(patient, clinician)
105 .ok_or(QdnfError::Unauthorized)?;
106 let mut pair = self.slots[i].ok_or(QdnfError::Unauthorized)?;
107 pair.patient_contact = patient_contact;
108 pair.clinician_contact = clinician_contact;
109 self.slots[i] = Some(pair);
110 Ok(())
111 }
112
113 pub fn exchange_route(
116 &mut self,
117 patient: StrongDigest,
118 clinician: StrongDigest,
119 route: StrongDigest,
120 outcome: PolicyOutcome,
121 grant: &TemporalGrant,
122 now_unix: u64,
123 ) -> Result<StrongDigest, QdnfError> {
124 if route == StrongDigest::ZERO {
125 return Err(QdnfError::Malformed);
126 }
127 let i = self
128 .find(patient, clinician)
129 .ok_or(QdnfError::Unauthorized)?;
130 let mut pair = self.slots[i].ok_or(QdnfError::Unauthorized)?;
131 require_active_mutual(pair.patient_contact, pair.clinician_contact)?;
132 grant.current_at(now_unix)?;
133 if grant.audience_digest != StrongDigest::ZERO
134 && grant.audience_digest != clinician
135 && grant.audience_digest != patient
136 {
137 return Err(QdnfError::Denied);
138 }
139 admit_outcome(outcome)?;
140 pair.route = route;
141 self.slots[i] = Some(pair);
142 Ok(route)
143 }
144
145 pub fn index_public(&self, _pair: ClinicalPair) -> Result<(), QdnfError> {
147 Err(QdnfError::Denied)
148 }
149}
150
151impl Default for PairingTable {
152 fn default() -> Self {
153 Self::new()
154 }
155}
156
157pub fn publish_relationship(
159 _patient: StrongDigest,
160 _clinician: StrongDigest,
161) -> Result<(), QdnfError> {
162 Err(QdnfError::Denied)
163}
164
165#[cfg(test)]
166mod tests {
167 use super::*;
168 use crate::net::qdnf::types::ProfileId;
169
170 fn d(tag: u8) -> StrongDigest {
171 let mut x = StrongDigest::ZERO;
172 x.0[0] = tag;
173 x
174 }
175
176 fn grant_until(exp: u64) -> TemporalGrant {
177 TemporalGrant {
178 purpose_digest: d(0x11),
179 audience_digest: d(2),
180 authority_generation: 1,
181 not_before_unix: 0,
182 expires_unix: exp,
183 profile: ProfileId::QDNF_CRYPTO_1,
184 }
185 }
186
187 fn pair(patient: u8, clinician: u8, clinician_contact: ContactState) -> ClinicalPair {
188 ClinicalPair {
189 patient: d(patient),
190 clinician: d(clinician),
191 patient_contact: ContactState::Active,
192 clinician_contact,
193 route: StrongDigest::ZERO,
194 }
195 }
196
197 #[test]
198 fn private_pairing_rejects_public_vip_and_diagnosis_index() {
199 let mut table = PairingTable::new();
200 let p = pair(1, 2, ContactState::Active);
201 assert!(table.admit(p).is_ok());
202 assert_eq!(table.index_public(p), Err(QdnfError::Denied));
203 assert_eq!(
204 publish_relationship(p.patient, p.clinician),
205 Err(QdnfError::Denied)
206 );
207 assert!(!public_patient_index_allowed());
208 assert!(!vip_association_indexed());
209 assert!(!diagnosis_discovery_allowed());
210 let grant = grant_until(100);
211 let route = d(9);
212 assert_eq!(
213 table.exchange_route(
214 p.patient,
215 p.clinician,
216 route,
217 PolicyOutcome::Allow,
218 &grant,
219 10
220 ),
221 Ok(route)
222 );
223 assert_eq!(table.get(p.patient, p.clinician).unwrap().route, route);
224 }
225
226 #[test]
227 fn suspended_blocked_and_request_cannot_exchange() {
228 let grant = grant_until(100);
229 let route = d(9);
230 let mut table = PairingTable::new();
231 let p = pair(1, 2, ContactState::Active);
232 table.admit(p).unwrap();
233 table
234 .set_contacts(
235 p.patient,
236 p.clinician,
237 ContactState::Active,
238 ContactState::Suspended,
239 )
240 .unwrap();
241 assert_eq!(
242 table.exchange_route(
243 p.patient,
244 p.clinician,
245 route,
246 PolicyOutcome::Allow,
247 &grant,
248 10
249 ),
250 Err(QdnfError::Denied)
251 );
252 table
253 .set_contacts(
254 p.patient,
255 p.clinician,
256 ContactState::Active,
257 ContactState::Blocked,
258 )
259 .unwrap();
260 assert_eq!(
261 table.exchange_route(
262 p.patient,
263 p.clinician,
264 route,
265 PolicyOutcome::Allow,
266 &grant,
267 10
268 ),
269 Err(QdnfError::Denied)
270 );
271 table
272 .set_contacts(
273 p.patient,
274 p.clinician,
275 ContactState::Request,
276 ContactState::Active,
277 )
278 .unwrap();
279 assert_eq!(
280 table.exchange_route(
281 p.patient,
282 p.clinician,
283 route,
284 PolicyOutcome::Allow,
285 &grant,
286 10
287 ),
288 Err(QdnfError::Unauthorized)
289 );
290 }
291
292 #[test]
293 fn unpaired_and_capacity() {
294 let mut table = PairingTable::new();
295 let grant = grant_until(100);
296 assert_eq!(
297 table.exchange_route(d(3), d(4), d(9), PolicyOutcome::Allow, &grant, 10),
298 Err(QdnfError::Unauthorized)
299 );
300 let mut n = 1u8;
301 while n <= 8 {
302 assert!(table.admit(pair(n, n + 10, ContactState::Active)).is_ok());
303 n += 1;
304 }
305 assert_eq!(
306 table.admit(pair(20, 21, ContactState::Active)),
307 Err(QdnfError::Capacity)
308 );
309 }
310}