focaccia/folding/mapping/
mod.rs

1use core::iter::FusedIterator;
2
3mod lookup;
4
5pub use lookup::lookup;
6
7#[derive(Debug, Clone, Copy)]
8pub enum Mode {
9    Full,
10    Turkic,
11}
12
13#[derive(Debug, Clone)]
14pub enum Mapping {
15    Empty,
16    Single(u32),
17    Double(u32, u32),
18    Triple(u32, u32, u32),
19}
20
21impl Mapping {
22    #[inline]
23    #[must_use]
24    pub fn len(&self) -> usize {
25        match self {
26            Self::Empty => 0,
27            Self::Single(..) => 1,
28            Self::Double(..) => 2,
29            Self::Triple(..) => 3,
30        }
31    }
32}
33
34impl IntoIterator for Mapping {
35    type Item = u32;
36    type IntoIter = Iter;
37
38    #[inline]
39    fn into_iter(self) -> Self::IntoIter {
40        Iter(self)
41    }
42}
43
44#[derive(Debug)]
45pub struct Iter(Mapping);
46
47impl Iterator for Iter {
48    type Item = u32;
49
50    #[inline]
51    fn next(&mut self) -> Option<Self::Item> {
52        let next = match self.0 {
53            Mapping::Empty => return None,
54            Mapping::Single(ch) => {
55                self.0 = Mapping::Empty;
56                ch
57            }
58            Mapping::Double(ch, next) => {
59                self.0 = Mapping::Single(next);
60                ch
61            }
62            Mapping::Triple(ch, next, after) => {
63                self.0 = Mapping::Double(next, after);
64                ch
65            }
66        };
67        Some(next)
68    }
69
70    #[inline]
71    fn size_hint(&self) -> (usize, Option<usize>) {
72        let len = self.0.len();
73        (len, Some(len))
74    }
75
76    #[inline]
77    fn count(self) -> usize {
78        self.0.len()
79    }
80}
81
82impl ExactSizeIterator for Iter {}
83
84impl FusedIterator for Iter {}
85
86#[cfg(test)]
87mod tests {
88    use core::fmt::Write as _;
89    use std::string::String;
90
91    use super::{Mapping, Mode};
92
93    #[test]
94    fn mode_debug_is_not_empty() {
95        let mut buf = String::new();
96        write!(&mut buf, "{:?}", Mode::Full).unwrap();
97        assert!(!buf.is_empty());
98
99        let mut buf = String::new();
100        write!(&mut buf, "{:?}", Mode::Turkic).unwrap();
101        assert!(!buf.is_empty());
102    }
103
104    #[test]
105    fn mapping_debug_is_not_empty() {
106        let mut buf = String::new();
107        write!(&mut buf, "{:?}", Mapping::Empty).unwrap();
108        assert!(!buf.is_empty());
109
110        let mut buf = String::new();
111        write!(&mut buf, "{:?}", Mapping::Single(0)).unwrap();
112        assert!(!buf.is_empty());
113
114        let mut buf = String::new();
115        write!(&mut buf, "{:?}", Mapping::Double(0, 0)).unwrap();
116        assert!(!buf.is_empty());
117
118        let mut buf = String::new();
119        write!(&mut buf, "{:?}", Mapping::Triple(0, 0, 0)).unwrap();
120        assert!(!buf.is_empty());
121    }
122
123    #[test]
124    fn mapping_iter_debug_is_not_empty() {
125        let mut buf = String::new();
126        write!(&mut buf, "{:?}", Mapping::Empty.into_iter()).unwrap();
127        assert!(!buf.is_empty());
128
129        let mut buf = String::new();
130        write!(&mut buf, "{:?}", Mapping::Single(0).into_iter()).unwrap();
131        assert!(!buf.is_empty());
132
133        let mut buf = String::new();
134        write!(&mut buf, "{:?}", Mapping::Double(0, 0).into_iter()).unwrap();
135        assert!(!buf.is_empty());
136
137        let mut buf = String::new();
138        write!(&mut buf, "{:?}", Mapping::Triple(0, 0, 0).into_iter()).unwrap();
139        assert!(!buf.is_empty());
140    }
141
142    #[test]
143    fn mapping_single_iter() {
144        let mapping = Mapping::Single(20);
145        let mut iter = mapping.into_iter();
146        assert_eq!(iter.next(), Some(20_u32));
147        assert_eq!(iter.next(), None);
148        assert_eq!(iter.next(), None);
149        assert_eq!(iter.next(), None);
150        assert_eq!(iter.next(), None);
151        assert_eq!(iter.next(), None);
152    }
153
154    #[test]
155    fn mapping_single_count() {
156        let mapping = Mapping::Single(20);
157
158        let iter = mapping.clone().into_iter();
159        assert_eq!(iter.size_hint(), (1_usize, Some(1_usize)));
160        assert_eq!(iter.count(), 1);
161
162        let mut iter = mapping.clone().into_iter();
163        iter.next();
164        assert_eq!(iter.size_hint(), (0_usize, Some(0_usize)));
165        assert_eq!(iter.count(), 0);
166
167        let mut iter = mapping.clone().into_iter();
168        iter.next();
169        iter.next();
170        assert_eq!(iter.size_hint(), (0_usize, Some(0_usize)));
171        assert_eq!(iter.count(), 0);
172
173        let mut iter = mapping.clone().into_iter();
174        iter.next();
175        iter.next();
176        iter.next();
177        assert_eq!(iter.size_hint(), (0_usize, Some(0_usize)));
178        assert_eq!(iter.count(), 0);
179
180        let mut iter = mapping.into_iter();
181        iter.next();
182        iter.next();
183        iter.next();
184        iter.next();
185        assert_eq!(iter.size_hint(), (0_usize, Some(0_usize)));
186        assert_eq!(iter.count(), 0);
187    }
188
189    #[test]
190    fn mapping_double_iter() {
191        let mapping = Mapping::Double(20, 30);
192        let mut iter = mapping.into_iter();
193        assert_eq!(iter.next(), Some(20_u32));
194        assert_eq!(iter.next(), Some(30_u32));
195        assert_eq!(iter.next(), None);
196        assert_eq!(iter.next(), None);
197        assert_eq!(iter.next(), None);
198        assert_eq!(iter.next(), None);
199    }
200
201    #[test]
202    fn mapping_double_count() {
203        let mapping = Mapping::Double(20, 30);
204
205        let iter = mapping.clone().into_iter();
206        assert_eq!(iter.size_hint(), (2_usize, Some(2_usize)));
207        assert_eq!(iter.count(), 2);
208
209        let mut iter = mapping.clone().into_iter();
210        iter.next();
211        assert_eq!(iter.size_hint(), (1_usize, Some(1_usize)));
212        assert_eq!(iter.count(), 1);
213
214        let mut iter = mapping.clone().into_iter();
215        iter.next();
216        iter.next();
217        assert_eq!(iter.size_hint(), (0_usize, Some(0_usize)));
218        assert_eq!(iter.count(), 0);
219
220        let mut iter = mapping.clone().into_iter();
221        iter.next();
222        iter.next();
223        iter.next();
224        assert_eq!(iter.size_hint(), (0_usize, Some(0_usize)));
225        assert_eq!(iter.count(), 0);
226
227        let mut iter = mapping.into_iter();
228        iter.next();
229        iter.next();
230        iter.next();
231        iter.next();
232        assert_eq!(iter.size_hint(), (0_usize, Some(0_usize)));
233        assert_eq!(iter.count(), 0);
234    }
235
236    #[test]
237    fn mapping_triple_iter() {
238        let mapping = Mapping::Triple(20, 30, 40);
239        let mut iter = mapping.into_iter();
240        assert_eq!(iter.next(), Some(20_u32));
241        assert_eq!(iter.next(), Some(30_u32));
242        assert_eq!(iter.next(), Some(40_u32));
243        assert_eq!(iter.next(), None);
244        assert_eq!(iter.next(), None);
245        assert_eq!(iter.next(), None);
246    }
247
248    #[test]
249    fn mapping_triple_count() {
250        let mapping = Mapping::Triple(20, 30, 40);
251
252        let iter = mapping.clone().into_iter();
253        assert_eq!(iter.size_hint(), (3_usize, Some(3_usize)));
254        assert_eq!(iter.count(), 3);
255
256        let mut iter = mapping.clone().into_iter();
257        iter.next();
258        assert_eq!(iter.size_hint(), (2_usize, Some(2_usize)));
259        assert_eq!(iter.count(), 2);
260
261        let mut iter = mapping.clone().into_iter();
262        iter.next();
263        iter.next();
264        assert_eq!(iter.size_hint(), (1_usize, Some(1_usize)));
265        assert_eq!(iter.count(), 1);
266
267        let mut iter = mapping.clone().into_iter();
268        iter.next();
269        iter.next();
270        iter.next();
271        assert_eq!(iter.size_hint(), (0_usize, Some(0_usize)));
272        assert_eq!(iter.count(), 0);
273
274        let mut iter = mapping.into_iter();
275        iter.next();
276        iter.next();
277        iter.next();
278        iter.next();
279        assert_eq!(iter.size_hint(), (0_usize, Some(0_usize)));
280        assert_eq!(iter.count(), 0);
281    }
282}