diff options
Diffstat (limited to 'src/eval')
| -rw-r--r-- | src/eval/array.rs | 10 | ||||
| -rw-r--r-- | src/eval/dict.rs | 12 | ||||
| -rw-r--r-- | src/eval/function.rs | 30 | ||||
| -rw-r--r-- | src/eval/str.rs | 22 | ||||
| -rw-r--r-- | src/eval/template.rs | 8 | ||||
| -rw-r--r-- | src/eval/value.rs | 57 | ||||
| -rw-r--r-- | src/eval/walk.rs | 19 |
7 files changed, 70 insertions, 88 deletions
diff --git a/src/eval/array.rs b/src/eval/array.rs index bae89c4b..17192cb3 100644 --- a/src/eval/array.rs +++ b/src/eval/array.rs @@ -1,6 +1,6 @@ use std::cmp::Ordering; use std::convert::TryFrom; -use std::fmt::{self, Debug, Display, Formatter, Write}; +use std::fmt::{self, Debug, Formatter, Write}; use std::iter::FromIterator; use std::ops::{Add, AddAssign}; use std::rc::Rc; @@ -120,15 +120,9 @@ fn out_of_bounds(index: i64, len: i64) -> String { impl Debug for Array { fn fmt(&self, f: &mut Formatter) -> fmt::Result { - f.debug_list().entries(self.0.iter()).finish() - } -} - -impl Display for Array { - fn fmt(&self, f: &mut Formatter) -> fmt::Result { f.write_char('(')?; for (i, value) in self.iter().enumerate() { - Display::fmt(value, f)?; + value.fmt(f)?; if i + 1 < self.0.len() { f.write_str(", ")?; } diff --git a/src/eval/dict.rs b/src/eval/dict.rs index dfac04ed..c0ddf328 100644 --- a/src/eval/dict.rs +++ b/src/eval/dict.rs @@ -1,5 +1,5 @@ use std::collections::BTreeMap; -use std::fmt::{self, Debug, Display, Formatter, Write}; +use std::fmt::{self, Debug, Formatter, Write}; use std::iter::FromIterator; use std::ops::{Add, AddAssign}; use std::rc::Rc; @@ -79,17 +79,11 @@ impl Dict { /// The missing key access error message. #[cold] fn missing_key(key: &Str) -> String { - format!("dictionary does not contain key: {}", key) + format!("dictionary does not contain key: {:?}", key) } impl Debug for Dict { fn fmt(&self, f: &mut Formatter) -> fmt::Result { - f.debug_map().entries(self.0.iter()).finish() - } -} - -impl Display for Dict { - fn fmt(&self, f: &mut Formatter) -> fmt::Result { f.write_char('(')?; if self.is_empty() { f.write_char(':')?; @@ -97,7 +91,7 @@ impl Display for Dict { for (i, (key, value)) in self.iter().enumerate() { f.write_str(key)?; f.write_str(": ")?; - Display::fmt(value, f)?; + value.fmt(f)?; if i + 1 < self.0.len() { f.write_str(", ")?; } diff --git a/src/eval/function.rs b/src/eval/function.rs index 57364d11..c45ac7ba 100644 --- a/src/eval/function.rs +++ b/src/eval/function.rs @@ -1,4 +1,4 @@ -use std::fmt::{self, Debug, Display, Formatter, Write}; +use std::fmt::{self, Debug, Formatter, Write}; use std::rc::Rc; use super::{Cast, EvalContext, Str, Value}; @@ -40,12 +40,6 @@ impl Function { impl Debug for Function { fn fmt(&self, f: &mut Formatter) -> fmt::Result { - f.debug_struct("Function").field("name", &self.0.name).finish() - } -} - -impl Display for Function { - fn fmt(&self, f: &mut Formatter) -> fmt::Result { f.write_str("<function")?; if let Some(name) = self.name() { f.write_char(' ')?; @@ -63,7 +57,7 @@ impl PartialEq for Function { } /// Evaluated arguments to a function. -#[derive(Debug, Clone, PartialEq)] +#[derive(Clone, PartialEq)] pub struct Arguments { /// The span of the whole argument list. pub span: Span, @@ -72,7 +66,7 @@ pub struct Arguments { } /// An argument to a function call: `12` or `draw: false`. -#[derive(Debug, Clone, PartialEq)] +#[derive(Clone, PartialEq)] pub struct Argument { /// The span of the whole argument. pub span: Span, @@ -192,15 +186,11 @@ impl Arguments { } } -impl Display for Arguments { +impl Debug for Arguments { fn fmt(&self, f: &mut Formatter) -> fmt::Result { f.write_char('(')?; for (i, arg) in self.items.iter().enumerate() { - if let Some(name) = &arg.name { - f.write_str(name)?; - f.write_str(": ")?; - } - Display::fmt(&arg.value.v, f)?; + arg.fmt(f)?; if i + 1 < self.items.len() { f.write_str(", ")?; } @@ -209,6 +199,16 @@ impl Display for Arguments { } } +impl Debug for Argument { + fn fmt(&self, f: &mut Formatter) -> fmt::Result { + if let Some(name) = &self.name { + f.write_str(name)?; + f.write_str(": ")?; + } + Debug::fmt(&self.value.v, f) + } +} + dynamic! { Arguments: "arguments", } diff --git a/src/eval/str.rs b/src/eval/str.rs index 099a4363..59e0887a 100644 --- a/src/eval/str.rs +++ b/src/eval/str.rs @@ -1,6 +1,6 @@ use std::borrow::Borrow; use std::convert::TryFrom; -use std::fmt::{self, Debug, Display, Formatter, Write}; +use std::fmt::{self, Debug, Formatter, Write}; use std::ops::{Add, AddAssign, Deref}; use crate::diag::StrResult; @@ -10,9 +10,9 @@ use crate::util::EcoString; macro_rules! format_str { ($($tts:tt)*) => {{ use std::fmt::Write; - let mut s = $crate::util::EcoString::new(); + let mut s = $crate::eval::Str::new(); write!(s, $($tts)*).unwrap(); - $crate::eval::Str::from(s) + s }}; } @@ -67,12 +67,6 @@ impl Deref for Str { impl Debug for Str { fn fmt(&self, f: &mut Formatter) -> fmt::Result { - Debug::fmt(&self.0, f) - } -} - -impl Display for Str { - fn fmt(&self, f: &mut Formatter) -> fmt::Result { f.write_char('"')?; for c in self.chars() { match c { @@ -103,6 +97,16 @@ impl AddAssign for Str { } } +impl Write for Str { + fn write_str(&mut self, s: &str) -> fmt::Result { + self.0.write_str(s) + } + + fn write_char(&mut self, c: char) -> fmt::Result { + self.0.write_char(c) + } +} + impl AsRef<str> for Str { fn as_ref(&self) -> &str { self diff --git a/src/eval/template.rs b/src/eval/template.rs index f4feda40..42b35fc3 100644 --- a/src/eval/template.rs +++ b/src/eval/template.rs @@ -1,5 +1,5 @@ use std::convert::TryFrom; -use std::fmt::{self, Debug, Display, Formatter}; +use std::fmt::{self, Debug, Formatter}; use std::mem; use std::ops::{Add, AddAssign}; use std::rc::Rc; @@ -189,12 +189,6 @@ impl Template { impl Debug for Template { fn fmt(&self, f: &mut Formatter) -> fmt::Result { - f.pad("Template { .. }") - } -} - -impl Display for Template { - fn fmt(&self, f: &mut Formatter) -> fmt::Result { f.pad("<template>") } } diff --git a/src/eval/value.rs b/src/eval/value.rs index 77cb766c..a1d65a18 100644 --- a/src/eval/value.rs +++ b/src/eval/value.rs @@ -1,6 +1,6 @@ use std::any::Any; use std::cmp::Ordering; -use std::fmt::{self, Debug, Display, Formatter}; +use std::fmt::{self, Debug, Formatter}; use std::rc::Rc; use super::{ops, Array, Dict, Function, Str, Template}; @@ -11,7 +11,7 @@ use crate::syntax::Spanned; use crate::util::EcoString; /// A computational value. -#[derive(Debug, Clone)] +#[derive(Clone)] pub enum Value { /// The value that indicates the absence of a meaningful value. None, @@ -89,6 +89,11 @@ impl Value { T::cast(self) } + /// Return the debug representation of the value. + pub fn repr(&self) -> Str { + format_str!("{:?}", self) + } + /// Join the value with another value. pub fn join(self, rhs: Self) -> StrResult<Self> { ops::join(self, rhs) @@ -101,26 +106,26 @@ impl Default for Value { } } -impl Display for Value { +impl Debug for Value { fn fmt(&self, f: &mut Formatter) -> fmt::Result { match self { Self::None => f.pad("none"), Self::Auto => f.pad("auto"), - Self::Bool(v) => Display::fmt(v, f), - Self::Int(v) => Display::fmt(v, f), - Self::Float(v) => Display::fmt(v, f), - Self::Length(v) => Display::fmt(v, f), - Self::Angle(v) => Display::fmt(v, f), - Self::Relative(v) => Display::fmt(v, f), - Self::Linear(v) => Display::fmt(v, f), - Self::Fractional(v) => Display::fmt(v, f), - Self::Color(v) => Display::fmt(v, f), - Self::Str(v) => Display::fmt(v, f), - Self::Array(v) => Display::fmt(v, f), - Self::Dict(v) => Display::fmt(v, f), - Self::Template(v) => Display::fmt(v, f), - Self::Func(v) => Display::fmt(v, f), - Self::Dyn(v) => Display::fmt(v, f), + Self::Bool(v) => Debug::fmt(v, f), + Self::Int(v) => Debug::fmt(v, f), + Self::Float(v) => Debug::fmt(v, f), + Self::Length(v) => Debug::fmt(v, f), + Self::Angle(v) => Debug::fmt(v, f), + Self::Relative(v) => Debug::fmt(v, f), + Self::Linear(v) => Debug::fmt(v, f), + Self::Fractional(v) => Debug::fmt(v, f), + Self::Color(v) => Debug::fmt(v, f), + Self::Str(v) => Debug::fmt(v, f), + Self::Array(v) => Debug::fmt(v, f), + Self::Dict(v) => Debug::fmt(v, f), + Self::Template(v) => Debug::fmt(v, f), + Self::Func(v) => Debug::fmt(v, f), + Self::Dyn(v) => Debug::fmt(v, f), } } } @@ -186,7 +191,7 @@ impl Dynamic { /// Create a new instance from any value that satisifies the required bounds. pub fn new<T>(any: T) -> Self where - T: Type + Debug + Display + PartialEq + 'static, + T: Type + Debug + PartialEq + 'static, { Self(Rc::new(any)) } @@ -213,19 +218,13 @@ impl Debug for Dynamic { } } -impl Display for Dynamic { - fn fmt(&self, f: &mut Formatter) -> fmt::Result { - Display::fmt(&self.0, f) - } -} - impl PartialEq for Dynamic { fn eq(&self, other: &Self) -> bool { self.0.dyn_eq(other) } } -trait Bounds: Debug + Display + 'static { +trait Bounds: Debug + 'static { fn as_any(&self) -> &dyn Any; fn dyn_eq(&self, other: &Dynamic) -> bool; fn dyn_type_name(&self) -> &'static str; @@ -233,7 +232,7 @@ trait Bounds: Debug + Display + 'static { impl<T> Bounds for T where - T: Type + Debug + Display + PartialEq + 'static, + T: Type + Debug + PartialEq + 'static, { fn as_any(&self) -> &dyn Any { self @@ -420,11 +419,11 @@ mod tests { #[track_caller] fn test(value: impl Into<Value>, exp: &str) { - assert_eq!(value.into().to_string(), exp); + assert_eq!(format!("{:?}", value.into()), exp); } #[test] - fn test_value_to_string() { + fn test_value_debug() { // Primitives. test(Value::None, "none"); test(false, "false"); diff --git a/src/eval/walk.rs b/src/eval/walk.rs index db9fbbde..7ffb4c05 100644 --- a/src/eval/walk.rs +++ b/src/eval/walk.rs @@ -1,12 +1,10 @@ -use std::fmt::Write; use std::rc::Rc; -use super::{Eval, EvalContext, Template, Value}; +use super::{Eval, EvalContext, Str, Template, Value}; use crate::diag::TypResult; use crate::geom::Gen; use crate::layout::{ParChild, ParNode, StackChild, StackNode}; use crate::syntax::*; -use crate::util::EcoString; /// Walk a syntax node and fill the currently built template. pub trait Walk { @@ -38,13 +36,13 @@ impl Walk for SyntaxNode { Self::Enum(enum_) => enum_.walk(ctx)?, Self::Expr(expr) => match expr.eval(ctx)? { Value::None => {} - Value::Int(v) => ctx.template.text(v.to_string()), - Value::Float(v) => ctx.template.text(v.to_string()), + Value::Int(v) => ctx.template.text(format_str!("{}", v)), + Value::Float(v) => ctx.template.text(format_str!("{}", v)), Value::Str(v) => ctx.template.text(v), Value::Template(v) => ctx.template += v, // For values which can't be shown "naturally", we print the // representation in monospace. - other => ctx.template.monospace(other.to_string()), + other => ctx.template.monospace(other.repr()), }, } Ok(()) @@ -91,7 +89,7 @@ impl Walk for HeadingNode { impl Walk for ListNode { fn walk(&self, ctx: &mut EvalContext) -> TypResult<()> { let body = self.body.eval(ctx)?; - walk_item(ctx, '•'.into(), body); + walk_item(ctx, Str::from('•'), body); Ok(()) } } @@ -99,20 +97,19 @@ impl Walk for ListNode { impl Walk for EnumNode { fn walk(&self, ctx: &mut EvalContext) -> TypResult<()> { let body = self.body.eval(ctx)?; - let mut label = EcoString::new(); - write!(&mut label, "{}.", self.number.unwrap_or(1)).unwrap(); + let label = format_str!("{}.", self.number.unwrap_or(1)); walk_item(ctx, label, body); Ok(()) } } -fn walk_item(ctx: &mut EvalContext, label: EcoString, body: Template) { +fn walk_item(ctx: &mut EvalContext, label: Str, body: Template) { ctx.template += Template::from_block(move |state| { let label = ParNode { dir: state.dirs.inline, line_spacing: state.line_spacing(), children: vec![ParChild::Text( - label.clone(), + (&label).into(), state.aligns.inline, Rc::clone(&state.font), vec![], |
