ar_reshaper/
iterator.rs

1use alloc::string::String;
2
3use crate::{ArabicReshaper, ReshaperConfig};
4
5/// Iterator for the [ArabicReshaper], you can use this type to iterate over
6/// strings in a [Iterator] and reshape them
7pub struct ArabicReshaperIter<I>
8where
9    I: Iterator,
10{
11    reshaper: ArabicReshaper,
12    underlying: I,
13}
14
15impl<I> Iterator for ArabicReshaperIter<I>
16where
17    I: Iterator,
18    I::Item: AsRef<str>,
19{
20    type Item = String;
21
22    fn next(&mut self) -> Option<Self::Item> {
23        self.underlying.next().map(|v| self.reshaper.reshape(v))
24    }
25}
26
27/// Wrap an iterator to reshape strings
28pub trait ArabicReshaperExt: Iterator + Sized
29where
30    Self::Item: AsRef<str>,
31{
32    /// Reshape the iterator with the default [ArabicReshaper] config
33    fn reshape_default(self) -> ArabicReshaperIter<Self> {
34        ArabicReshaperIter {
35            reshaper: ArabicReshaper::default(),
36            underlying: self,
37        }
38    }
39
40    /// Reshape the iterator using the given config
41    fn reshape_with_config(self, config: ReshaperConfig) -> ArabicReshaperIter<Self> {
42        ArabicReshaperIter {
43            reshaper: ArabicReshaper::new(config),
44            underlying: self,
45        }
46    }
47}
48
49impl<I: Iterator> ArabicReshaperExt for I where I::Item: AsRef<str> {}