use unicode_segmentation::UnicodeSegmentation;
use unicode_width::UnicodeWidthStr;
pub(crate) mod sealed {
pub trait Gauge {
const WIDE: bool;
}
}
pub trait Gauge: sealed::Gauge {
type Caption<'a>: AsRef<str> + Copy;
}
#[derive(Debug)]
pub enum Wide {}
#[derive(Debug)]
pub enum Narrow {}
impl sealed::Gauge for Wide {
const WIDE: bool = true;
}
impl sealed::Gauge for Narrow {
const WIDE: bool = false;
}
impl Gauge for Wide {
type Caption<'a> = &'a str;
}
impl Gauge for Narrow {
type Caption<'a> = NarrowStr<'a>;
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct NarrowStr<'a>(&'a str);
impl<'a> NarrowStr<'a> {
pub fn new(text: &'a str) -> Option<Self> {
text.graphemes( true)
.all(|letter| letter.width() <= 1)
.then_some(Self(text))
}
}
impl AsRef<str> for NarrowStr<'_> {
fn as_ref(&self) -> &str {
self.0
}
}