vtcode-commons 0.134.11

Shared traits for paths, telemetry, and error reporting reused across VT Code component extractions
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
use std::borrow::Cow;
use std::fmt;

use anyhow::{Error, Result};

// File operation errors
pub const ERR_READ_FILE: &str = "failed to read file";
pub const ERR_WRITE_FILE: &str = "failed to write file";
pub const ERR_READ_DIR: &str = "failed to read directory";
pub const ERR_CREATE_DIR: &str = "failed to create directory";
pub const ERR_REMOVE_FILE: &str = "failed to remove file";
pub const ERR_REMOVE_DIR: &str = "failed to remove directory";
pub const ERR_READ_DIR_ENTRY: &str = "failed to read directory entry";
pub const ERR_GET_FILE_TYPE: &str = "failed to read file type";
pub const ERR_GET_METADATA: &str = "failed to read file metadata";
pub const ERR_CANONICALIZE_PATH: &str = "failed to canonicalize path";
pub const ERR_READ_SYMLINK: &str = "failed to read symlink";

// Skill/Tool errors
pub const ERR_CREATE_SKILLS_DIR: &str = "failed to create skills directory";
pub const ERR_CREATE_SKILL_DIR: &str = "failed to create skill directory";
pub const ERR_READ_SKILL_CODE: &str = "failed to read skill code";
pub const ERR_WRITE_SKILL_CODE: &str = "failed to write skill code";
pub const ERR_READ_SKILL_METADATA: &str = "failed to read skill metadata";
pub const ERR_WRITE_SKILL_METADATA: &str = "failed to write skill metadata";
pub const ERR_PARSE_SKILL_METADATA: &str = "failed to parse skill metadata";
pub const ERR_WRITE_SKILL_DOCS: &str = "failed to write skill documentation";
pub const ERR_DELETE_SKILL: &str = "failed to delete skill";
pub const ERR_READ_SKILLS_DIR: &str = "failed to read skills directory";
pub const ERR_TOOL_DENIED: &str = "tool denied or unavailable by policy";

// Audit/Logging errors
pub const ERR_CREATE_AUDIT_DIR: &str = "Failed to create audit directory";
pub const ERR_WRITE_AUDIT_LOG: &str = "failed to write audit log";

// Checkpoint/Snapshot errors
pub const ERR_CREATE_CHECKPOINT_DIR: &str = "failed to create checkpoint directory";
pub const ERR_WRITE_CHECKPOINT: &str = "failed to write checkpoint";
pub const ERR_READ_CHECKPOINT: &str = "failed to read checkpoint";

// Policy errors
pub const ERR_CREATE_POLICY_DIR: &str = "Failed to create directory for tool policy config";
pub const ERR_CREATE_WORKSPACE_POLICY_DIR: &str = "Failed to create workspace policy directory";

// Serialization errors
pub const ERR_SERIALIZE_METADATA: &str = "failed to serialize skill metadata";
pub const ERR_SERIALIZE_STATE: &str = "failed to serialize state";
pub const ERR_DESERIALIZE: &str = "failed to deserialize data";

// IPC/SDK errors
pub const ERR_CREATE_IPC_DIR: &str = "failed to create IPC directory";
pub const ERR_READ_REQUEST_FILE: &str = "failed to read request file";
pub const ERR_READ_REQUEST_JSON: &str = "failed to read request JSON";
pub const ERR_PARSE_REQUEST_JSON: &str = "failed to parse request JSON";
pub const ERR_PARSE_ARGS: &str = "failed to parse tokenized args";
pub const ERR_PARSE_RESULT: &str = "failed to parse de-tokenized result";

/// Helper macro for file operation errors with context
/// Usage: file_err!("path", "read") -> "failed to read path"
#[macro_export]
macro_rules! file_err {
    ($path:expr, read) => {
        format!("failed to read {}", $path)
    };
    ($path:expr, write) => {
        format!("failed to write {}", $path)
    };
    ($path:expr, delete) => {
        format!("failed to delete {}", $path)
    };
    ($path:expr, create) => {
        format!("failed to create {}", $path)
    };
}

/// Helper macro for context errors
/// Usage: ctx_err!(operation, context) -> "operation context"
#[macro_export]
macro_rules! ctx_err {
    ($op:expr, $ctx:expr) => {
        format!("{}: {}", $op, $ctx)
    };
}

/// Formats an error into a user-facing description. This allows extracted
/// components to present consistent error messaging without depending on the
/// CLI presentation layer.
pub trait ErrorFormatter: Send + Sync {
    /// Render the error into a user-facing string.
    fn format_error(&self, error: &Error) -> Cow<'_, str>;
}

/// Reports non-fatal errors to an observability backend.
pub trait ErrorReporter: Send + Sync {
    /// Capture the provided error for later inspection.
    fn capture(&self, error: &Error) -> Result<()>;

    /// Convenience helper to capture a simple message.
    fn capture_message(&self, message: impl Into<Cow<'static, str>>) -> Result<()> {
        let message: Cow<'static, str> = message.into();
        self.capture(&Error::msg(message))
    }
}

/// Error reporting implementation that drops every event. Useful for tests or
/// when a consumer does not yet integrate with error monitoring.
#[derive(Debug, Default, Clone, Copy)]
pub struct NoopErrorReporter;

impl ErrorReporter for NoopErrorReporter {
    fn capture(&self, _error: &Error) -> Result<()> {
        Ok(())
    }
}

/// Default formatter that surfaces the error's display output.
#[derive(Debug, Default, Clone, Copy)]
pub struct DisplayErrorFormatter;

impl ErrorFormatter for DisplayErrorFormatter {
    fn format_error(&self, error: &Error) -> Cow<'_, str> {
        Cow::Owned(format!("{error}"))
    }
}

/// A collection of errors that enables continuing work while collecting failures.
///
/// This type implements the "error parameter" pattern: instead of short-circuiting
/// on the first error, processing continues and errors are accumulated. The caller
/// can inspect the collection afterward to determine whether all operations
/// succeeded.
///
/// # Ergonomic Result handling
///
/// [`collect_result`](MultiErrors::collect_result) lets you process a `Result<T, E>`
/// while keeping the happy path dominant:
///
/// ```rust
/// use vtcode_commons::MultiErrors;
/// let mut errors: MultiErrors<String> = MultiErrors::new();
/// let value: Option<i32> = errors.collect_result("42".parse::<i32>().map_err(|e| e.to_string()));
/// assert_eq!(value, Some(42));
/// ```
///
/// # Composing with traditional error handling
///
/// Use [`ok`](MultiErrors::ok) or [`to_anyhow`](MultiErrors::to_anyhow) to convert
/// back into a traditional `Result`.
#[derive(Debug, Clone)]
pub struct MultiErrors<E = Error> {
    errors: Vec<E>,
}

impl<E> MultiErrors<E> {
    /// Create an empty error collection.
    pub fn new() -> Self {
        Self { errors: Vec::new() }
    }

    /// Add a single error to the collection.
    pub fn push(&mut self, error: E) {
        self.errors.push(error);
    }

    /// Extend the collection with multiple errors.
    pub fn extend(&mut self, iter: impl IntoIterator<Item = E>) {
        self.errors.extend(iter);
    }

    /// Returns `true` if no errors have been collected.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.errors.is_empty()
    }

    /// Returns the number of collected errors.
    #[must_use]
    pub fn len(&self) -> usize {
        self.errors.len()
    }

    /// Consume the collector and return the underlying error vector.
    #[must_use]
    pub fn into_inner(self) -> Vec<E> {
        self.errors
    }

    /// Returns a slice of all collected errors.
    #[must_use]
    pub fn as_slice(&self) -> &[E] {
        &self.errors
    }

    /// Returns an iterator over the collected errors.
    pub fn iter(&self) -> std::slice::Iter<'_, E> {
        self.errors.iter()
    }

    /// Convert into `Result<()>` — succeeds if no errors were collected.
    pub fn ok(self) -> std::result::Result<(), Self> {
        if self.errors.is_empty() {
            Ok(())
        } else {
            Err(self)
        }
    }

    /// Remove all errors from the collection.
    pub fn clear(&mut self) {
        self.errors.clear();
    }

    /// Process a `Result`, returning the success value or collecting the error.
    ///
    /// This is the key ergonomic method — it keeps the happy path as the primary
    /// flow while silently collecting errors for later inspection.
    pub fn collect_result<T, F>(&mut self, result: std::result::Result<T, F>) -> Option<T>
    where
        F: Into<E>,
    {
        match result {
            Ok(val) => Some(val),
            Err(e) => {
                self.errors.push(e.into());
                None
            }
        }
    }

    /// Convert into an [`anyhow::Error`] for use with traditional error handling.
    #[must_use]
    pub fn to_anyhow(&self) -> Error
    where
        E: fmt::Display,
    {
        Error::msg(self.to_string())
    }
}

impl<E> Default for MultiErrors<E> {
    fn default() -> Self {
        Self::new()
    }
}

impl<E> From<Vec<E>> for MultiErrors<E> {
    fn from(errors: Vec<E>) -> Self {
        Self { errors }
    }
}

impl<E> IntoIterator for MultiErrors<E> {
    type Item = E;
    type IntoIter = std::vec::IntoIter<E>;

    fn into_iter(self) -> Self::IntoIter {
        self.errors.into_iter()
    }
}

impl<E: serde::Serialize> serde::Serialize for MultiErrors<E> {
    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        self.errors.serialize(serializer)
    }
}

impl<'de, E: serde::Deserialize<'de>> serde::Deserialize<'de> for MultiErrors<E> {
    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        Vec::<E>::deserialize(deserializer).map(|errors| Self { errors })
    }
}

impl<E: fmt::Display> fmt::Display for MultiErrors<E> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self.errors.len() {
            0 => write!(f, "no errors"),
            1 => write!(f, "{}", self.errors[0]),
            _ => {
                for (i, error) in self.errors.iter().enumerate() {
                    if i > 0 {
                        writeln!(f)?;
                    }
                    write!(f, "  {}. {error}", i + 1)?;
                }
                Ok(())
            }
        }
    }
}

impl<E: std::error::Error + 'static> std::error::Error for MultiErrors<E> {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        self.errors
            .first()
            .map(|e| e as &(dyn std::error::Error + 'static))
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn formatter_uses_display() {
        let formatter = DisplayErrorFormatter;
        let error = Error::msg("test error");
        assert_eq!(formatter.format_error(&error), "test error");
    }

    #[test]
    fn noop_reporter_drops_errors() {
        let reporter = NoopErrorReporter;
        let error = Error::msg("test");
        assert!(reporter.capture(&error).is_ok());
        assert!(reporter.capture_message("message").is_ok());
    }

    #[test]
    fn multi_errors_new_is_empty() {
        let errors: MultiErrors<String> = MultiErrors::new();
        assert!(errors.is_empty());
        assert_eq!(errors.len(), 0);
    }

    #[test]
    fn multi_errors_push_and_len() {
        let mut errors = MultiErrors::new();
        errors.push("error 1".to_string());
        errors.push("error 2".to_string());
        assert!(!errors.is_empty());
        assert_eq!(errors.len(), 2);
    }

    #[test]
    fn multi_errors_collect_result_ok() {
        let mut errors: MultiErrors<String> = MultiErrors::new();
        let value: i32 = errors.collect_result(Ok::<_, String>(42)).unwrap_or(0);
        assert_eq!(value, 42);
        assert!(errors.is_empty());
    }

    #[test]
    fn multi_errors_collect_result_err() {
        let mut errors: MultiErrors<String> = MultiErrors::new();
        let value: i32 = errors
            .collect_result(Err::<i32, String>("bad".to_string()))
            .unwrap_or(0);
        assert_eq!(value, 0);
        assert_eq!(errors.len(), 1);
    }

    #[test]
    fn multi_errors_ok_succeeds_when_empty() {
        let errors: MultiErrors<String> = MultiErrors::new();
        assert!(errors.ok().is_ok());
    }

    #[test]
    fn multi_errors_ok_fails_when_not_empty() {
        let mut errors = MultiErrors::new();
        errors.push("error".to_string());
        assert!(errors.ok().is_err());
    }

    #[test]
    fn multi_errors_display_empty() {
        let errors: MultiErrors<String> = MultiErrors::new();
        assert_eq!(errors.to_string(), "no errors");
    }

    #[test]
    fn multi_errors_display_single() {
        let mut errors = MultiErrors::new();
        errors.push("something failed".to_string());
        assert_eq!(errors.to_string(), "something failed");
    }

    #[test]
    fn multi_errors_display_multiple() {
        let mut errors = MultiErrors::new();
        errors.push("first issue".to_string());
        errors.push("second issue".to_string());
        let display = errors.to_string();
        assert!(display.contains("1. first issue"));
        assert!(display.contains("2. second issue"));
    }

    #[test]
    fn multi_errors_extend() {
        let mut errors = MultiErrors::new();
        errors.extend(vec!["a".to_string(), "b".to_string()]);
        assert_eq!(errors.len(), 2);
    }

    #[test]
    fn multi_errors_into_inner() {
        let mut errors = MultiErrors::new();
        errors.push("test".to_string());
        let inner: Vec<String> = errors.into_inner();
        assert_eq!(inner.len(), 1);
    }

    #[test]
    fn multi_errors_from_vec() {
        let errors: MultiErrors<String> = MultiErrors::from(vec!["a".to_string()]);
        assert_eq!(errors.len(), 1);
    }

    #[test]
    fn multi_errors_into_iterator() {
        let mut errors = MultiErrors::new();
        errors.push("a".to_string());
        errors.push("b".to_string());
        let collected: Vec<String> = errors.into_iter().collect();
        assert_eq!(collected, vec!["a", "b"]);
    }

    #[test]
    fn multi_errors_slice_access() {
        let mut errors = MultiErrors::new();
        errors.push("err".to_string());
        assert_eq!(errors.as_slice(), &["err".to_string()]);
    }

    #[test]
    fn multi_errors_to_anyhow() {
        let mut errors = MultiErrors::new();
        errors.push("something broke".to_string());
        let err = errors.to_anyhow();
        assert!(err.to_string().contains("something broke"));
    }
}