use unicode_bidi::{bidi_class, BidiClass, BidiInfo, ParagraphInfo};
#[derive(Debug)]
pub struct BidiParagraphs<'text> {
text: &'text str,
info: alloc::vec::IntoIter<ParagraphInfo>,
}
impl<'text> BidiParagraphs<'text> {
pub fn new(text: &'text str) -> Self {
let info = BidiInfo::new(text, None);
let info = info.paragraphs.into_iter();
Self { text, info }
}
}
impl<'text> Iterator for BidiParagraphs<'text> {
type Item = &'text str;
fn next(&mut self) -> Option<Self::Item> {
let para = self.info.next()?;
let paragraph = &self.text[para.range];
let mut char_indices = paragraph.char_indices();
if let Some(i) = char_indices.next_back().and_then(|(i, c)| {
(bidi_class(c) == BidiClass::B).then_some(i)
}) {
Some(¶graph[0..i])
} else {
Some(paragraph)
}
}
}