derive_struct/
derive_struct.rs

1
2fn main() {
3    slice::run();
4    string::run();
5}
6
7mod slice {
8    use std::ops::Range;
9    use splitter::{Info, Splitter};
10
11    pub fn run() {
12        #[derive(Debug, PartialEq, Info)]
13        struct Custom<'a, T> {
14            content: &'a [T],
15            span: Range<usize>,
16        }
17
18        let sp = Splitter::new(b"bytes example", b" ").with_info::<Custom<u8>>();
19        assert_eq!(
20            sp.collect::<Vec<_>>(),
21            vec![
22                Custom { content: b"bytes", span: 0..5 },
23                Custom { content: b" ", span: 5..6 },
24                Custom { content: b"example", span: 6..13 },
25            ],
26        );
27
28        #[derive(Debug, PartialEq, Info)]
29        // it is also possible to define the type of the slice
30        #[splitter(u8)]
31        struct CustomU8<'a> {
32            content: &'a [u8],
33            // this uses the 'impls' feature, which aautomatically implements `Info`
34            // for usefull types from `core` and `std`, see README.md for more details
35            span: Range<usize>,
36        }
37
38        let sp = Splitter::new(b"bytes example", b" ").with_info::<CustomU8>();
39        assert_eq!(
40            sp.collect::<Vec<_>>(),
41            vec![
42                CustomU8 { content: b"bytes", span: 0..5 },
43                CustomU8 { content: b" ", span: 5..6 },
44                CustomU8 { content: b"example", span: 6..13 },
45            ],
46        );
47    }
48}
49
50mod string {
51    use std::ops::Range;
52    use splitter::{StrInfo, StrSplitter};
53
54    pub fn run() {
55        #[derive(Debug, PartialEq, StrInfo)]
56        struct Custom<'a> {
57            content: &'a str,
58            // this uses the 'impls' feature, which aautomatically implements `StrInfo`
59            // for usefull types from `core` and `std`, see README.md for more details
60            span: Range<usize>,
61        }
62        
63        let sp = StrSplitter::new("bytes example", " ").with_info::<Custom>();
64        assert_eq!(
65            sp.collect::<Vec<_>>(),
66            vec![
67                Custom { content: "bytes", span: 0..5 },
68                Custom { content: " ", span: 5..6 },
69                Custom { content: "example", span: 6..13 },
70            ],
71        );
72    }
73}