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
#![cfg_attr(not(feature = "std"), no_std)]
//
#![cfg_attr(docsrs, feature(doc_cfg))]
//
#![doc = include_str!("../README.md")]
//
#![deny(anonymous_parameters)]
#![deny(nonstandard_style)]
#![deny(rust_2018_idioms)]
#![deny(trivial_numeric_casts)]
#![deny(unsafe_code)]
#![deny(rustdoc::broken_intra_doc_links)]
#![deny(unused)]
#![deny(unreachable_pub)]
//
// Warn (try not to do this)
#![warn(missing_copy_implementations)]
#![warn(missing_debug_implementations)]
#![warn(variant_size_differences)]
#![warn(missing_docs)]
//
// Clippy
#![warn(clippy::pedantic)]

use bytes::Bytes;
use core::fmt::Display;
use core::iter::Enumerate;
use core::ops::{Range, RangeFrom, RangeFull, RangeTo};
use core::str::Utf8Error;
use nom::{
    AsBytes, Compare, InputIter, InputLength, InputTake, InputTakeAtPosition, Needed, Offset, Slice,
};

mod range_type;
pub use range_type::RangeType;

#[cfg(feature = "miette")]
#[cfg_attr(docsrs, doc(cfg(feature = "miette")))]
mod miette;

/// A wrapper around [`bytes::Bytes`] to be able to use it with [`nom`].
#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct NomBytes(Bytes, Option<RangeType<usize>>);

// Why the extra `Option<RangeType<usize>>`? Nom expects to be able to calculate
// offsets between two of its inputs, but `Bytes` has this optimization where if
// slicing results in an empty slice, it returns a new, empty `Bytes` rather than
// an empty slice of the existing `Bytes`. This causes problems down the line when
// nom asks for offsets between two inputs. Thus, in cases where slicing would
// result in an empty slice, we instead store the original `Bytes` plus the slice
// range itself, which we can use to hand out correct offsets.
//
// All the code here uses `bytes()` or `as_bytes()` for doing operations on the
// underlying bytes rather than accessing the "raw" `.0` field, because those two
// contain code that handles this custom slicing correctly, and thus we don't have
// to be careful anywhere else.
//
// Tried reporting this as unexpected/incorrect behavior, but it was said to be an
// intentional behavior:
// <https://github.com/tokio-rs/bytes/issues/557>

impl NomBytes {
    /// Creates a new `NomBytes` wrapping the provided [`Bytes`].
    ///
    /// # Examples
    ///
    /// ```
    /// use bytes::Bytes;
    /// use nombytes::NomBytes;
    ///
    /// let b = Bytes::new();
    /// let nb = NomBytes::new(b);
    /// ```
    #[inline]
    pub fn new(bytes: Bytes) -> Self {
        Self(bytes, None)
    }

    /// Returns a string slice to the contents of the inner [`Bytes`].
    ///
    /// # Examples
    ///
    /// ```
    /// use bytes::Bytes;
    /// use nombytes::NomBytes;
    ///
    /// let nb = NomBytes::new(Bytes::from("hello"));
    /// assert_eq!(nb.to_str(), "hello");
    /// ```
    ///
    /// # Panics
    ///
    /// Panics if the [`Bytes`] slice is not UTF-8.
    #[inline]
    pub fn to_str(&self) -> &str {
        self.try_to_str().unwrap()
    }

    /// Returns a string slice to the contents of the inner [`Bytes`].
    ///
    /// # Examples
    ///
    /// ```
    /// use bytes::Bytes;
    /// use nombytes::NomBytes;
    ///
    /// let nb = NomBytes::new(Bytes::from("hello"));
    /// assert_eq!(nb.try_to_str().unwrap(), "hello");
    /// ```
    ///
    /// # Errors
    ///
    /// Returns `Err` if the [`Bytes`] slice is not UTF-8 with a description
    /// as to why the provided slice is not UTF-8.
    #[inline]
    pub fn try_to_str(&self) -> Result<&str, Utf8Error> {
        core::str::from_utf8(self.as_bytes())
    }

    #[doc = include_str!("to_bytes_doc.md")]
    /// # Examples
    ///
    /// ```
    /// use bytes::Bytes;
    /// use nombytes::NomBytes;
    ///
    /// let nb = NomBytes::new(Bytes::from("hello"));
    /// let b = nb.to_bytes();
    /// assert_eq!(b.as_ref(), b"hello");
    /// ```
    #[inline]
    pub fn to_bytes(&self) -> Bytes {
        match self.1.as_ref() {
            Some(range) => self.0.slice(range.clone()),
            None => self.0.clone(),
        }
    }

    #[doc = include_str!("to_bytes_doc.md")]
    /// # Examples
    ///
    /// ```
    /// use bytes::Bytes;
    /// use nombytes::NomBytes;
    ///
    /// let nb = NomBytes::new(Bytes::from("hello"));
    /// let b = nb.into_bytes();
    /// assert_eq!(b.as_ref(), b"hello");
    /// ```
    #[inline]
    pub fn into_bytes(self) -> Bytes {
        match self.1.as_ref() {
            Some(range) => self.0.slice(range.clone()),
            None => self.0,
        }
    }

    /// Returns the values from the inner representation of this type.
    ///
    /// See [`into_bytes`](Self::into_bytes) for an explanation of why this
    /// inner representation exists.
    // I dunno what anyone would use this for, but... might as well
    // offer it.
    pub fn into_raw(self) -> (Bytes, Option<RangeType<usize>>) {
        let Self(bytes, range_type) = self;
        (bytes, range_type)
    }

    /// Returns a new `NomBytes` using the raw values passed in. If these
    /// values represent something invalid, you'll likely see incorrect
    /// behavior or even panics. Regular usage should create values using
    /// [`new`](Self::new) instead.
    ///
    /// See [`into_bytes`](Self::into_bytes) for an explanation of why this
    /// inner representation exists.
    // I dunno what anyone would use this for, but... might as well
    // offer it.
    pub fn from_raw((bytes, range_type): (Bytes, Option<RangeType<usize>>)) -> Self {
        Self(bytes, range_type)
    }
}

impl AsBytes for NomBytes {
    #[inline]
    fn as_bytes(&self) -> &[u8] {
        match self.1.as_ref() {
            Some(range) => range.slice(self.0.as_ref()),
            None => self.0.as_ref(),
        }
    }
}

impl InputIter for NomBytes {
    type Item = u8;
    type Iter = Enumerate<Self::IterElem>;
    type IterElem = bytes::buf::IntoIter<Bytes>;

    #[inline]
    fn iter_indices(&self) -> Self::Iter {
        self.iter_elements().enumerate()
    }

    #[inline]
    fn iter_elements(&self) -> Self::IterElem {
        self.to_bytes().into_iter()
    }

    #[inline]
    fn position<P>(&self, predicate: P) -> Option<usize>
    where
        P: Fn(Self::Item) -> bool,
    {
        self.as_bytes().iter().position(|b| predicate(*b))
    }

    #[inline]
    fn slice_index(&self, count: usize) -> Result<usize, nom::Needed> {
        if self.as_bytes().len() >= count {
            Ok(count)
        } else {
            Err(Needed::new(count - self.as_bytes().len()))
        }
    }
}

impl InputTake for NomBytes {
    #[inline]
    fn take(&self, count: usize) -> Self {
        self.slice(..count)
    }

    #[inline]
    fn take_split(&self, count: usize) -> (Self, Self) {
        let prefix = self.slice(..count);
        let suffix = self.slice(count..);
        (suffix, prefix)
    }
}

impl InputTakeAtPosition for NomBytes {
    type Item = <Self as InputIter>::Item;

    fn split_at_position<P, E: nom::error::ParseError<Self>>(
        &self,
        predicate: P,
    ) -> nom::IResult<Self, Self, E>
    where
        P: Fn(Self::Item) -> bool,
    {
        match self.as_bytes().iter().position(|c| predicate(*c)) {
            Some(i) => Ok(self.take_split(i)),
            None => Err(nom::Err::Incomplete(Needed::new(1))),
        }
    }

    fn split_at_position1<P, E: nom::error::ParseError<Self>>(
        &self,
        predicate: P,
        e: nom::error::ErrorKind,
    ) -> nom::IResult<Self, Self, E>
    where
        P: Fn(Self::Item) -> bool,
    {
        match self.as_bytes().iter().position(|c| predicate(*c)) {
            Some(0) => Err(nom::Err::Error(E::from_error_kind(self.clone(), e))),
            Some(i) => Ok(self.take_split(i)),
            None => Err(nom::Err::Incomplete(Needed::new(1))),
        }
    }

    fn split_at_position_complete<P, E: nom::error::ParseError<Self>>(
        &self,
        predicate: P,
    ) -> nom::IResult<Self, Self, E>
    where
        P: Fn(Self::Item) -> bool,
    {
        match self.as_bytes().iter().position(|c| predicate(*c)) {
            Some(i) => Ok(self.take_split(i)),
            None => Ok(self.take_split(self.input_len())),
        }
    }

    fn split_at_position1_complete<P, E: nom::error::ParseError<Self>>(
        &self,
        predicate: P,
        e: nom::error::ErrorKind,
    ) -> nom::IResult<Self, Self, E>
    where
        P: Fn(Self::Item) -> bool,
    {
        let bytes = self.as_bytes();
        match bytes.iter().position(|c| predicate(*c)) {
            Some(0) => Err(nom::Err::Error(E::from_error_kind(self.clone(), e))),
            Some(i) => Ok(self.take_split(i)),
            None => {
                if bytes.is_empty() {
                    Err(nom::Err::Error(E::from_error_kind(self.clone(), e)))
                } else {
                    Ok(self.take_split(self.input_len()))
                }
            }
        }
    }
}

impl InputLength for NomBytes {
    #[inline]
    fn input_len(&self) -> usize {
        self.as_bytes().len()
    }
}

macro_rules! nom_bytes_slice {
    ($range_ty:ty, $requirement:expr) => {
        impl Slice<$range_ty> for NomBytes {
            fn slice(&self, range: $range_ty) -> Self {
                let bytes = self.to_bytes();
                if bytes.is_empty() && $requirement(&range) {
                    return self.clone();
                }

                let slice = bytes.slice(range.clone());
                if slice.is_empty() {
                    NomBytes(bytes, Some(RangeType::from(range)))
                } else {
                    assert!(!slice.is_empty());
                    NomBytes(slice, None)
                }
            }
        }
    };
}

nom_bytes_slice!(Range<usize>, |r: &Range<usize>| r.start == 0 && r.end == 0);
nom_bytes_slice!(RangeTo<usize>, |r: &RangeTo<usize>| r.end == 0);
nom_bytes_slice!(RangeFrom<usize>, |r: &RangeFrom<usize>| r.start == 0);
nom_bytes_slice!(RangeFull, |_: &RangeFull| true);

impl Offset for NomBytes {
    #[inline]
    fn offset(&self, second: &Self) -> usize {
        self.as_bytes().offset(second.as_bytes())
    }
}

impl Compare<NomBytes> for NomBytes {
    #[inline]
    fn compare(&self, t: NomBytes) -> nom::CompareResult {
        self.as_bytes().compare(t.as_bytes())
    }

    #[inline]
    fn compare_no_case(&self, t: NomBytes) -> nom::CompareResult {
        self.as_bytes().compare_no_case(t.as_bytes())
    }
}

impl Compare<&'_ str> for NomBytes {
    #[inline]
    fn compare(&self, t: &str) -> nom::CompareResult {
        self.as_bytes().compare(t.as_bytes())
    }

    #[inline]
    fn compare_no_case(&self, t: &str) -> nom::CompareResult {
        self.as_bytes().compare_no_case(t.as_bytes())
    }
}

impl Display for NomBytes {
    #[inline]
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        write!(f, "{}", self.to_str())
    }
}

impl From<&'_ str> for NomBytes {
    #[inline]
    fn from(string: &'_ str) -> Self {
        Self::from(string.as_bytes())
    }
}

impl From<&'_ [u8]> for NomBytes {
    #[inline]
    fn from(byte_slice: &'_ [u8]) -> Self {
        use bytes::{BufMut, BytesMut};

        let mut buf = BytesMut::with_capacity(byte_slice.len());
        buf.put(byte_slice);
        Self::new(buf.into())
    }
}

#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
impl From<String> for NomBytes {
    #[inline]
    fn from(string: String) -> Self {
        Self::new(Bytes::from(string))
    }
}

// We implement the eq/ord traits in terms of &[u8] since it's both
// cheap and easy:

impl PartialEq for NomBytes {
    fn eq(&self, other: &Self) -> bool {
        self.as_bytes().eq(other.as_bytes())
    }
}
impl Eq for NomBytes {}

impl PartialOrd for NomBytes {
    fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
        self.as_bytes().partial_cmp(other.as_bytes())
    }
}
impl Ord for NomBytes {
    fn cmp(&self, other: &Self) -> core::cmp::Ordering {
        self.as_bytes().cmp(other.as_bytes())
    }
}