From 2bd1ff3d597f800ae258d379f00799dbd4a5b7b6 Mon Sep 17 00:00:00 2001 From: _ <_@_> Date: Tue, 26 Sep 2023 15:57:23 -0500 Subject: [PATCH] :recycle: refactor impl Display for Value --- src/state.rs | 11 +---------- src/value.rs | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/src/state.rs b/src/state.rs index 878ec80..0758795 100644 --- a/src/state.rs +++ b/src/state.rs @@ -245,16 +245,7 @@ impl State { assert_eq! (*c, 1); let value = r.get (a + 1).unwrap (); - match value { - Value::Nil => println! ("nil"), - Value::Boolean (false) => println! ("false"), - Value::Boolean (true) => println! ("true"), - Value::Float (x) => println! ("{:?}", x), - Value::Integer (x) => println! ("{}", x), - Value::String (s) => println! ("{}", s), - Value::Table (t) => println! ("table: {:?}", std::rc::Rc::as_ptr (t)), - _ => unimplemented! (), - }; + println! ("{}", value); r [a] = r [a + 1].take (); }, diff --git a/src/value.rs b/src/value.rs index 18624b2..5c21c54 100644 --- a/src/value.rs +++ b/src/value.rs @@ -4,6 +4,7 @@ use std::{ PartialEq, }, collections::HashMap, + fmt, rc::Rc, }; @@ -41,6 +42,25 @@ impl Default for Value { } } +impl fmt::Display for Value { + fn fmt (&self, f: &mut fmt::Formatter) -> fmt::Result { + match self { + Value::Nil => write! (f, "nil"), + Value::Boolean (false) => write! (f, "false"), + Value::Boolean (true) => write! (f, "true"), + Value::Float (x) => write! (f, "{:?}", x), + Value::Integer (x) => write! (f, "{}", x), + Value::String (s) => write! (f, "{}", s), + Value::Table (t) => write! (f, "table: {:?}", std::rc::Rc::as_ptr (t)), + + Value::BogusArg (_) => write! (f, "BogusArg"), + Value::BogusClosure (_) => write! (f, "BogusClosure"), + Value::BogusEnv (_) => write! (f, "BogusEnv"), + Value::BogusPrint => write! (f, "BogusPrint"), + } + } +} + impl From for Value { fn from (x: String) -> Self { Self::String (x.into ())