use core::fmt;
use crate::RdfDisplay;
#[derive(Debug, thiserror::Error)]
#[error("invalid base direction: {0:?} (expected \"ltr\" or \"rtl\")")]
pub struct InvalidDirection(pub String);
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize), serde(rename_all = "lowercase"))]
pub enum Direction {
Ltr,
Rtl,
}
impl Direction {
pub const fn as_str(self) -> &'static str {
match self {
Self::Ltr => "ltr",
Self::Rtl => "rtl",
}
}
pub const fn parse_const(s: &str) -> Option<Self> {
match s.as_bytes() {
b"ltr" => Some(Self::Ltr),
b"rtl" => Some(Self::Rtl),
_ => None,
}
}
pub fn parse(s: &str) -> Result<Self, InvalidDirection> {
Self::parse_const(s).ok_or_else(|| InvalidDirection(s.to_owned()))
}
}
impl fmt::Display for Direction {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str(self.as_str())
}
}
impl RdfDisplay for Direction {
fn rdf_fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str(self.as_str())
}
}
impl AsRef<str> for Direction {
fn as_ref(&self) -> &str {
self.as_str()
}
}
impl core::str::FromStr for Direction {
type Err = InvalidDirection;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Self::parse(s)
}
}