diff options
| author | Laurenz <laurmaedje@gmail.com> | 2023-03-10 20:47:23 +0100 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2023-03-10 21:19:50 +0100 |
| commit | a9fdff244aef859449a76e5f762ee7c343a8ddcc (patch) | |
| tree | 172b543183296b4bc30b3008650f594688467914 /src/eval/dict.rs | |
| parent | 62f35602a87574dcc607f1637aeae1be574981ff (diff) | |
Expose content representation more
Diffstat (limited to 'src/eval/dict.rs')
| -rw-r--r-- | src/eval/dict.rs | 45 |
1 files changed, 22 insertions, 23 deletions
diff --git a/src/eval/dict.rs b/src/eval/dict.rs index 6c1934c9..ececce07 100644 --- a/src/eval/dict.rs +++ b/src/eval/dict.rs @@ -1,5 +1,5 @@ use std::collections::BTreeMap; -use std::fmt::{self, Debug, Formatter, Write}; +use std::fmt::{self, Debug, Formatter}; use std::ops::{Add, AddAssign}; use std::sync::Arc; @@ -8,7 +8,7 @@ use ecow::{eco_format, EcoString}; use super::{array, Array, Str, Value}; use crate::diag::StrResult; use crate::syntax::is_ident; -use crate::util::ArcExt; +use crate::util::{pretty_array, ArcExt}; /// Create a new [`Dict`] from key-value pairs. #[macro_export] @@ -132,31 +132,24 @@ impl Dict { } } -/// The missing key access error message. -#[cold] -fn missing_key(key: &str) -> EcoString { - eco_format!("dictionary does not contain key {:?}", Str::from(key)) -} - impl Debug for Dict { fn fmt(&self, f: &mut Formatter) -> fmt::Result { - f.write_char('(')?; if self.is_empty() { - f.write_char(':')?; + return f.write_str("(:)"); } - for (i, (key, value)) in self.iter().enumerate() { - if is_ident(key) { - f.write_str(key)?; - } else { - write!(f, "{key:?}")?; - } - f.write_str(": ")?; - value.fmt(f)?; - if i + 1 < self.0.len() { - f.write_str(", ")?; - } - } - f.write_char(')') + + let pieces: Vec<_> = self + .iter() + .map(|(key, value)| { + if is_ident(key) { + eco_format!("{key}: {value:?}") + } else { + eco_format!("{key:?}: {value:?}") + } + }) + .collect(); + + f.write_str(&pretty_array(&pieces, false)) } } @@ -207,3 +200,9 @@ impl<'a> IntoIterator for &'a Dict { self.iter() } } + +/// The missing key access error message. +#[cold] +fn missing_key(key: &str) -> EcoString { + eco_format!("dictionary does not contain key {:?}", Str::from(key)) +} |
