spinoso_string/
inspect.rs

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
use core::fmt;
use core::iter::FusedIterator;

use crate::enc;

/// An iterator that yields a debug representation of a `String` and its byte
/// contents as a sequence of `char`s.
///
/// This struct is created by the [`inspect`] method on [`String`]. See its
/// documentation for more.
///
/// To format a `String` directly into a writer, see [`format_into`] or
/// [`write_into`].
///
/// # Examples
///
/// To inspect an empty byte string:
///
/// ```
/// # extern crate alloc;
/// use alloc::string::String;
/// # use spinoso_string::Inspect;
/// let inspect = Inspect::default();
/// let debug = inspect.collect::<String>();
/// assert_eq!(debug, r#""""#);
/// ```
///
/// To inspect a well-formed UTF-8 byte string:
///
/// ```
/// # extern crate alloc;
/// use alloc::string::String;
/// # use spinoso_string::Inspect;
/// let s = spinoso_string::String::from("spinoso");
/// let inspect = s.inspect();
/// let debug = inspect.collect::<String>();
/// assert_eq!(debug, "\"spinoso\"");
/// ```
///
/// To inspect a byte string with invalid UTF-8 bytes:
///
/// ```
/// # extern crate alloc;
/// use alloc::string::String;
/// # use spinoso_string::Inspect;
/// let s = spinoso_string::String::utf8(b"invalid-\xFF-utf8".to_vec());
/// let inspect = s.inspect();
/// let debug = inspect.collect::<String>();
/// assert_eq!(debug, r#""invalid-\xFF-utf8""#);
/// ```
///
/// To inspect a binary string:
///
/// ```
/// # extern crate alloc;
/// use alloc::string::String;
/// # use spinoso_string::Inspect;
/// let s = spinoso_string::String::binary("💎".as_bytes().to_vec());
/// let inspect = s.inspect();
/// let debug = inspect.collect::<String>();
/// assert_eq!(debug, r#""\xF0\x9F\x92\x8E""#);
/// ```
///
/// [`inspect`]: crate::String::inspect
/// [`String`]: crate::String
/// [`format_into`]: Self::format_into
/// [`write_into`]: Self::write_into
#[derive(Default, Debug, Clone)]
#[must_use = "this `Inspect` is an `Iterator`, which should be consumed if constructed"]
pub struct Inspect<'a>(enc::Inspect<'a>);

impl<'a> Iterator for Inspect<'a> {
    type Item = char;

    fn next(&mut self) -> Option<Self::Item> {
        self.0.next()
    }
}

impl<'a> FusedIterator for Inspect<'a> {}

impl<'a> Inspect<'a> {
    pub(crate) fn new(value: enc::Inspect<'a>) -> Self {
        Self(value)
    }

    /// Write an `Inspect` iterator into the given destination using the debug
    /// representation of the byte buffer associated with a source `String`.
    ///
    /// This formatter writes content like `"spinoso"` and `"invalid-\xFF-utf8"`.
    /// To see example output of the underlying iterator, see the `Inspect`
    /// documentation.
    ///
    /// To write binary output, use [`write_into`], which requires the **std**
    /// feature to be activated.
    ///
    /// # Errors
    ///
    /// If the given writer returns an error as it is being written to, that
    /// error is returned.
    ///
    /// # Examples
    ///
    /// ```
    /// # extern crate alloc;
    /// # use core::fmt::Write;
    /// use alloc::string::String;
    /// # use spinoso_string::Inspect;
    /// let mut buf = String::new();
    /// let s = spinoso_string::String::from("spinoso");
    /// let iter = s.inspect();
    /// iter.format_into(&mut buf);
    /// assert_eq!(buf, "\"spinoso\"");
    ///
    /// let mut buf = String::new();
    /// let s = spinoso_string::String::utf8(b"\xFF".to_vec());
    /// let iter = s.inspect();
    /// iter.format_into(&mut buf);
    /// assert_eq!(buf, r#""\xFF""#);
    /// ```
    ///
    /// [`write_into`]: Self::write_into
    #[inline]
    pub fn format_into<W>(self, mut dest: W) -> fmt::Result
    where
        W: fmt::Write,
    {
        for ch in self {
            dest.write_char(ch)?;
        }
        Ok(())
    }

    /// Write an `Inspect` iterator into the given destination using the debug
    /// representation of the byte buffer associated with a source `String`.
    ///
    /// This formatter writes content like `"spinoso"` and `"invalid-\xFF-utf8"`.
    /// To see example output of the underlying iterator, see the `Inspect`
    /// documentation.
    ///
    /// To write to a [formatter], use [`format_into`].
    ///
    /// # Errors
    ///
    /// If the given writer returns an error as it is being written to, that
    /// error is returned.
    ///
    /// # Examples
    ///
    /// ```
    /// # use std::io::Write;
    /// # use spinoso_string::{Inspect, String};
    /// let mut buf = Vec::new();
    /// let s = String::from("spinoso");
    /// let iter = s.inspect();
    /// iter.write_into(&mut buf);
    /// assert_eq!(buf, &b"\"spinoso\""[..]);
    ///
    /// let mut buf = Vec::new();
    /// let s = String::utf8(b"\xFF".to_vec());
    /// let iter = s.inspect();
    /// iter.write_into(&mut buf);
    /// assert_eq!(buf, &[b'"', b'\\', b'x', b'F', b'F', b'"']);
    /// ```
    ///
    /// [formatter]: fmt::Write
    /// [`format_into`]: Self::format_into
    #[inline]
    #[cfg(feature = "std")]
    #[cfg_attr(docsrs, doc(cfg(feature = "std")))]
    pub fn write_into<W>(self, mut dest: W) -> std::io::Result<()>
    where
        W: std::io::Write,
    {
        let mut buf = [0; 4];
        for ch in self {
            let utf8 = ch.encode_utf8(&mut buf);
            dest.write_all(utf8.as_bytes())?;
        }
        Ok(())
    }
}

/// Helper iterator-ish struct for tracking when to emit wrapping quotes for
/// inspect iterators.
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct Flags {
    bits: u8,
}

impl Flags {
    // Bit flags
    const EMIT_LEADING_QUOTE: Self = Self { bits: 0b0000_0001 };
    const EMIT_TRAILING_QUOTE: Self = Self { bits: 0b0000_0010 };

    // Initial states
    pub const DEFAULT: Self = Self {
        bits: Self::EMIT_LEADING_QUOTE.bits | Self::EMIT_TRAILING_QUOTE.bits,
    };

    #[inline]
    pub fn emit_leading_quote(&mut self) -> Option<char> {
        if (self.bits & Self::EMIT_LEADING_QUOTE.bits) == Self::EMIT_LEADING_QUOTE.bits {
            self.bits &= !Self::EMIT_LEADING_QUOTE.bits;
            Some('"')
        } else {
            None
        }
    }

    #[inline]
    pub fn emit_trailing_quote(&mut self) -> Option<char> {
        if (self.bits & Self::EMIT_TRAILING_QUOTE.bits) == Self::EMIT_TRAILING_QUOTE.bits {
            self.bits &= !Self::EMIT_TRAILING_QUOTE.bits;
            Some('"')
        } else {
            None
        }
    }
}

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

    #[test]
    fn flags_default_emit_quotes() {
        let mut flags = Flags::DEFAULT;

        assert_eq!(flags.emit_leading_quote(), Some('"'));
        assert_eq!(flags.emit_leading_quote(), None);

        assert_eq!(flags.emit_trailing_quote(), Some('"'));
        assert_eq!(flags.emit_trailing_quote(), None);
    }
}