Skip to main content

qualia_core_db/modalities/logic/shacl_extensions/
config.rs

1//! SHACL Extensions for Logging, System Tray, and Enhanced Settings
2//!
3//! This module extends the core SHACL compiler with constraints for the new
4//! client-side functionality including comprehensive logging, system tray
5//! integration, and enhanced configuration settings.
6
7use crate::webizen::SlgOpcode;
8
9// ── Logging System Constraints ─────────────────────────────────────────────
10
11/// `q42:LogConfiguration` — validates logging system configuration
12#[derive(Debug, Clone)]
13pub struct LogConfiguration {
14    pub max_memory_logs: u32,
15    pub max_disk_logs: u32,
16    pub flush_interval_ms: u32,
17}
18
19/// `q42:LogLevel` — validates log level enumeration
20#[derive(Debug, Clone)]
21pub struct LogLevel {
22    pub allowed_levels: Vec<String>, // ["DEBUG", "INFO", "WARN", "ERROR", "CRITICAL"]
23}
24
25/// `q42:LogEntry` — validates individual log entry structure
26#[derive(Debug, Clone)]
27pub struct LogEntry {
28    pub require_timestamp: bool,
29    pub require_context: bool,
30    pub max_message_length: u32,
31    pub allow_data_payload: bool,
32}
33
34/// `q42:LogRetention` — validates log retention policies
35#[derive(Debug, Clone)]
36pub struct LogRetention {
37    pub max_age_days: u32,
38    pub max_size_mb: u32,
39    pub backup_before_cleanup: bool,
40}
41
42/// `q42:LogExportFormat` — validates log export format constraints
43#[derive(Debug, Clone)]
44pub struct LogExportFormat {
45    pub allowed_formats: Vec<String>, // ["json", "txt", "csv"]
46    pub max_export_size_mb: u32,
47}
48
49// ── System Tray Constraints ─────────────────────────────────────────────────
50
51/// `q42:SystemTrayConfiguration` — validates system tray menu configuration
52#[derive(Debug, Clone)]
53pub struct SystemTrayConfiguration {
54    pub max_menu_items: u8,
55    pub require_separator_logic: bool,
56    pub allow_nested_menus: bool,
57}
58
59/// `q42:TrayMenuItem` — validates individual system tray menu items
60#[derive(Debug, Clone)]
61pub struct TrayMenuItem {
62    pub require_label: bool,
63    pub require_action: bool,
64    pub max_label_length: u32,
65    pub allow_icons: bool,
66}
67
68/// `q42:TrayStatusIndicator` — validates system tray status indicators
69#[derive(Debug, Clone)]
70pub struct TrayStatusIndicator {
71    pub allow_live_updates: bool,
72    pub update_interval_ms: u32,
73    pub max_status_length: u32,
74}
75
76/// `q42:TrayAction` — validates system tray action handlers
77#[derive(Debug, Clone)]
78pub struct TrayAction {
79    pub require_window_context: bool,
80    pub allow_async_actions: bool,
81    pub timeout_ms: u32,
82}
83
84// ── Enhanced Settings Constraints ─────────────────────────────────────────────
85
86/// `q42:StorageConfiguration` — validates storage path and quota settings
87#[derive(Debug, Clone)]
88pub struct StorageConfiguration {
89    pub min_quota_gb: u32,
90    pub max_quota_gb: u32,
91    pub require_absolute_path: bool,
92    pub allowed_path_patterns: Vec<String>,
93}
94
95/// `q42:NetworkConfiguration` — validates daemon network settings
96#[derive(Debug, Clone)]
97pub struct NetworkConfiguration {
98    pub allowed_ports: Vec<u16>,
99    pub require_port_conflict_check: bool,
100    pub default_host: String,
101}
102
103/// `q42:TaxRecipientConfiguration` — validates ILP tax recipient configuration
104#[derive(Debug, Clone)]
105pub struct TaxRecipientConfiguration {
106    pub require_ilp_address: bool,
107    pub min_share_percent: u8,
108    pub max_share_percent: u8,
109    pub allow_nym_routing: bool,
110    pub total_share_validation: bool,
111}
112
113/// `q42:SecurityConfiguration` — validates security-related settings
114#[derive(Debug, Clone)]
115pub struct SecurityConfiguration {
116    pub require_encryption: bool,
117    pub allowed_cipher_suites: Vec<String>,
118    pub min_key_length_bits: u16,
119}
120
121// ── Opcode Generation for New Constraints ─────────────────────────────────────
122
123impl LogConfiguration {
124    /// Compiles the log configuration to SlgOpcode sequence.
125    pub fn to_opcodes(&self) -> Vec<SlgOpcode> {
126        vec![
127            SlgOpcode::CheckMaxInclusive(self.max_memory_logs as f64),
128            SlgOpcode::CheckMaxInclusive(self.max_disk_logs as f64),
129            SlgOpcode::CheckMinInclusive(self.flush_interval_ms as f64),
130        ]
131    }
132}
133
134impl LogLevel {
135    /// Compiles the log level to opcodes.
136    pub fn to_opcodes(&self) -> Vec<SlgOpcode> {
137        // Validate that log level is in allowed set
138        self.allowed_levels
139            .iter()
140            .map(|level| SlgOpcode::CheckHasValue(crate::q_hash(level)))
141            .collect()
142    }
143}
144
145impl LogEntry {
146    /// Compiles a log entry to opcodes.
147    pub fn to_opcodes(&self) -> Vec<SlgOpcode> {
148        let mut opcodes = Vec::new();
149        if self.require_timestamp {
150            opcodes.push(SlgOpcode::CheckMinCount(1));
151        }
152        if self.require_context {
153            opcodes.push(SlgOpcode::CheckMinCount(1));
154        }
155        if self.max_message_length > 0 {
156            opcodes.push(SlgOpcode::CheckMaxLength(self.max_message_length));
157        }
158        if !self.allow_data_payload {
159            opcodes.push(SlgOpcode::CheckMaxCount(3)); // timestamp, level, message only
160        }
161        opcodes
162    }
163}
164
165impl LogRetention {
166    /// Compiles retention policy to opcodes.
167    pub fn to_opcodes(&self) -> Vec<SlgOpcode> {
168        vec![
169            SlgOpcode::CheckMaxInclusive(self.max_age_days as f64),
170            SlgOpcode::CheckMaxInclusive(self.max_size_mb as f64),
171        ]
172    }
173}
174
175impl LogExportFormat {
176    /// Compiles the export format to opcodes.
177    pub fn to_opcodes(&self) -> Vec<SlgOpcode> {
178        let mut opcodes = vec![SlgOpcode::CheckMaxInclusive(self.max_export_size_mb as f64)];
179        // Add format validation
180        for format in &self.allowed_formats {
181            opcodes.push(SlgOpcode::CheckHasValue(crate::q_hash(format)));
182        }
183        opcodes
184    }
185}
186
187impl SystemTrayConfiguration {
188    /// Compiles tray configuration to opcodes.
189    pub fn to_opcodes(&self) -> Vec<SlgOpcode> {
190        vec![SlgOpcode::CheckMaxInclusive(self.max_menu_items as f64)]
191    }
192}
193
194impl TrayMenuItem {
195    /// Compiles a tray menu item to opcodes.
196    pub fn to_opcodes(&self) -> Vec<SlgOpcode> {
197        let mut opcodes = Vec::new();
198        if self.require_label {
199            opcodes.push(SlgOpcode::CheckMinCount(1));
200        }
201        if self.require_action {
202            opcodes.push(SlgOpcode::CheckMinCount(1));
203        }
204        if self.max_label_length > 0 {
205            opcodes.push(SlgOpcode::CheckMaxLength(self.max_label_length));
206        }
207        opcodes
208    }
209}
210
211impl TrayStatusIndicator {
212    /// Compiles a status indicator to opcodes.
213    pub fn to_opcodes(&self) -> Vec<SlgOpcode> {
214        vec![
215            SlgOpcode::CheckMaxLength(self.max_status_length),
216            SlgOpcode::CheckMinInclusive(self.update_interval_ms as f64),
217        ]
218    }
219}
220
221impl TrayAction {
222    /// Compiles a tray action to opcodes.
223    pub fn to_opcodes(&self) -> Vec<SlgOpcode> {
224        vec![SlgOpcode::CheckMaxInclusive(self.timeout_ms as f64)]
225    }
226}
227
228impl StorageConfiguration {
229    /// Compiles storage configuration to opcodes.
230    pub fn to_opcodes(&self) -> Vec<SlgOpcode> {
231        let mut opcodes = vec![
232            SlgOpcode::CheckMinInclusive(self.min_quota_gb as f64),
233            SlgOpcode::CheckMaxInclusive(self.max_quota_gb as f64),
234        ];
235        if self.require_absolute_path {
236            opcodes.push(SlgOpcode::CheckMinCount(1));
237        }
238        opcodes
239    }
240}
241
242impl NetworkConfiguration {
243    /// Compiles network configuration to opcodes.
244    pub fn to_opcodes(&self) -> Vec<SlgOpcode> {
245        let mut opcodes = Vec::new();
246        for port in &self.allowed_ports {
247            opcodes.push(SlgOpcode::CheckHasValue(*port as u64));
248        }
249        opcodes
250    }
251}
252
253impl TaxRecipientConfiguration {
254    /// Compiles tax-recipient configuration to opcodes.
255    pub fn to_opcodes(&self) -> Vec<SlgOpcode> {
256        vec![
257            SlgOpcode::CheckMinInclusive(self.min_share_percent as f64),
258            SlgOpcode::CheckMaxInclusive(self.max_share_percent as f64),
259        ]
260    }
261}
262
263impl SecurityConfiguration {
264    /// Compiles security configuration to opcodes.
265    pub fn to_opcodes(&self) -> Vec<SlgOpcode> {
266        vec![SlgOpcode::CheckMinInclusive(
267            self.min_key_length_bits as f64,
268        )]
269    }
270}
271
272// ── SHACL TTL Vocabulary Extensions ─────────────────────────────────────────────
273
274/// Returns the SHACL TTL vocabulary extensions for the new constraints
275pub fn get_shacl_extensions_ttl() -> &'static str {
276    r#"
277@prefix q42: <https://webizen.org/q42#> .
278@prefix sh: <http://www.w3.org/ns/shacl#> .
279@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
280
281# ── Logging System Constraints ─────────────────────────────────────────────
282
283q42:LogConfiguration a sh:NodeShape ;
284    sh:property [
285        sh:path q42:maxMemoryLogs ;
286        sh:datatype xsd:integer ;
287        sh:minInclusive 1 ;
288        sh:maxInclusive 100000 ;
289        sh:message "Maximum memory logs must be between 1 and 100000" ;
290    ] ;
291    sh:property [
292        sh:path q42:maxDiskLogs ;
293        sh:datatype xsd:integer ;
294        sh:minInclusive 1 ;
295        sh:maxInclusive 1000000 ;
296        sh:message "Maximum disk logs must be between 1 and 1000000" ;
297    ] ;
298    sh:property [
299        sh:path q42:flushIntervalMs ;
300        sh:datatype xsd:integer ;
301        sh:minInclusive 100 ;
302        sh:maxInclusive 60000 ;
303        sh:message "Flush interval must be between 100ms and 60000ms" ;
304    ] .
305
306q42:LogLevel a sh:NodeShape ;
307    sh:property [
308        sh:path q42:allowedLevels ;
309        sh:in ("DEBUG" "INFO" "WARN" "ERROR" "CRITICAL") ;
310        sh:message "Log level must be one of: DEBUG, INFO, WARN, ERROR, CRITICAL" ;
311    ] .
312
313q42:LogEntry a sh:NodeShape ;
314    sh:property [
315        sh:path q42:timestamp ;
316        sh:datatype xsd:dateTime ;
317        sh:minCount 1 ;
318        sh:message "Log entry must have a timestamp" ;
319    ] ;
320    sh:property [
321        sh:path q42:level ;
322        sh:datatype xsd:string ;
323        sh:minCount 1 ;
324        sh:message "Log entry must have a level" ;
325    ] ;
326    sh:property [
327        sh:path q42:message ;
328        sh:datatype xsd:string ;
329        sh:maxLength 10000 ;
330        sh:message "Log message must not exceed 10000 characters" ;
331    ] .
332
333q42:LogRetention a sh:NodeShape ;
334    sh:property [
335        sh:path q42:maxAgeDays ;
336        sh:datatype xsd:integer ;
337        sh:minInclusive 1 ;
338        sh:maxInclusive 365 ;
339        sh:message "Log retention age must be between 1 and 365 days" ;
340    ] ;
341    sh:property [
342        sh:path q42:maxSizeMb ;
343        sh:datatype xsd:integer ;
344        sh:minInclusive 1 ;
345        sh:maxInclusive 10240 ;
346        sh:message "Log retention size must be between 1MB and 10GB" ;
347    ] .
348
349# ── System Tray Constraints ─────────────────────────────────────────────────
350
351q42:SystemTrayConfiguration a sh:NodeShape ;
352    sh:property [
353        sh:path q42:maxMenuItems ;
354        sh:datatype xsd:integer ;
355        sh:minInclusive 1 ;
356        sh:maxInclusive 20 ;
357        sh:message "System tray can have at most 20 menu items" ;
358    ] .
359
360q42:TrayMenuItem a sh:NodeShape ;
361    sh:property [
362        sh:path q42:label ;
363        sh:datatype xsd:string ;
364        sh:minCount 1 ;
365        sh:maxLength 50 ;
366        sh:message "Tray menu item must have a label (max 50 characters)" ;
367    ] ;
368    sh:property [
369        sh:path q42:action ;
370        sh:datatype xsd:string ;
371        sh:minCount 1 ;
372        sh:message "Tray menu item must have an action" ;
373    ] .
374
375q42:TrayStatusIndicator a sh:NodeShape ;
376    sh:property [
377        sh:path q42:statusText ;
378        sh:datatype xsd:string ;
379        sh:maxLength 100 ;
380        sh:message "Status text must not exceed 100 characters" ;
381    ] ;
382    sh:property [
383        sh:path q42:updateIntervalMs ;
384        sh:datatype xsd:integer ;
385        sh:minInclusive 100 ;
386        sh:maxInclusive 10000 ;
387        sh:message "Update interval must be between 100ms and 10000ms" ;
388    ] .
389
390# ── Enhanced Settings Constraints ─────────────────────────────────────────────
391
392q42:StorageConfiguration a sh:NodeShape ;
393    sh:property [
394        sh:path q42:storageQuotaGb ;
395        sh:datatype xsd:integer ;
396        sh:minInclusive 1 ;
397        sh:maxInclusive 1000 ;
398        sh:message "Storage quota must be between 1GB and 1000GB" ;
399    ] ;
400    sh:property [
401        sh:path q42:storagePath ;
402        sh:datatype xsd:string ;
403        sh:minCount 1 ;
404        sh:pattern "^/" ;
405        sh:message "Storage path must be an absolute path" ;
406    ] .
407
408q42:NetworkConfiguration a sh:NodeShape ;
409    sh:property [
410        sh:path q42:daemonPort ;
411        sh:datatype xsd:integer ;
412        sh:minInclusive 1024 ;
413        sh:maxInclusive 65535 ;
414        sh:message "Daemon port must be between 1024 and 65535" ;
415    ] ;
416    sh:property [
417        sh:path q42:daemonHost ;
418        sh:datatype xsd:string ;
419        sh:pattern "^(localhost|127\\.0\\.0\\.1|0\\.0\\.0\\.0)$" ;
420        sh:message "Daemon host must be localhost, 127.0.0.1, or 0.0.0.0" ;
421    ] .
422
423q42:TaxRecipientConfiguration a sh:NodeShape ;
424    sh:property [
425        sh:path q42:sharePercent ;
426        sh:datatype xsd:integer ;
427        sh:minInclusive 0 ;
428        sh:maxInclusive 100 ;
429        sh:message "Share percent must be between 0 and 100" ;
430    ] ;
431    sh:property [
432        sh:path q42:ilpAddress ;
433        sh:datatype xsd:string ;
434        sh:minCount 1 ;
435        sh:pattern "^\\$ilp\\." ;
436        sh:message "ILP address must start with $ilp." ;
437    ] .
438
439q42:SecurityConfiguration a sh:NodeShape ;
440    sh:property [
441        sh:path q42:minKeyLengthBits ;
442        sh:datatype xsd:integer ;
443        sh:minInclusive 128 ;
444        sh:maxInclusive 4096 ;
445        sh:message "Key length must be between 128 and 4096 bits" ;
446    ] ;
447    sh:property [
448        sh:path q42:requireEncryption ;
449        sh:datatype xsd:boolean ;
450        sh:message "Require encryption must be a boolean" ;
451    ] .
452"#
453}