derive_enum/
derive_enum.rs

1
2fn main() {
3    slice::run();
4    string::run();
5}
6
7mod slice {
8    use splitter::{Splitter, Info};
9
10    pub fn run() {
11        fn is_number(ts: &[u8]) -> bool {
12            ts == b"15"
13        }
14
15        //
16        // A enum with the 'Info'-derive can't have variants with fields
17        // 
18        // Every enum-variant needs a #[splitter(...)] attribute
19        //
20        // The attribute must contain a &[T] pattern (similar to match)
21        //
22        // The pattern can consist of a '|' seperated list of &[T], a variable name, or '_',
23        // followed by a optional if statement
24        //
25        // The Info will use the first match, so ordering is important
26        //
27
28        #[derive(Debug, PartialEq, Info)]
29        #[splitter(u8)]
30        enum SliceEnum {
31            #[splitter(b" " | b"\n")]
32            WS,
33
34            #[splitter(b":)")]
35            Smiley,
36
37            #[splitter(s if is_number(s))]
38            Number,
39
40            #[splitter(_)]
41            Other,
42        }
43
44        let sp = Splitter::new(
45            b"hello\nsplitter :) 15",
46            [" ", "\n"],
47        ).with_info::<SliceEnum>();
48
49        assert_eq!(
50            sp.collect::<Vec<_>>(),
51            vec![
52                SliceEnum::Other,
53                SliceEnum::WS,
54                SliceEnum::Other,
55                SliceEnum::WS,
56                SliceEnum::Smiley,
57                SliceEnum::WS,
58                SliceEnum::Number,
59            ],
60        );
61    }
62}
63
64mod string {
65    use splitter::{StrSplitter, StrInfo};
66
67    pub fn run() {
68        fn is_number(s: &str) -> bool {
69            // can do some regex here if you want
70            s.chars().all(char::is_numeric)
71        }
72
73        //
74        // A enum with the 'StrInfo'-derive can't have variants with fields
75        // 
76        // Every enum-variant needs a #[splitter(...)] attribute
77        //
78        // The attribute must contain a &str pattern (similar to match)
79        //
80        // The pattern can consist of a '|' seperated list of &str, a variable name, or '_',
81        // followed by a optional if statement
82        //
83        // The StrInfo will use the first match, so ordering is important
84        //
85
86        #[derive(Debug, PartialEq, StrInfo)]
87        enum StrEnum {
88            #[splitter(" " | "\n")]
89            WS,
90
91            #[splitter(":)")]
92            Smiley,
93
94            #[splitter(s if is_number(s))]
95            Number,
96
97            #[splitter(_)]
98            Other,
99        }
100
101        let sp = StrSplitter::new(
102            "hello\nsplitter :) 15",
103            [" ", "\n"],
104        ).with_info::<StrEnum>();
105
106        assert_eq!(
107            sp.collect::<Vec<_>>(),
108            vec![
109                StrEnum::Other,
110                StrEnum::WS,
111                StrEnum::Other,
112                StrEnum::WS,
113                StrEnum::Smiley,
114                StrEnum::WS,
115                StrEnum::Number,
116            ],
117        );
118    }
119}