use std::fmt::{Debug, Display, Error, Formatter, Write};
use serde::{Deserialize, Serialize};
use ts_rs::TS;
#[derive(Debug, Clone, Copy, Hash, Default, PartialEq, Eq, PartialOrd, Ord)]
pub struct OptionalId(usize);
pub trait Decorate<T, W>
where
T: Display,
W: Write,
{
fn decorate(&self, out: &mut W, decoratee: &T) -> std::result::Result<(), Error>;
}
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize, TS)]
#[ts(export)]
#[derive(Default)]
pub enum ProductionAttribute {
#[default]
None,
CollectionStart,
AddToCollection,
OptionalSome,
OptionalNone,
}
impl Display for ProductionAttribute {
fn fmt(&self, f: &mut Formatter<'_>) -> std::result::Result<(), Error> {
match self {
Self::None => write!(f, "-"),
Self::CollectionStart => write!(f, "Vec<T>::New"),
Self::AddToCollection => write!(f, "Vec<T>::Push"),
Self::OptionalSome => write!(f, "Option<T>::Some"),
Self::OptionalNone => write!(f, "Option<T>::None"),
}
}
}
impl<T, W> Decorate<T, W> for ProductionAttribute
where
T: Display,
W: Write,
{
fn decorate(&self, out: &mut W, decoratee: &T) -> std::result::Result<(), Error> {
match self {
Self::None => out.write_fmt(format_args!("{decoratee}")),
Self::CollectionStart => out.write_fmt(format_args!("{decoratee} /* Vec<T>::New */")),
Self::AddToCollection => out.write_fmt(format_args!("{decoratee} /* Vec<T>::Push */")),
Self::OptionalSome => out.write_fmt(format_args!("{decoratee} /* Option<T>::Some */")),
Self::OptionalNone => out.write_fmt(format_args!("{decoratee} /* Option<T>::None */")),
}
}
}
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize, TS)]
#[ts(export)]
#[derive(Default)]
pub enum SymbolAttribute {
#[default]
None,
RepetitionAnchor,
Option,
Clipped,
}
impl Display for SymbolAttribute {
fn fmt(&self, f: &mut Formatter<'_>) -> std::result::Result<(), Error> {
match self {
Self::None => Ok(()),
Self::RepetitionAnchor => write!(f, "`Vec<T>`"),
Self::Option => write!(f, "`Option<T>`"),
Self::Clipped => write!(f, "Clipped"),
}
}
}
impl<T, W> Decorate<T, W> for SymbolAttribute
where
T: Display,
W: std::fmt::Write,
{
fn decorate(&self, out: &mut W, decoratee: &T) -> std::result::Result<(), Error> {
match self {
Self::None => out.write_fmt(format_args!("{decoratee}")),
Self::RepetitionAnchor => out.write_fmt(format_args!("{decoratee} /* Vec */")),
Self::Option => out.write_fmt(format_args!("{decoratee} /* Option */")),
Self::Clipped => out.write_fmt(format_args!("{decoratee}^ /* Clipped */")),
}
}
}