From bdf0bb68ca1a343754c760af332ba7cfbcfaf368 Mon Sep 17 00:00:00 2001 From: John Date: Mon, 1 Apr 2024 04:18:31 -0500 Subject: [PATCH] cl-ast: improve formatting of enums --- cl-ast/src/ast_impl.rs | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/cl-ast/src/ast_impl.rs b/cl-ast/src/ast_impl.rs index 6c35567..de31185 100644 --- a/cl-ast/src/ast_impl.rs +++ b/cl-ast/src/ast_impl.rs @@ -13,6 +13,8 @@ mod display { pub open: &'t str, pub close: &'t str, } + /// Delimits with braces decorated with spaces `" {n"`, ..., `"\n}"` + pub const SPACED_BRACES: Delimiters = Delimiters { open: " {\n", close: "\n}" }; /// Delimits with braces on separate lines `{\n`, ..., `\n}` pub const BRACES: Delimiters = Delimiters { open: "{\n", close: "\n}" }; /// Delimits with parentheses on separate lines `{\n`, ..., `\n}` @@ -190,9 +192,7 @@ mod display { match self { StructKind::Empty => ';'.fmt(f), StructKind::Tuple(v) => delimit(separate(v, ", "), INLINE_PARENS)(f), - StructKind::Struct(v) => { - delimit(separate(v, ",\n"), Delimiters { open: " {\n", ..BRACES })(f) - } + StructKind::Struct(v) => delimit(separate(v, ",\n"), SPACED_BRACES)(f), } } } @@ -211,8 +211,8 @@ mod display { impl Display for EnumKind { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { - EnumKind::NoVariants => todo!(), - EnumKind::Variants(v) => separate(v, ", ")(f), + EnumKind::NoVariants => ';'.fmt(f), + EnumKind::Variants(v) => delimit(separate(v, ",\n"), SPACED_BRACES)(f), } } } @@ -226,9 +226,9 @@ mod display { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { VariantKind::Plain => Ok(()), - VariantKind::CLike(n) => n.fmt(f), + VariantKind::CLike(n) => write!(f, " = {n}"), VariantKind::Tuple(v) => delimit(separate(v, ", "), INLINE_PARENS)(f), - VariantKind::Struct(v) => delimit(separate(v, ",\n"), BRACES)(f), + VariantKind::Struct(v) => delimit(separate(v, ", "), INLINE_BRACES)(f), } } } @@ -563,7 +563,6 @@ mod display { } } - mod convert { //! Converts between major enums and enum variants use super::*;