use core::fmt;
use std::fmt::Write;
use crate::{Formatter, Type, type_def::TypeDef};
#[derive(Debug, Clone)]
pub struct TypeAlias {
type_def: TypeDef,
ty: Type,
}
impl TypeAlias {
pub fn new(name: impl ToString, ty: impl ToString) -> 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)?;
writeln!(fmt, ";")?;
Ok(())
}
pub fn type_def(&self) -> &Type {
&self.type_def.ty
}
pub fn vis(&mut self, vis: impl ToString) -> &mut Self {
self.type_def.vis(vis);
self
}
pub fn generic(&mut self, name: impl ToString) -> &mut Self {
self.type_def.ty.generic(name);
self
}
pub fn bound<T>(&mut self, name: impl ToString, ty: T) -> &mut Self
where
T: Into<Type>,
{
self.type_def.bound(name, ty);
self
}
pub fn doc(&mut self, docs: impl ToString) -> &mut Self {
self.type_def.doc(docs);
self
}
pub fn derive(&mut self, name: impl ToString) -> &mut Self {
self.type_def.derive(name);
self
}
pub fn allow(&mut self, allow: impl ToString) -> &mut Self {
self.type_def.allow(allow);
self
}
pub fn repr(&mut self, repr: impl ToString) -> &mut Self {
self.type_def.repr(repr);
self
}
pub fn set_ty(&mut self, ty: Type) {
self.ty = ty;
}
pub fn ty(&self) -> &Type {
&self.ty
}
}