use std::borrow::Cow;
use serde::{Deserialize, Serialize, Serializer};
use crate::tag::{Tag, Tagged};
const TYPE_NAME: &str = "type";
#[derive(Deserialize, Clone, PartialEq, Eq, PartialOrd, Ord, Debug)]
#[serde(from = "TypeTypst<'static>")]
pub struct Type {
pub ty: String,
}
impl Type {
pub const fn new(ty: String) -> Self {
Self { ty }
}
}
#[derive(Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
struct TypeTypst<'a> {
typwire_type: Tag<Type>,
ty: Cow<'a, str>,
}
impl Tagged for Type {
const TYPE_NAME: &'static str = TYPE_NAME;
}
impl Serialize for Type {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
TypeTypst::from(self).serialize(serializer)
}
}
impl<'a> From<&'a Type> for TypeTypst<'a> {
fn from(value: &'a Type) -> Self {
Self {
typwire_type: Tag::new(),
ty: Cow::Borrowed(&value.ty),
}
}
}
impl From<TypeTypst<'_>> for Type {
fn from(value: TypeTypst<'_>) -> Self {
Self::new(value.ty.into_owned())
}
}