Skip to main content

vtcode_commons/
unicode.rs

1//! Unicode monitoring and validation utilities
2
3use once_cell::sync::Lazy;
4use std::collections::HashMap;
5use std::sync::Mutex;
6use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering};
7use std::time::Instant;
8
9/// Global unicode monitoring statistics
10#[derive(Debug, Default)]
11pub struct UnicodeMonitor {
12    // Processing statistics
13    pub total_bytes_processed: AtomicU64,
14    pub total_unicode_bytes: AtomicU64,
15    pub total_sequences: AtomicU64,
16    pub total_errors: AtomicU64,
17
18    // Error tracking
19    pub error_types: Mutex<HashMap<String, usize>>,
20    pub last_error: Mutex<Option<String>>,
21
22    // Performance metrics
23    pub processing_time_ns: AtomicU64,
24    pub max_buffer_size: AtomicUsize,
25
26    // Session tracking
27    pub active_sessions: AtomicUsize,
28    pub total_sessions: AtomicUsize,
29    pub unicode_sessions: AtomicUsize,
30}
31
32impl UnicodeMonitor {
33    /// Create a new unicode monitor
34    pub fn new() -> Self {
35        Self {
36            total_bytes_processed: AtomicU64::new(0),
37            total_unicode_bytes: AtomicU64::new(0),
38            total_sequences: AtomicU64::new(0),
39            total_errors: AtomicU64::new(0),
40            error_types: Mutex::new(HashMap::new()),
41            last_error: Mutex::new(None),
42            processing_time_ns: AtomicU64::new(0),
43            max_buffer_size: AtomicUsize::new(0),
44            active_sessions: AtomicUsize::new(0),
45            total_sessions: AtomicUsize::new(0),
46            unicode_sessions: AtomicUsize::new(0),
47        }
48    }
49
50    /// Record unicode processing statistics
51    pub fn record_processing(&self, bytes: usize, unicode_bytes: usize, contains_unicode: bool) {
52        self.total_bytes_processed
53            .fetch_add(bytes as u64, Ordering::Relaxed);
54        self.total_unicode_bytes
55            .fetch_add(unicode_bytes as u64, Ordering::Relaxed);
56        self.total_sequences.fetch_add(1, Ordering::Relaxed);
57
58        if contains_unicode {
59            self.unicode_sessions.fetch_add(1, Ordering::Relaxed);
60        }
61    }
62
63    /// Record a unicode error
64    pub fn record_error(&self, error_type: &str, details: &str) {
65        self.total_errors.fetch_add(1, Ordering::Relaxed);
66
67        if let Ok(mut errors) = self.error_types.lock() {
68            *errors.entry(error_type.to_string()).or_insert(0) += 1;
69        }
70
71        if let Ok(mut last) = self.last_error.lock() {
72            *last = Some(format!("{}: {}", error_type, details));
73        }
74    }
75
76    /// Record processing time in nanoseconds
77    pub fn record_processing_time(&self, nanoseconds: u64) {
78        self.processing_time_ns
79            .fetch_add(nanoseconds, Ordering::Relaxed);
80    }
81
82    /// Record maximum buffer size encountered
83    pub fn record_max_buffer_size(&self, size: usize) {
84        self.max_buffer_size.fetch_max(size, Ordering::Relaxed);
85    }
86
87    /// Start a new unicode processing session
88    pub fn start_session(&self) {
89        self.active_sessions.fetch_add(1, Ordering::Relaxed);
90        self.total_sessions.fetch_add(1, Ordering::Relaxed);
91    }
92
93    /// End a unicode processing session
94    pub fn end_session(&self) {
95        self.active_sessions.fetch_sub(1, Ordering::Relaxed);
96    }
97}
98
99/// Unicode validation context for tracking processing of individual buffers
100pub struct UnicodeValidationContext {
101    pub start_time: Instant,
102    pub unicode_detected: bool,
103    pub errors: Vec<String>,
104}
105
106impl UnicodeValidationContext {
107    /// Create a new validation context
108    pub fn new(_buffer_size: usize) -> Self {
109        Self {
110            start_time: Instant::now(),
111            unicode_detected: false,
112            errors: Vec::new(),
113        }
114    }
115
116    /// Record that unicode was detected in this buffer
117    pub fn record_unicode_detected(&mut self) {
118        self.unicode_detected = true;
119    }
120
121    /// Record an error that occurred during processing
122    pub fn record_error(&mut self, error: String) {
123        self.errors.push(error);
124    }
125
126    /// Complete the validation and record statistics
127    pub fn complete(self, _processed_bytes: usize) {
128        // Implementation can be expanded to update global statistics
129    }
130}
131
132/// Global unicode monitor instance
133pub static UNICODE_MONITOR: Lazy<UnicodeMonitor> = Lazy::new(UnicodeMonitor::new);