facet_deserialize/
debug.rs

1//! Debug utilities for deserialization formats.
2
3use crate::span::Span;
4use alloc::borrow::Cow;
5
6/// Trait for handling input data in error reporting and debugging.
7/// Provides methods to convert input slices to human-readable strings
8/// and to create byte arrays for error storage.
9pub trait InputDebug {
10    /// Returns a string representation of the input at the given span.
11    /// Used for pretty error messages in diagnostics.
12    fn slice(&self, span: Span) -> &str;
13
14    /// Converts the input to a borrowed or owned byte array.
15    /// Used when constructing error objects that need to store input data.
16    fn as_cow(&self) -> Cow<'_, [u8]>;
17}
18
19impl InputDebug for [u8] {
20    fn slice(&self, span: Span) -> &str {
21        core::str::from_utf8(&self[span.start()..span.end()]).unwrap_or("<invalid utf8>")
22    }
23
24    fn as_cow(&self) -> Cow<'_, [u8]> {
25        alloc::borrow::Cow::Borrowed(self)
26    }
27}
28
29impl InputDebug for [&str] {
30    fn slice(&self, span: Span) -> &str {
31        // Simplified implementation - just return the argument at that position if it exists
32        if span.start() < self.len() {
33            self[span.start()]
34        } else {
35            "<out of bounds>"
36        }
37    }
38
39    fn as_cow(&self) -> Cow<'_, [u8]> {
40        // For CLI args, we join them for error reporting
41        let joined = self.join(" ");
42        Cow::Owned(joined.into_bytes())
43    }
44}