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
//! This module defines the [`StrStream`] and [`FromStream`] traits
//!
//! The [`FromStream`] trait defines how to convert a [`StrStream`] (a stream of strings) to a
//! value. See [its definition](FromStream) for more documentation.

use std::io;
use std::str::SplitWhitespace;

/// A streaming iterator yielding borrowed strings.
pub trait StrStream {
    fn next(&mut self) -> io::Result<Option<&str>>;
}

impl<'a> StrStream for SplitWhitespace<'a> {
    fn next(&mut self) -> io::Result<Option<&str>> { Ok(Iterator::next(self)) }
}

/// Fast version of [`std::str::SplitWhitespace`], but with some drawbacks.
///
/// It considers to be whitespace everything with codepoint <= 0x20
/// (this includes " \t\n\r", but also some other unprintable characters).
/// It doesn't consider to be whitespace any of non-ascii UTF whitespace characters
/// (such as non-breaking space).
pub struct SplitAsciiWhitespace<'a> {
    s: &'a str,
    position: usize,
}

impl<'a> SplitAsciiWhitespace<'a> {
    pub fn new(s: &'a str) -> Self { SplitAsciiWhitespace { s: s, position: 0 } }

    pub fn position(&self) -> usize { self.position }

    pub fn from_parts(s: &'a str, position: usize) -> Self {
        SplitAsciiWhitespace {
            s: s,
            position: position,
        }
    }
}

impl<'a> Iterator for SplitAsciiWhitespace<'a> {
    type Item = &'a str;

    fn next(&mut self) -> Option<&'a str> {
        let bytes = self.s.as_bytes();
        let mut start = self.position;
        while let Some(&c) = bytes.get(start) {
            if c > b' ' {
                break;
            }
            start += 1;
        }
        let mut end = start;
        while let Some(&c) = bytes.get(end) {
            if c <= b' ' {
                break;
            }
            end += 1;
        }
        self.position = end;
        if start != end {
            Some(&self.s[start..end])
        } else {
            None
        }
    }
}

impl<'a> StrStream for SplitAsciiWhitespace<'a> {
    fn next(&mut self) -> io::Result<Option<&str>> { Ok(Iterator::next(self)) }
}

/// Extends a `str` with `split_ascii_whitespace` method.
pub trait StrExt {
    fn split_ascii_whitespace(&self) -> SplitAsciiWhitespace;
}

impl StrExt for str {
    fn split_ascii_whitespace(&self) -> SplitAsciiWhitespace { SplitAsciiWhitespace::new(self) }
}

/// Trait for values that can be parsed from stream of whitespace-separated words.
///
/// Implementations for primitives consume and parse one element from a stream
/// (advancing a stream).
/// Implementations for tuples just parse elements from left to right.
/// Implementation for vector parses till the end of stream.
///
/// See [`adapters`](crate::adapters) module for more types that
/// implement this trait in a special way.
///
/// # Examples
///
/// Using a trait directly
///
/// ```
/// use whiteread::FromStream;
/// let mut stream = "123".split_whitespace();
/// assert_eq!(<i32 as FromStream>::read(&mut stream).unwrap(), 123)
/// ```
///
/// Semantics of provided trait implementations:
///
/// ```
/// # use whiteread::parse_string;
/// # use whiteread::adapters::{Skip, Lengthed};
/// // tuples (up to 6)
/// assert_eq!(parse_string("2 1 3 4").ok(), Some( ((2, 1), (3, 4)) ));
///
/// // optional elements at the end of stream
/// assert_eq!(parse_string("2 1 3").ok(), Some( ((2, 1), Some(3)) ));
/// assert_eq!(parse_string("2 1").ok(), Some( ((2, 1), None::<i32>) ));
///
/// // eager vector
/// assert_eq!(parse_string("2 1 3 4").ok(), Some( vec![2, 1, 3, 4] ));
///
/// // vec prefixed with length
/// assert_eq!(parse_string("2 1 3").ok(), Some( Lengthed(vec![1, 3]) ));
///
/// // placeholder
/// assert_eq!(parse_string("spam 3").ok(), Some( (Skip, 3) ));
///
/// // you can mix impls of course
/// assert_eq!(parse_string("a 1 b 2").ok(), Some( vec![('a', 1), ('b', 2)] ));
/// ```
///
/// There are a few more adapter structs in [`adapters`](crate::adapters) module,
/// which implement the `FromStream` trait in various ways.
pub trait FromStream: Sized {
    /// Reads a value from a str-producing stream.
    fn read<I: StrStream>(it: &mut I) -> Result<Self>;

    /// Whether a function returning this type should produce cheap errors instead of
    /// pretty-printed ones.
    ///
    /// For user-facing errors, this should be set to false, so that user can see a nice rendered
    /// error. This is the default behaviour.  If you expect the error to happen on a hot path and
    /// you don't just pass it up, this should be set to false. See
    /// [`WithCheapError`](crate::adapters::WithCheapError).
    const REQUEST_CHEAP_ERROR: bool = false;
}

pub type Result<T> = ::std::result::Result<T, Error>;

/// Specifies the flavour of [`TooShort`](Error::TooShort) error
#[derive(Debug)]
pub enum Progress {
    /// The stream didn't contain any data
    Nothing,

    /// The stream ended in the middle of parsing
    Partial,
}

/// Error which can occur while parsing [`FromStream`] object.
///
/// It's convertible into [`io::Error`](::std::io::Error),
/// so it composes well with other reading functions.
///
/// # Examples
///
/// ```
/// # use whiteread::parse_string;
/// # use whiteread::stream::Error::*;
/// # use whiteread::stream::Progress::*;
/// if let Err(TooShort(Nothing)) = parse_string::<(u8, u16)>("") {} else { panic!(); }
/// if let Err(TooShort(Partial)) = parse_string::<(u8, u16)>("1") {} else { panic!(); }
/// if let Err(Leftovers) = parse_string::<char>("x y z") {} else { panic!(); }
/// if let Err(ParseError) = parse_string::<i32>("seven") {} else { panic!(); }
/// ```
#[derive(Debug)]
pub enum Error {
    /// There was not enough input to parse a value.
    TooShort(Progress),

    /// Excessive input was provided.
    Leftovers,

    /// Parse error occured (data was in invalid format).
    ParseError,

    /// IO Error occured.
    IoError(io::Error),
}

/// # Variants checking
impl Error {
    /// Checks if error variant is [`TooShort`](Error::TooShort)
    pub fn is_too_short(&self) -> bool {
        match *self {
            Error::TooShort(_) => true,
            _ => false,
        }
    }

    /// Checks if error variant is [`Nothing`](Progress::Nothing) flavour
    /// of [`TooShort`](Error::TooShort)
    pub fn is_nothing(&self) -> bool {
        match *self {
            Error::TooShort(Progress::Nothing) => true,
            _ => false,
        }
    }

    /// Checks if error variant is [`Partial`](Progress::Partial) flavour
    /// of [`TooShort`](Error::TooShort)
    pub fn is_partial(&self) -> bool {
        match *self {
            Error::TooShort(Progress::Partial) => true,
            _ => false,
        }
    }

    /// Checks if error variant is [`Leftovers`](Error::Leftovers)
    pub fn is_leftovers(&self) -> bool {
        match *self {
            Error::Leftovers => true,
            _ => false,
        }
    }

    /// Checks if error variant is [`ParseError`](Error::ParseError)
    pub fn is_parse_error(&self) -> bool {
        match *self {
            Error::ParseError => true,
            _ => false,
        }
    }

    /// Checks if error variant is [`IoError`](Error::IoError)
    pub fn is_io_error(&self) -> bool {
        match *self {
            Error::IoError(_) => true,
            _ => false,
        }
    }
}

impl From<io::Error> for Error {
    fn from(e: io::Error) -> Error { Error::IoError(e) }
}

impl ::std::error::Error for Error {
    fn description(&self) -> &str {
        match *self {
            Error::TooShort(Progress::Nothing) => "not enough input to start parsing a value",
            Error::TooShort(Progress::Partial) => "not enough input to finish parsing a value",
            Error::Leftovers => "excessive input provided",
            Error::ParseError => "parse error occured",
            Error::IoError(ref e) => e.description(),
        }
    }

    fn cause(&self) -> Option<&::std::error::Error> {
        #[allow(deprecated)] // Rust 1.15 doesn't have Error::source yet
        match *self {
            Error::IoError(ref e) => e.cause(),
            _ => None,
        }
    }
}

impl ::std::fmt::Display for Error {
    fn fmt(&self, fmt: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
        use std::error::Error as _StdError;
        match *self {
            Error::IoError(ref e) => e.fmt(fmt),
            _ => fmt.write_str(self.description()),
        }
    }
}

impl From<Error> for io::Error {
    fn from(e: Error) -> io::Error {
        match e {
            Error::IoError(e) => e,
            e => io::Error::new(io::ErrorKind::InvalidData, e),
        }
    }
}

pub(crate) trait ResultExt {
    /// Treats the value as a subsequent part of bigger value,
    /// so we can assume that some part of it had already been parsed,
    /// and always emit `TooShort(Partial)`, never `TooShort(Nothing)`.
    fn as_subsequent(self) -> Self;
}

impl<T> ResultExt for Result<T> {
    fn as_subsequent(mut self) -> Self {
        if let Err(Error::TooShort(ref mut kind)) = self {
            *kind = Progress::Partial;
        }
        self
    }
}

// not using T: FromStr here because of coherence and tuples
macro_rules! impl_using_from_str {
    ($T:ident) => {
        impl FromStream for $T {
            fn read<I: StrStream>(it: &mut I) -> Result<$T> {
                it.next()?
                    .ok_or(Error::TooShort(Progress::Nothing))
                    .and_then(|s| s.parse().or(Err(Error::ParseError)))
            }
        }
    };
}

impl_using_from_str!(bool);
impl_using_from_str!(u8);
impl_using_from_str!(u16);
impl_using_from_str!(u32);
impl_using_from_str!(u64);
impl_using_from_str!(usize);
impl_using_from_str!(i8);
impl_using_from_str!(i16);
impl_using_from_str!(i32);
impl_using_from_str!(i64);
impl_using_from_str!(isize);
impl_using_from_str!(String);
impl_using_from_str!(f32);
impl_using_from_str!(f64);

impl FromStream for char {
    fn read<I: StrStream>(it: &mut I) -> Result<char> {
        let s = it.next()?;
        s.and_then(|s| s.chars().next()).ok_or(Error::TooShort(Progress::Nothing))
    }
}

impl FromStream for () {
    fn read<I: StrStream>(_: &mut I) -> Result<Self> { Ok(()) }
}

macro_rules! impl_tuple {
    ( $first:ident $(, $x:ident)* ) => {
        impl< $first: FromStream $( , $x: FromStream )* > FromStream for ( $first, $( $x ),* ) {
            fn read<I: StrStream>(it: &mut I) -> Result<Self> {
                Ok(( $first::read(it)?, $( $x::read(it).as_subsequent()? ),* ))
            }
        }
    };
}

impl_tuple!(A);
impl_tuple!(A, B);
impl_tuple!(A, B, C);
impl_tuple!(A, B, C, D);
impl_tuple!(A, B, C, D, E);
impl_tuple!(A, B, C, D, E, F);

/// `Option<T>` is read from the stream in a similar way
/// as `T`, except of returning `None` instead of
/// [`TooShort(Nothing)`](Progress::Nothing) error variant.
///
/// This allows for graceful handling of end-of-stream condition.
///
/// Note that partially parsed value (eg. half of a pair) is still
/// considered an error.
impl<T: FromStream> FromStream for Option<T> {
    fn read<I: StrStream>(it: &mut I) -> Result<Option<T>> {
        match FromStream::read(it) {
            Err(Error::TooShort(Progress::Nothing)) => Ok(None),
            result => Ok(Some(result?)),
        }
    }
}

/// Vector of elements is read by attempting to read elements
/// from stream until it's drained.
impl<T: FromStream> FromStream for Vec<T> {
    fn read<I: StrStream>(it: &mut I) -> Result<Vec<T>> {
        let mut v = vec![];
        while let Some(x) = FromStream::read(it)? {
            v.push(x);
        }
        Ok(v)
    }
}

#[test]
fn partial_vec() {
    type V = Vec<(u8, u8)>;
    assert_eq!(super::parse_string::<V>("1 2 3 4").unwrap().len(), 2);
    assert!(super::parse_string::<V>("1 2 3 4 5").unwrap_err().is_partial());
}