libhaystack 3.1.6

Rust implementation of the Haystack 4 data types, defs, filter, units, and encodings
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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
// Copyright (C) 2020 - 2022, J2 Innovations

//! Scanner implementation

use std::io::{Error, ErrorKind, Read};

/// Zinc decoder scanner
pub struct Scanner<'a, R: Read> {
    input: &'a mut R,
    pub cur: u8,
    pub(super) next: Option<Vec<u8>>,
    pub(super) last_peek: u8,
    pub is_eof: bool,
    pub(super) pos: u64,
    line: usize,
}

impl<'a, R: Read> Scanner<'a, R> {
    pub fn make(input: &'a mut R) -> Result<Self, Error> {
        let mut buf: [u8; 1] = [0];

        if let Err(err) = input.read_exact(&mut buf) {
            if err.kind() == ErrorKind::UnexpectedEof {
                Ok(Scanner {
                    cur: buf[0],
                    input,
                    next: None,
                    last_peek: 0xFF,
                    is_eof: true,
                    pos: 0,
                    line: 1,
                })
            } else {
                Err(err)
            }
        } else {
            Ok(Scanner {
                cur: buf[0],
                input,
                next: None,
                last_peek: 0xFF,
                is_eof: false,
                pos: 0,
                line: 1,
            })
        }
    }

    /// Consumes all space characters until EOS or non space character is found
    pub fn consume_spaces(&mut self) -> Result<(), Error> {
        loop {
            if !self.is_space() {
                return Ok(());
            }

            if let Err(err) = self.read() {
                if self.is_eof {
                    return Ok(());
                } else {
                    return Err(err);
                }
            }
        }
    }

    /// Consumes all white space characters, including new lines
    pub fn consume_white_spaces(&mut self) -> Result<(), Error> {
        loop {
            if !self.is_white_space() {
                return Ok(());
            }

            if let Err(err) = self.read() {
                if self.is_eof {
                    return Ok(());
                } else {
                    return Err(err);
                }
            }
        }
    }

    /// True if cur is a space char
    pub fn is_space(&self) -> bool {
        " \t".as_bytes().contains(&self.cur)
    }

    /// True if cur is a new line char
    pub fn is_newline(&self) -> bool {
        "\r\n".as_bytes().contains(&self.cur)
    }

    /// True if cur is a new line or a space char
    pub fn is_white_space(&self) -> bool {
        self.is_space() || self.is_newline()
    }

    /// True if cur is a digit char
    pub fn is_digit(&self) -> bool {
        self.cur.is_ascii_digit()
    }

    /// True if cur is a hex digit char
    pub fn is_hex_digit(&self) -> bool {
        self.cur.is_ascii_hexdigit()
    }

    /// True if cur is an upper case char
    pub fn is_upper(&self) -> bool {
        self.cur.is_ascii_uppercase()
    }

    /// True if cur is an lower case char
    pub fn is_lower(&self) -> bool {
        self.cur.is_ascii_lowercase()
    }

    /// True if cur is an alpha char
    pub fn is_alpha(&self) -> bool {
        self.is_lower() || self.is_upper()
    }

    /// True if cur is an alpha numeric char
    pub fn is_alpha_num(&self) -> bool {
        self.is_digit() || self.is_lower() || self.is_upper()
    }

    /// True if cur is included in the provide `chars` string
    pub fn is_any_of(&self, chars: &str) -> bool {
        chars.as_bytes().contains(&self.cur)
    }

    /// True if cur is included in the `range`
    pub fn is_in_range(&self, range: &std::ops::RangeInclusive<u8>) -> bool {
        range.contains(&self.cur)
    }

    /// Expect current char to match the `expect`, and read next char
    pub fn expect_and_consume(&mut self, expect: u8) -> Result<u8, Error> {
        if self.cur == expect {
            let cur = self.cur;
            if let Err(err) = self.read() {
                if self.is_eof {
                    return Ok(cur);
                } else {
                    return Err(err);
                }
            }
            Ok(cur)
        } else {
            self.make_expect_err(expect as char)
        }
    }

    /// Expect current char to be included in the `expect` string, and read next char
    pub fn expect_and_consume_any_of(&mut self, expect: &str) -> Result<u8, Error> {
        if self.is_any_of(expect) {
            let cur = self.cur;
            if let Err(err) = self.read() {
                if self.is_eof {
                    return Ok(cur);
                } else {
                    return Err(err);
                }
            }
            Ok(cur)
        } else {
            self.make_expect_err(expect)
        }
    }

    /// Expect current char to be included in the `range`, and read next char
    pub fn expect_and_consume_any_in_range(
        &mut self,
        range: &std::ops::RangeInclusive<u8>,
    ) -> Result<u8, Error> {
        if self.is_in_range(range) {
            let cur = self.cur;
            if let Err(err) = self.read() {
                if self.is_eof {
                    return Ok(cur);
                } else {
                    return Err(err);
                }
            }
            Ok(cur)
        } else {
            self.make_generic_err(&format!(
                "Expected '{range:?}', found '{cur}'",
                cur = self.cur
            ))
        }
    }

    /// Expect the input to match the `expect` sting, advances the input until complete match or error
    pub fn expect_and_consume_seq(&mut self, seq: &str) -> Result<(), Error> {
        for (i, c) in seq.as_bytes().iter().enumerate() {
            if c != &self.cur {
                return self.make_expect_err(c);
            }
            if let Err(err) = self.read() {
                if self.is_eof && i == seq.len() - 1 {
                    return Ok(());
                } else {
                    return Err(err);
                }
            }
        }
        Ok(())
    }

    /// Reads single char form input
    pub fn read(&mut self) -> Result<u8, Error> {
        if let Some(peek_bytes) = &mut self.next {
            self.cur = peek_bytes.remove(0);
            if peek_bytes.is_empty() {
                self.next = None;
            }

            self.increment_pos();
            Ok(self.cur)
        } else {
            match self.read_byte() {
                Ok(byte) => {
                    self.cur = byte;

                    self.increment_pos();
                    Ok(byte)
                }
                Err(err) => Err(err),
            }
        }
    }

    /// Advances the current input by one, errors out in case of IO Error.
    /// If current input is at EOS, silently ignore it and does nothing.
    pub fn advance(&mut self) -> Result<(), Error> {
        if let Err(err) = self.read() {
            if !self.is_eof { Err(err) } else { Ok(()) }
        } else {
            Ok(())
        }
    }

    /// Consume input until EOS or until the limit
    pub fn advance_by(&mut self, limit: u64) -> Result<u8, Error> {
        for _ in 0..limit {
            self.read()?;
        }
        Ok(self.cur)
    }

    /// Peeks for next element, errors out if end of stream or other read error
    /// is encountered.
    /// Each peek call will stash the value in an internal buffer, so a read call
    /// will consume the stashed data, then when stash is consumed, data is read from
    /// the stream again.
    pub fn peek(&mut self) -> Result<u8, Error> {
        let next = self.read_byte()?;

        Ok(self.buffer_peek(next))
    }

    /// Tries to peek any available data, returns `None`
    /// if there is any error while reading ahead
    /// See [peek](self::Scanner::peek)
    pub fn safe_peek(&mut self) -> Option<u8> {
        let next = match self.read_byte() {
            Ok(val) => val,
            Err(_) => return None,
        };

        Some(self.buffer_peek(next))
    }

    /// Create am error for an missing expect condition
    /// Current position and the current char are captured
    pub fn make_expect_err<T, E: std::fmt::Display>(&self, item: E) -> Result<T, Error> {
        Err(Error::new(
            ErrorKind::InvalidInput,
            format!(
                "Scanner expected '{item}', found {cur:?}. Input position: {pos}[{look_ahead}], line {line}",
                cur = self.cur as char,
                pos = self.pos,
                look_ahead = self.next.as_ref().map_or(0, |b| b.len() as u64),
                line = self.line
            ),
        ))
    }

    /// Create a generic error
    /// Current position is captured
    pub fn make_generic_err<T>(&self, msg: &str) -> Result<T, Error> {
        Err(Error::new(
            ErrorKind::InvalidInput,
            format!(
                "{msg} Input position: {pos}[{look_ahead}], line {line}",
                pos = self.pos,
                look_ahead = self.next.as_ref().map_or(0, |b| b.len() as u64),
                line = self.line
            ),
        ))
    }

    /// Increment current input position and line number
    fn increment_pos(&mut self) {
        self.pos += 1;
        if self.is_newline() {
            self.line += 1;
        }
    }

    fn buffer_peek(&mut self, next: u8) -> u8 {
        if self.next.is_none() {
            self.next = Some(Vec::new());
        }
        if let Some(peek_bytes) = &mut self.next {
            peek_bytes.push(next);
            self.last_peek = next;
        }
        next
    }

    fn read_byte(&mut self) -> Result<u8, Error> {
        let mut buf: [u8; 1] = [0];
        match self.input.read_exact(&mut buf) {
            Ok(_) => Ok(buf[0]),
            Err(err) => {
                if err.kind() == ErrorKind::UnexpectedEof {
                    self.is_eof = true;
                }
                Err(err)
            }
        }
    }
}

#[cfg(test)]
mod test {
    use std::io::Cursor;

    #[test]
    fn test_zinc_scanner_empty() {
        let mut input = Cursor::new("".as_bytes());

        let scanner = super::Scanner::make(&mut input).expect("Scanner");

        assert!(scanner.is_eof, "Should be eof");
    }

    #[test]
    fn test_zinc_scanner_consume_spaces() {
        let mut input = Cursor::new("   abc  def".as_bytes());

        let mut scanner = super::Scanner::make(&mut input).expect("Scanner");

        scanner.consume_spaces().expect("Valid");

        assert_eq!(scanner.cur, b'a');

        assert!(
            scanner.consume_spaces().is_ok(),
            "Should not consume spaces"
        );
        assert_eq!(scanner.cur, b'a');

        assert_eq!(scanner.advance_by(3).expect("Space"), b' ');

        scanner.consume_spaces().expect("Valid");

        assert_eq!(scanner.cur, b'd');
    }

    #[test]
    fn test_zinc_scanner_peek() {
        let mut input = Cursor::new("123".as_bytes());

        let mut scanner = super::Scanner::make(&mut input).expect("Scanner");

        scanner.consume_spaces().expect("Valid");

        assert_eq!(scanner.cur, b'1');

        assert_eq!(scanner.peek().ok(), Some(b'2'));
        assert_eq!(scanner.cur, b'1');

        assert_eq!(scanner.peek().ok(), Some(b'3'));
        assert_eq!(scanner.cur, b'1');

        assert_eq!(scanner.read().ok(), Some(b'2'));
        assert_eq!(scanner.cur, b'2');

        assert_eq!(scanner.read().ok(), Some(b'3'));
        assert_eq!(scanner.cur, b'3');

        assert!(scanner.peek().is_err());
    }

    #[test]
    fn test_zinc_scanner_is_any_of() {
        let mut input = Cursor::new("8".as_bytes());

        let scanner = super::Scanner::make(&mut input).expect("Scanner");

        assert!(scanner.is_any_of("8abc"));
    }

    #[test]
    fn test_zinc_scanner_expect_seq() {
        let mut input = Cursor::new("abcDEF".as_bytes());

        let mut scanner = super::Scanner::make(&mut input).expect("Scanner");

        assert_eq!(scanner.expect_and_consume_seq("abc").ok(), Some(()));
        assert_eq!(scanner.expect_and_consume_seq("DEF").ok(), Some(()));

        assert!(scanner.expect_and_consume_seq("123").is_err());
        assert!(scanner.expect_and_consume_seq("Fabc").is_err());
    }

    #[test]
    fn test_zinc_scanner_alpha_num_hex_space() {
        let mut input = Cursor::new("aB1 \t".as_bytes());

        let mut scanner = super::Scanner::make(&mut input).expect("Scanner");

        assert_eq!(scanner.cur, b'a');

        assert!(scanner.is_alpha());
        assert!(scanner.is_lower());
        assert!(scanner.is_hex_digit());
        assert!(!scanner.is_digit());

        assert!(scanner.read().is_ok());
        assert_eq!(scanner.cur, b'B');
        assert!(scanner.is_alpha());
        assert!(scanner.is_upper());
        assert!(!scanner.is_lower());
        assert!(scanner.is_hex_digit());
        assert!(!scanner.is_digit());

        assert!(scanner.read().is_ok());
        assert_eq!(scanner.cur, b'1');
        assert!(!scanner.is_alpha());
        assert!(!scanner.is_upper());
        assert!(!scanner.is_lower());
        assert!(scanner.is_hex_digit());
        assert!(scanner.is_digit());

        assert!(scanner.read().is_ok());
        assert_eq!(scanner.cur, b' ');
        assert!(scanner.is_space());
        assert!(!scanner.is_alpha());
        assert!(!scanner.is_upper());
        assert!(!scanner.is_lower());
        assert!(!scanner.is_hex_digit());
        assert!(!scanner.is_digit());

        assert!(scanner.read().is_ok());
        assert_eq!(scanner.cur, b'\t');
        assert!(scanner.is_space());
        assert!(!scanner.is_alpha());
        assert!(!scanner.is_upper());
        assert!(!scanner.is_lower());
        assert!(!scanner.is_hex_digit());
        assert!(!scanner.is_digit());

        assert!(scanner.read().is_err());
        assert!(scanner.is_eof);
    }
}