use std::{fmt::Display, str::FromStr};
use serde::{Deserialize, Serialize};
#[cfg_attr(
all(feature = "schemars", not(feature = "test")),
derive(schemars::JsonSchema)
)]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum RankDir {
LeftToRight,
RightToLeft,
#[default]
TopToBottom,
BottomToTop,
}
impl RankDir {
pub fn is_default(&self) -> bool {
matches!(self, RankDir::TopToBottom)
}
}
impl FromStr for RankDir {
type Err = ();
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"left_to_right" => Ok(RankDir::LeftToRight),
"right_to_left" => Ok(RankDir::RightToLeft),
"top_to_bottom" => Ok(RankDir::TopToBottom),
"bottom_to_top" => Ok(RankDir::BottomToTop),
_ => Err(()),
}
}
}
impl Display for RankDir {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
RankDir::LeftToRight => write!(f, "left_to_right"),
RankDir::RightToLeft => write!(f, "right_to_left"),
RankDir::TopToBottom => write!(f, "top_to_bottom"),
RankDir::BottomToTop => write!(f, "bottom_to_top"),
}
}
}