use core::fmt;
use std::fmt::Write;
use crate::{type_def::TypeDef, Formatter, Type};
#[derive(Debug, Clone)]
pub struct TypeAlias {
type_def: TypeDef,
ty: Type,
}
impl TypeAlias {
pub fn new(name: impl Into<String>, ty: impl Into<String>) -> Self {
Self {
type_def: TypeDef::new(name),
ty: Type::new(ty),
}
}
pub fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
self.type_def.fmt_head("type", &[], fmt)?;
write!(fmt, " = ")?;
self.ty.fmt(fmt)?;
write!(fmt, ";")?;
Ok(())
}
pub fn type_def(&self) -> &Type {
&self.type_def.ty
}
pub fn vis(&mut self, vis: impl Into<String>) -> &mut Self {
self.type_def.vis(vis);
self
}
pub fn generic(&mut self, name: impl Into<String>) -> &mut Self {
self.type_def.ty.generic(name);
self
}
pub fn bound<T>(&mut self, name: impl Into<String>, ty: T) -> &mut Self
where
T: Into<Type>,
{
self.type_def.bound(name.into(), ty);
self
}
pub fn doc(&mut self, docs: impl Into<String>) -> &mut Self {
self.type_def.doc(docs);
self
}
pub fn derive(&mut self, name: impl Into<String>) -> &mut Self {
self.type_def.derive(name);
self
}
pub fn allow(&mut self, allow: impl Into<String>) -> &mut Self {
self.type_def.allow(allow);
self
}
pub fn repr(&mut self, repr: impl Into<String>) -> &mut Self {
self.type_def.repr(repr.into());
self
}
pub fn set_ty(&mut self, ty: Type) {
self.ty = ty;
}
pub fn ty(&self) -> &Type {
&self.ty
}
}