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
use crate::{ArabicReshaper, ReshaperConfig};

/// Iterator for the [ArabicReshaper], you can use this type to iterate over
/// strings in a [Iterator] and reshape them
pub struct ArabicReshaperIter<I>
where
    I: Iterator,
{
    reshaper: ArabicReshaper,
    underlying: I,
}

impl<I> Iterator for ArabicReshaperIter<I>
where
    I: Iterator,
    I::Item: AsRef<str>,
{
    type Item = String;

    fn next(&mut self) -> Option<Self::Item> {
        self.underlying.next().map(|v| self.reshaper.reshape(v))
    }
}

/// Wrap an iterator to reshape strings
pub trait ArabicReshaperExt: Iterator + Sized
where
    Self::Item: AsRef<str>,
{
    /// Reshape the iterator with the default [ArabicReshaper] config
    fn reshape_default(self) -> ArabicReshaperIter<Self> {
        ArabicReshaperIter {
            reshaper: ArabicReshaper::default(),
            underlying: self,
        }
    }

    /// Reshape the iterator using the given config
    fn reshape_with_config(self, config: ReshaperConfig) -> ArabicReshaperIter<Self> {
        ArabicReshaperIter {
            reshaper: ArabicReshaper::new(config),
            underlying: self,
        }
    }
}

impl<I: Iterator> ArabicReshaperExt for I where I::Item: AsRef<str> {}