1use unicode_bidi::{bidi_class, BidiClass, BidiInfo, ParagraphInfo};
4
5#[derive(Debug)]
8pub struct BidiParagraphs<'text> {
9 text: &'text str,
10 info: alloc::vec::IntoIter<ParagraphInfo>,
11}
12
13impl<'text> BidiParagraphs<'text> {
14 pub fn new(text: &'text str) -> Self {
17 let info = BidiInfo::new(text, None);
18 let info = info.paragraphs.into_iter();
19 Self { text, info }
20 }
21}
22
23impl<'text> Iterator for BidiParagraphs<'text> {
24 type Item = &'text str;
25
26 fn next(&mut self) -> Option<Self::Item> {
27 let para = self.info.next()?;
28 let paragraph = &self.text[para.range];
29 let mut char_indices = paragraph.char_indices();
31 if let Some(i) = char_indices.next_back().and_then(|(i, c)| {
32 (bidi_class(c) == BidiClass::B).then_some(i)
34 }) {
35 Some(¶graph[0..i])
36 } else {
37 Some(paragraph)
38 }
39 }
40}