Skip to main content

qualia_core_db/net/qdnf/clinical/
help.rs

1//! Confidential help independent of an abusive controller.
2//!
3//! Age/capacity/mandate is an explicit policy enum. A guardian is never
4//! notified automatically.
5
6use crate::net::qdnf::authority::ContactState;
7use crate::net::qdnf::errors::QdnfError;
8use crate::net::qdnf::types::StrongDigest;
9
10/// Selected participation / capacity / mandate policy. Never a boolean
11/// “notify parent”.
12#[repr(u8)]
13#[derive(Clone, Copy, Debug, PartialEq, Eq)]
14pub enum CapacityMandatePolicy {
15    AdultSelf = 1,
16    ChildAssisted = 2,
17    IndependentConfidentialHelp = 3,
18    DisputedGuardian = 4,
19}
20
21/// Automatic guardian notification is never enabled.
22#[inline]
23pub const fn notify_guardian_automatically() -> bool {
24    false
25}
26
27/// Auto-notify is always Denied. Independent confidential help does not
28/// disclose to a restricted or disputed guardian.
29pub fn notify_guardian(
30    policy: CapacityMandatePolicy,
31    guardian: StrongDigest,
32    controller: ContactState,
33) -> Result<(), QdnfError> {
34    let _ = (policy, guardian, controller);
35    debug_assert!(!notify_guardian_automatically());
36    Err(QdnfError::Denied)
37}
38
39/// Admit confidential help independently of the controller contact state.
40pub fn admit_confidential_help(
41    helper: StrongDigest,
42    policy: CapacityMandatePolicy,
43    controller: ContactState,
44) -> Result<(), QdnfError> {
45    if helper == StrongDigest::ZERO {
46        return Err(QdnfError::Malformed);
47    }
48    let _ = controller;
49    match policy {
50        CapacityMandatePolicy::AdultSelf
51        | CapacityMandatePolicy::ChildAssisted
52        | CapacityMandatePolicy::IndependentConfidentialHelp
53        | CapacityMandatePolicy::DisputedGuardian => Ok(()),
54    }
55}
56
57#[cfg(test)]
58mod tests {
59    use super::*;
60
61    fn d(tag: u8) -> StrongDigest {
62        let mut x = StrongDigest::ZERO;
63        x.0[0] = tag;
64        x
65    }
66
67    #[test]
68    fn guardian_is_not_auto_notified() {
69        assert!(!notify_guardian_automatically());
70        assert_eq!(
71            notify_guardian(
72                CapacityMandatePolicy::IndependentConfidentialHelp,
73                d(3),
74                ContactState::Active,
75            ),
76            Err(QdnfError::Denied)
77        );
78        assert_eq!(
79            notify_guardian(
80                CapacityMandatePolicy::DisputedGuardian,
81                d(3),
82                ContactState::Blocked,
83            ),
84            Err(QdnfError::Denied)
85        );
86        assert_eq!(
87            notify_guardian(
88                CapacityMandatePolicy::ChildAssisted,
89                d(3),
90                ContactState::Active
91            ),
92            Err(QdnfError::Denied)
93        );
94    }
95
96    #[test]
97    fn confidential_help_ignores_abusive_controller() {
98        assert!(admit_confidential_help(
99            d(8),
100            CapacityMandatePolicy::IndependentConfidentialHelp,
101            ContactState::Blocked,
102        )
103        .is_ok());
104        assert!(admit_confidential_help(
105            d(8),
106            CapacityMandatePolicy::DisputedGuardian,
107            ContactState::Suspended,
108        )
109        .is_ok());
110        assert_eq!(
111            admit_confidential_help(
112                StrongDigest::ZERO,
113                CapacityMandatePolicy::AdultSelf,
114                ContactState::Active,
115            ),
116            Err(QdnfError::Malformed)
117        );
118    }
119}