use serde::{Deserialize, Serialize};
use std::fmt;
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum NodeKind {
Start,
End,
Custom(String),
}
impl NodeKind {
#[must_use]
pub fn encode(&self) -> String {
match self {
Self::Start => "Start".to_owned(),
Self::End => "End".to_owned(),
Self::Custom(s) => format!("Custom:{s}"),
}
}
pub fn decode(s: &str) -> Self {
match s {
"Start" => Self::Start,
"End" => Self::End,
_ => Self::Custom(s.strip_prefix("Custom:").unwrap_or(s).to_owned()),
}
}
#[must_use]
pub fn is_start(&self) -> bool {
matches!(self, Self::Start)
}
#[must_use]
pub fn is_end(&self) -> bool {
matches!(self, Self::End)
}
#[must_use]
pub fn is_custom(&self) -> bool {
matches!(self, Self::Custom(_))
}
#[must_use]
pub fn as_target(&self) -> String {
self.to_string()
}
#[must_use]
pub fn start_target() -> String {
"Start".to_owned()
}
#[must_use]
pub fn end_target() -> String {
"End".to_owned()
}
}
impl fmt::Display for NodeKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Start => f.write_str("Start"),
Self::End => f.write_str("End"),
Self::Custom(name) => f.write_str(name),
}
}
}
impl From<&str> for NodeKind {
fn from(s: &str) -> Self {
match s {
"Start" => Self::Start,
"End" => Self::End,
other => Self::Custom(other.to_owned()),
}
}
}
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum ChannelType {
Message,
Error,
Extra,
}
impl fmt::Display for ChannelType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Message => f.write_str("message"),
Self::Error => f.write_str("error"),
Self::Extra => f.write_str("extra"),
}
}
}