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
use nom::{
    branch::alt,
    character::complete::{char, multispace0},
    combinator::{map, opt},
    sequence::{preceded, separated_pair, terminated},
};
use serde_json_path_core::spec::selector::slice::Slice;

use crate::parser::{primitive::int::parse_int, PResult};

#[cfg_attr(feature = "trace", tracing::instrument(level = "trace", parent = None, ret, err))]
fn parse_int_space_after(input: &str) -> PResult<isize> {
    terminated(parse_int, multispace0)(input)
}

#[cfg_attr(feature = "trace", tracing::instrument(level = "trace", parent = None, ret, err))]
fn parse_int_space_before(input: &str) -> PResult<isize> {
    preceded(multispace0, parse_int)(input)
}

#[cfg_attr(feature = "trace", tracing::instrument(level = "trace", parent = None, ret, err))]
pub(crate) fn parse_array_slice(input: &str) -> PResult<Slice> {
    map(
        separated_pair(
            opt(parse_int_space_after),
            char(':'),
            preceded(
                multispace0,
                alt((
                    separated_pair(
                        opt(parse_int_space_after),
                        char(':'),
                        opt(parse_int_space_before),
                    ),
                    map(opt(parse_int_space_after), |i| (i, None)),
                )),
            ),
        ),
        |(start, (end, step))| Slice { start, end, step },
    )(input)
}

#[cfg(test)]
mod tests {
    use crate::parser::selector::slice::Slice;

    use super::parse_array_slice;

    #[test]
    fn valid_forward() {
        assert_eq!(
            parse_array_slice("1:5:1"),
            Ok(("", Slice::new().with_start(1).with_end(5).with_step(1)))
        );
        assert_eq!(
            parse_array_slice("1:10:3"),
            Ok(("", Slice::new().with_start(1).with_end(10).with_step(3)))
        );
        assert_eq!(
            parse_array_slice("1:5:-1"),
            Ok(("", Slice::new().with_start(1).with_end(5).with_step(-1)))
        );
        assert_eq!(
            parse_array_slice(":5:1"),
            Ok(("", Slice::new().with_end(5).with_step(1)))
        );
        assert_eq!(
            parse_array_slice("1::1"),
            Ok(("", Slice::new().with_start(1).with_step(1)))
        );
        assert_eq!(
            parse_array_slice("1:5"),
            Ok(("", Slice::new().with_start(1).with_end(5)))
        );
        assert_eq!(parse_array_slice("::"), Ok(("", Slice::new())));
    }

    #[test]
    fn optional_whitespace() {
        assert_eq!(
            parse_array_slice("1 :5:1"),
            Ok(("", Slice::new().with_start(1).with_end(5).with_step(1)))
        );
        assert_eq!(
            parse_array_slice("1: 5 :1"),
            Ok(("", Slice::new().with_start(1).with_end(5).with_step(1)))
        );
        assert_eq!(
            parse_array_slice("1: :1"),
            Ok(("", Slice::new().with_start(1).with_step(1)))
        );
        assert_eq!(
            parse_array_slice("1:5\n:1"),
            Ok(("", Slice::new().with_start(1).with_end(5).with_step(1)))
        );
        assert_eq!(
            parse_array_slice("1 : 5 :1"),
            Ok(("", Slice::new().with_start(1).with_end(5).with_step(1)))
        );
        assert_eq!(
            parse_array_slice("1:5: 1"),
            Ok(("", Slice::new().with_start(1).with_end(5).with_step(1)))
        );
    }
}