Skip to main content

vtcode_commons/
unicode.rs

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