qualia_core_db/governance/
tk_bc_labels.rs1use bytemuck::{Pod, Zeroable};
13use serde::{Deserialize, Serialize};
14
15#[repr(transparent)]
17#[derive(Debug, Clone, Copy, PartialEq, Eq, Pod, Zeroable, Serialize, Deserialize, Default)]
18pub struct TkBcLabelFlags(pub u32);
19
20impl TkBcLabelFlags {
21 pub const NONE: Self = Self(0);
22 pub const TK_ATTRIBUTION: Self = Self(1 << 0);
24 pub const TK_NON_COMMERCIAL: Self = Self(1 << 1);
26 pub const TK_CULTURALLY_SENSITIVE: Self = Self(1 << 2);
28 pub const TK_COMMUNITY_VERIFIED: Self = Self(1 << 3);
30 pub const BC_PROVENANCE: Self = Self(1 << 4);
32 pub const BC_MULTIPLE_COMMUNITY: Self = Self(1 << 5);
34
35 pub const fn has(self, flag: Self) -> bool {
37 (self.0 & flag.0) != 0
38 }
39
40 pub const fn union(self, other: Self) -> Self {
42 Self(self.0 | other.0)
43 }
44}
45
46#[derive(Debug, Clone, PartialEq, Eq)]
48pub struct TkAccessContext {
49 pub is_commercial_use: bool,
50 pub has_attribution: bool,
51 pub community_consent_signed: bool,
52 pub observer_is_verified_custodian: bool,
53}
54
55#[derive(Debug, Clone, Copy, PartialEq, Eq)]
57pub enum TkEvaluationVerdict {
58 Permitted,
60 ProhibitedCommercialWithoutConsent,
62 ProhibitedMissingAttribution,
64 RestrictedCulturalSensitivity,
66}
67
68pub fn evaluate_tk_bc_policy(
71 labels: TkBcLabelFlags,
72 context: &TkAccessContext,
73) -> TkEvaluationVerdict {
74 if labels.0 == 0 {
75 return TkEvaluationVerdict::Permitted;
76 }
77
78 if labels.has(TkBcLabelFlags::TK_CULTURALLY_SENSITIVE)
80 && !context.observer_is_verified_custodian
81 {
82 return TkEvaluationVerdict::RestrictedCulturalSensitivity;
83 }
84
85 if labels.has(TkBcLabelFlags::TK_NON_COMMERCIAL)
87 && context.is_commercial_use
88 && !context.community_consent_signed
89 {
90 return TkEvaluationVerdict::ProhibitedCommercialWithoutConsent;
91 }
92
93 if labels.has(TkBcLabelFlags::TK_ATTRIBUTION) && !context.has_attribution {
95 return TkEvaluationVerdict::ProhibitedMissingAttribution;
96 }
97
98 TkEvaluationVerdict::Permitted
99}
100
101#[cfg(test)]
102mod tests {
103 use super::*;
104
105 #[test]
106 fn test_tk_bc_policy_evaluation() {
107 let labels = TkBcLabelFlags::TK_ATTRIBUTION.union(TkBcLabelFlags::TK_NON_COMMERCIAL);
108
109 let commercial_ctx = TkAccessContext {
111 is_commercial_use: true,
112 has_attribution: true,
113 community_consent_signed: false,
114 observer_is_verified_custodian: false,
115 };
116 assert_eq!(
117 evaluate_tk_bc_policy(labels, &commercial_ctx),
118 TkEvaluationVerdict::ProhibitedCommercialWithoutConsent
119 );
120
121 let consent_ctx = TkAccessContext {
123 is_commercial_use: true,
124 has_attribution: true,
125 community_consent_signed: true,
126 observer_is_verified_custodian: false,
127 };
128 assert_eq!(
129 evaluate_tk_bc_policy(labels, &consent_ctx),
130 TkEvaluationVerdict::Permitted
131 );
132
133 let no_attr_ctx = TkAccessContext {
135 is_commercial_use: false,
136 has_attribution: false,
137 community_consent_signed: false,
138 observer_is_verified_custodian: false,
139 };
140 assert_eq!(
141 evaluate_tk_bc_policy(labels, &no_attr_ctx),
142 TkEvaluationVerdict::ProhibitedMissingAttribution
143 );
144 }
145}