From fb048ad550d2de0bbb34a62438584846da566ae5 Mon Sep 17 00:00:00 2001 From: Tony Zorman Date: Mon, 29 Apr 2024 18:49:04 +0200 Subject: [PATCH] util/pretty_print: Use Display instead of show --- src/eval/stdlib.rs | 3 +-- src/type/checker.rs | 2 +- src/util/pretty_print.rs | 16 ++++++++++------ 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/src/eval/stdlib.rs b/src/eval/stdlib.rs index 9de8593..70e0339 100644 --- a/src/eval/stdlib.rs +++ b/src/eval/stdlib.rs @@ -26,8 +26,7 @@ pub enum Builtin { } impl Builtin { - /// Pretty-print a 'Builtin' function. - pub fn show(&self) -> &'static str { + fn show(&self) -> &'static str { match self { Builtin::Id => "id", Builtin::BConst => "const", diff --git a/src/type/checker.rs b/src/type/checker.rs index 935ad5d..a2f9a46 100644 --- a/src/type/checker.rs +++ b/src/type/checker.rs @@ -255,7 +255,7 @@ impl Expr { // 1l⇒ Expr::Const(Const::Num(_)) => Ok(Type::Num), Expr::Const(_) | Expr::Obj(_) => Ok(Type::JSON), - Expr::Builtin(b) => expr::var(b.show()).synth(state), + Expr::Builtin(b) => expr::var(&format!("{b}")).synth(state), // List Expr::Arr(xs) => { if xs.is_empty() { diff --git a/src/util/pretty_print.rs b/src/util/pretty_print.rs index eecab03..5f617b0 100644 --- a/src/util/pretty_print.rs +++ b/src/util/pretty_print.rs @@ -1,3 +1,5 @@ +use std::fmt::Display; + use crate::util::style; /// A bunch of [`Block`]s! @@ -13,10 +15,9 @@ enum Block { Fancy(String), } -impl Block { - /// Pretty-print a single [`Block`]. - pub fn show(&self) -> String { - match self { +impl Display for Block { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let pp = match self { Block::Plain(s) => s.to_string(), Block::Fancy(s) => { if s.ends_with([',', ';', '.']) { @@ -26,9 +27,12 @@ impl Block { style(s) } }, - } + }; + write!(f, "{pp}",) } +} +impl Block { pub fn len(&self) -> usize { match self { Block::Plain(s) => s.len(), @@ -45,7 +49,7 @@ impl Blocks { let render = |line: Vec| -> String { line .iter() - .map(|b| b.show()) + .map(|b| format!("{b}")) .intersperse(" ".to_string()) .collect() };