vtcode_commons/
unicode.rs1#![expect(
2 unused_results,
3 reason = "Atomic metric updates are performed for their side effects; the previous counters are not needed."
4)]
5
6use hashbrown::HashMap;
9use std::sync::LazyLock;
10use std::sync::Mutex;
11use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering};
12
13#[derive(Debug, Default)]
15pub struct UnicodeMonitor {
16 total_bytes_processed: AtomicU64,
18 total_unicode_bytes: AtomicU64,
19 total_sequences: AtomicU64,
20 total_errors: AtomicU64,
21
22 error_types: Mutex<HashMap<String, usize>>,
24 last_error: Mutex<Option<String>>,
25
26 processing_time_ns: AtomicU64,
28 max_buffer_size: AtomicUsize,
29
30 active_sessions: AtomicUsize,
32 total_sessions: AtomicUsize,
33 unicode_sessions: AtomicUsize,
34}
35
36impl UnicodeMonitor {
37 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 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 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 pub fn record_processing_time(&self, nanoseconds: u64) {
80 self.processing_time_ns.fetch_add(nanoseconds, Ordering::Relaxed);
81 }
82
83 pub fn record_max_buffer_size(&self, size: usize) {
85 self.max_buffer_size.fetch_max(size, Ordering::Relaxed);
86 }
87
88 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 pub fn end_session(&self) {
96 self.active_sessions.fetch_sub(1, Ordering::Relaxed);
97 }
98}
99
100pub struct UnicodeValidationContext {
102 unicode_detected: bool,
103 errors: Vec<String>,
104}
105
106impl UnicodeValidationContext {
107 pub fn new(_buffer_size: usize) -> Self {
109 Self { unicode_detected: false, errors: Vec::new() }
110 }
111
112 pub fn record_unicode_detected(&mut self) {
114 self.unicode_detected = true;
115 }
116
117 pub fn record_error(&mut self, error: String) {
119 self.errors.push(error);
120 }
121
122 pub fn complete(self, _processed_bytes: usize) {
124 }
126}
127
128pub static UNICODE_MONITOR: LazyLock<UnicodeMonitor> = LazyLock::new(UnicodeMonitor::new);