vtcode_commons/
unicode.rs1use hashbrown::HashMap;
4use once_cell::sync::Lazy;
5use std::sync::Mutex;
6use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering};
7use std::time::Instant;
8
9#[derive(Debug, Default)]
11pub struct UnicodeMonitor {
12 pub total_bytes_processed: AtomicU64,
14 pub total_unicode_bytes: AtomicU64,
15 pub total_sequences: AtomicU64,
16 pub total_errors: AtomicU64,
17
18 pub error_types: Mutex<HashMap<String, usize>>,
20 pub last_error: Mutex<Option<String>>,
21
22 pub processing_time_ns: AtomicU64,
24 pub max_buffer_size: AtomicUsize,
25
26 pub active_sessions: AtomicUsize,
28 pub total_sessions: AtomicUsize,
29 pub unicode_sessions: AtomicUsize,
30}
31
32impl UnicodeMonitor {
33 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 pub fn record_processing(&self, bytes: usize, unicode_bytes: usize, contains_unicode: bool) {
52 self.total_bytes_processed.fetch_add(bytes as u64, Ordering::Relaxed);
53 self.total_unicode_bytes.fetch_add(unicode_bytes as u64, Ordering::Relaxed);
54 self.total_sequences.fetch_add(1, Ordering::Relaxed);
55
56 if contains_unicode {
57 self.unicode_sessions.fetch_add(1, Ordering::Relaxed);
58 }
59 }
60
61 pub fn record_error(&self, error_type: &str, details: &str) {
63 self.total_errors.fetch_add(1, Ordering::Relaxed);
64
65 if let Ok(mut errors) = self.error_types.lock() {
66 *errors.entry(error_type.to_string()).or_insert(0) += 1;
67 }
68
69 if let Ok(mut last) = self.last_error.lock() {
70 *last = Some(format!("{error_type}: {details}"));
71 }
72 }
73
74 pub fn record_processing_time(&self, nanoseconds: u64) {
76 self.processing_time_ns.fetch_add(nanoseconds, Ordering::Relaxed);
77 }
78
79 pub fn record_max_buffer_size(&self, size: usize) {
81 self.max_buffer_size.fetch_max(size, Ordering::Relaxed);
82 }
83
84 pub fn start_session(&self) {
86 self.active_sessions.fetch_add(1, Ordering::Relaxed);
87 self.total_sessions.fetch_add(1, Ordering::Relaxed);
88 }
89
90 pub fn end_session(&self) {
92 self.active_sessions.fetch_sub(1, Ordering::Relaxed);
93 }
94}
95
96pub struct UnicodeValidationContext {
98 pub start_time: Instant,
99 pub unicode_detected: bool,
100 pub errors: Vec<String>,
101}
102
103impl UnicodeValidationContext {
104 pub fn new(_buffer_size: usize) -> Self {
106 Self {
107 start_time: Instant::now(),
108 unicode_detected: false,
109 errors: Vec::new(),
110 }
111 }
112
113 pub fn record_unicode_detected(&mut self) {
115 self.unicode_detected = true;
116 }
117
118 pub fn record_error(&mut self, error: String) {
120 self.errors.push(error);
121 }
122
123 pub fn complete(self, _processed_bytes: usize) {
125 }
127}
128
129pub static UNICODE_MONITOR: Lazy<UnicodeMonitor> = Lazy::new(UnicodeMonitor::new);