summaryrefslogtreecommitdiff
path: root/src/utils.rs
blob: e45785fa72ae6de6328745779f825ceb776095b1 (plain) (blame)
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
//! Some usefull functions

use serde::Deserialize;
use serde_json::{Deserializer, Error as JsonError};

/// Retuns (`last_line`, `remaining`). See tests for examples.
pub fn last_line(s: &[u8]) -> Option<(&[u8], &[u8])> {
    let mut it = memchr::memrchr_iter(b'\n', s);
    let last = it.next()?;
    let rem = &s[(last + 1)..];
    if let Some(pre_last) = it.next() {
        Some((&s[(pre_last + 1)..last], rem))
    } else {
        Some((&s[..last], rem))
    }
}

/// Deserialize the last complete object. Returns (`object`, `remaining`). See tests for examples.
pub fn de_last_json<'a, T: Deserialize<'a>>(
    mut s: &'a [u8],
) -> Result<(Option<T>, &'a [u8]), JsonError> {
    let mut last = None;
    let mut tmp;
    loop {
        (tmp, s) = de_first_json(s)?;
        last = match tmp {
            Some(obj) => Some(obj),
            None => return Ok((last, s)),
        };
    }
}

/// Deserialize the first complete object. Returns (`object`, `remaining`). See tests for examples.
pub fn de_first_json<'a, T: Deserialize<'a>>(
    mut s: &'a [u8],
) -> Result<(Option<T>, &'a [u8]), JsonError> {
    while s
        .first()
        .map_or(false, |&x| x == b' ' || x == b',' || x == b'\n')
    {
        s = &s[1..];
    }
    let mut de = Deserializer::from_slice(s).into_iter();
    match de.next() {
        Some(Ok(obj)) => Ok((Some(obj), &s[de.byte_offset()..])),
        Some(Err(e)) if e.is_eof() => Ok((None, &s[de.byte_offset()..])),
        Some(Err(e)) => Err(e),
        None => Ok((None, &s[de.byte_offset()..])),
    }
}

/// Returns a byte slice with leading ASCII whitespace bytes removed.
///
/// TODO: remove if/when slice::trim_ascii_start is stabilized.
pub fn trim_ascii_start(mut bytes: &[u8]) -> &[u8] {
    while let [first, rest @ ..] = bytes {
        if first.is_ascii_whitespace() {
            bytes = rest;
        } else {
            break;
        }
    }
    bytes
}

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

    macro_rules! str {
        ($str:expr) => {
            &$str.as_bytes()[..]
        };
    }

    #[test]
    fn streaming_json() {
        let s = b",[2]\n, [3], [4, 3],[32][3] ";
        assert_eq!(
            de_first_json::<Vec<u8>>(s).unwrap(),
            (Some(vec![2]), str!("\n, [3], [4, 3],[32][3] "))
        );
        assert_eq!(
            de_last_json::<Vec<u8>>(s).unwrap(),
            (Some(vec![3]), str!(""))
        );

        let s = b",[2]\n, [3], [4, 3],[32][3] [2, 4";
        assert_eq!(
            de_last_json::<Vec<u8>>(s).unwrap(),
            (Some(vec![3]), str!("[2, 4"))
        );

        let s = b",[2]\n, [3], [4, 3],[32] invalid";
        assert_eq!(
            de_first_json::<Vec<u8>>(s).unwrap(),
            (Some(vec![2]), str!("\n, [3], [4, 3],[32] invalid"))
        );
        assert!(de_last_json::<Vec<u8>>(s).is_err());
    }

    #[test]
    fn test_last_line() {
        let s = b"hello";
        assert_eq!(last_line(s), None);

        let s = b"hello\n";
        assert_eq!(last_line(s), Some((str!("hello"), str!(""))));

        let s = b"hello\nworld";
        assert_eq!(last_line(s), Some((str!("hello"), str!("world"))));

        let s = b"hello\nworld\n";
        assert_eq!(last_line(s), Some((str!("world"), str!(""))));

        let s = b"hello\nworld\n...";
        assert_eq!(last_line(s), Some((str!("world"), str!("..."))));
    }

    #[test]
    fn test_trim_start() {
        let s = b" ";
        assert_eq!(trim_ascii_start(s), b"");

        let s = b"hello";
        assert_eq!(trim_ascii_start(s), b"hello");

        let s = b"\t \nhello";
        assert_eq!(trim_ascii_start(s), b"hello");

        let s = b" \t \nhello\n";
        assert_eq!(trim_ascii_start(s), b"hello\n");
    }
}