Skip to main content

qualia_core_db/domains/geospatial/
canvas_rights.rs

1use crate::NQuin;
2
3/// G2 explicit-denial guard: returns `true` when the location owner has explicitly
4/// refused this principal or asset. Fail-closed — callers must reject on `true`.
5pub fn explicit_denial_guard(
6    location_hash: u64,
7    principal_hash: u64,
8    asset_hash: u64,
9    arena_quins: &[NQuin],
10) -> bool {
11    let explicit_denial = crate::q_hash("q42:explicitDenial");
12    for quin in arena_quins {
13        if quin.predicate == explicit_denial && quin.subject == location_hash {
14            if quin.object == principal_hash || quin.object == asset_hash {
15                return true;
16            }
17        }
18    }
19    false
20}
21
22/// Represents the governed rights to anchor virtual content at a specific physical location.
23pub struct CanvasRightsModel;
24
25impl CanvasRightsModel {
26    /// Validates whether a specific principal has the right to place a virtual asset
27    /// at the given `location_hash`.
28    ///
29    /// Follows the G2 explicit-denial guard principle:
30    /// No automated agent can override a human principal's explicit refusal quin.
31    pub fn validate_placement(
32        location_hash: u64,
33        principal_hash: u64,
34        asset_hash: u64,
35        arena_quins: &[NQuin],
36    ) -> bool {
37        let placement_right = crate::q_hash("q42:hasPlacementRight");
38
39        if explicit_denial_guard(location_hash, principal_hash, asset_hash, arena_quins) {
40            return false;
41        }
42
43        // 2. Check for explicit grant or steward delegation
44        // In a permissive-commons scenario, some areas might be public.
45        // For owned regions, we require a placement_right quin.
46        let mut has_right = false;
47        // True once we see a placement_right quin for this location — i.e. the
48        // location is under private/owned governance rather than open commons.
49        // Derived from the arena quins below, not a mock.
50        let mut is_owned = false;
51
52        for quin in arena_quins {
53            if quin.predicate == placement_right && quin.subject == location_hash {
54                is_owned = true;
55                if quin.object == principal_hash {
56                    has_right = true;
57                    break;
58                }
59            }
60        }
61
62        // If it's privately owned, they must have the right.
63        // If it's a true commons (not owned), it's permitted unless denied above.
64        !is_owned || has_right
65    }
66}
67
68#[cfg(test)]
69mod tests {
70    use super::*;
71
72    #[test]
73    fn test_placement_explicit_denial() {
74        let loc = 0x111;
75        let prin = 0x222;
76        let asset = 0x333;
77
78        let mut quins = vec![];
79        // Explicitly deny the principal
80        quins.push(NQuin {
81            subject: loc,
82            predicate: crate::q_hash("q42:explicitDenial"),
83            object: prin,
84            context: 0,
85            metadata: 0,
86            parity: 0,
87        });
88
89        assert_eq!(
90            CanvasRightsModel::validate_placement(loc, prin, asset, &quins),
91            false
92        );
93    }
94
95    #[test]
96    fn test_placement_granted() {
97        let loc = 0x111;
98        let prin = 0x222;
99        let asset = 0x333;
100
101        let mut quins = vec![];
102        // Grant the principal
103        quins.push(NQuin {
104            subject: loc,
105            predicate: crate::q_hash("q42:hasPlacementRight"),
106            object: prin,
107            context: 0,
108            metadata: 0,
109            parity: 0,
110        });
111
112        assert_eq!(
113            CanvasRightsModel::validate_placement(loc, prin, asset, &quins),
114            true
115        );
116    }
117}