use unicode_bidi::{BidiInfo, ParagraphInfo};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ParagraphDirection {
LeftToRight,
RightToLeft,
Neutral,
}
#[must_use]
pub fn paragraph_direction(text: &str) -> ParagraphDirection {
let info = BidiInfo::new(text, None);
if info.paragraphs.is_empty() {
return ParagraphDirection::Neutral;
}
let p: &ParagraphInfo = &info.paragraphs[0];
let level = p.level;
if level.is_rtl() {
ParagraphDirection::RightToLeft
} else if has_strong_ltr(text) {
ParagraphDirection::LeftToRight
} else {
ParagraphDirection::Neutral
}
}
#[must_use]
pub fn level_at(text: &str, byte_offset: usize) -> Option<u8> {
if byte_offset > text.len() {
return None;
}
let info = BidiInfo::new(text, None);
info.levels.get(byte_offset).map(|l| l.number())
}
fn has_strong_ltr(text: &str) -> bool {
text.chars().any(|c| {
let class = unicode_bidi::bidi_class(c);
matches!(class, unicode_bidi::BidiClass::L)
})
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn empty_string_is_neutral() {
assert_eq!(paragraph_direction(""), ParagraphDirection::Neutral);
}
#[test]
fn pure_ascii_is_ltr() {
assert_eq!(
paragraph_direction("hello"),
ParagraphDirection::LeftToRight
);
}
#[test]
fn arabic_paragraph_is_rtl() {
assert_eq!(
paragraph_direction("مرحبا"),
ParagraphDirection::RightToLeft
);
}
#[test]
fn level_at_returns_run_level() {
let text = "hello مرحبا";
assert_eq!(level_at(text, 0), Some(0));
let arabic_byte = text.find('م').unwrap();
let level = level_at(text, arabic_byte).unwrap();
assert!(level % 2 == 1, "expected RTL level, got {level}");
}
#[test]
fn out_of_bounds_returns_none() {
assert_eq!(level_at("abc", 999), None);
}
}