ast: Make struct formatting look cool

This commit is contained in:
John 2025-10-07 05:43:19 -04:00
parent 4b0f30f78e
commit 2c63f1af63

View File

@ -127,7 +127,6 @@ pub struct Mod<A: Annotation = Span>(pub Ty, pub Anno<Expr<A>, A>);
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Struct(pub Pat);
/// Expressions: The beating heart of Dough
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Expr<A: Annotation = Span> {
@ -312,7 +311,15 @@ impl<A: Annotation> Display for Mod<A> {
impl Display for Struct {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let Self(pat) = self;
write!(f, "struct {pat}")
match pat {
Pat::Struct(name, bind) => match bind.as_ref() {
Pat::Tuple(parts) => f
.delimit_indented(fmt!("{name} {{"), "}")
.list_wrap("\n", parts, ",\n", ",\n"),
other => write!(f, "{name} {{ {other} }}"),
},
_ => write!(f, "struct {pat}"),
}
}
}
@ -438,7 +445,10 @@ impl Display for Pat {
Self::Lit(literal) => literal.fmt(f),
Self::MetId(name) => write!(f, "`{name}"),
Self::Name(name) => name.fmt(f),
Self::Struct(name, bind) => write!(f, "{name} {{{bind}}}"),
Self::Struct(name, bind) => match bind.as_ref() {
Pat::Tuple(parts) => f.delimit(fmt!("{name} {{"), "}").list(parts, ", "),
other => write!(f, "{name} {{ {other} }}"),
},
Self::TupStruct(name, bind) => write!(f, "{name} {bind}"),
Self::Rest(Some(rest)) => write!(f, "..{rest}"),
Self::Rest(None) => write!(f, ".."),