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)]
pub enum ProductionAttribute {
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 Default for ProductionAttribute {
fn default() -> Self {
Self::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!("{} /* `Vec<T>::New` */", decoratee))
}
Self::AddToCollection => {
out.write_fmt(format_args!("{} /* `Vec<T>::Push` */", decoratee))
}
Self::OptionalSome => {
out.write_fmt(format_args!("{} /* `Option<T>::Some` */", decoratee))
}
Self::OptionalNone => {
out.write_fmt(format_args!("{} /* `Option<T>::None` */", decoratee))
}
}
}
}
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize, TS)]
#[ts(export)]
pub enum SymbolAttribute {
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 Default for SymbolAttribute {
fn default() -> Self {
Self::None
}
}
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!("{} /* Vec */", decoratee)),
Self::Option => out.write_fmt(format_args!("{} /* Option */", decoratee)),
Self::Clipped => out.write_fmt(format_args!("{}^ /* Clipped */", decoratee)),
}
}
}