Skip to content

Commit

Permalink
util/pretty_print: Use Display instead of show
Browse files Browse the repository at this point in the history
  • Loading branch information
slotThe committed Apr 30, 2024
1 parent d5b4807 commit fb048ad
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
3 changes: 1 addition & 2 deletions src/eval/stdlib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion src/type/checker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
16 changes: 10 additions & 6 deletions src/util/pretty_print.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::fmt::Display;

use crate::util::style;

/// A bunch of [`Block`]s!
Expand All @@ -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([',', ';', '.']) {
Expand All @@ -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(),
Expand All @@ -45,7 +49,7 @@ impl Blocks {
let render = |line: Vec<Block>| -> String {
line
.iter()
.map(|b| b.show())
.map(|b| format!("{b}"))
.intersperse(" ".to_string())
.collect()
};
Expand Down

0 comments on commit fb048ad

Please sign in to comment.