Improve help output

This commit is contained in:
Jeff 2023-08-28 20:03:13 -04:00
parent 1f10bde1b4
commit 18e4fef62f
2 changed files with 34 additions and 10 deletions

View File

@ -162,7 +162,21 @@ impl Display for Table {
for row in &self.rows {
let row = row.iter().map(|value| {
let text = match value {
Value::List(list) => format!("{list:?}"),
Value::List(list) => {
let mut string = "(".to_string();
for (index, value) in list.into_iter().enumerate() {
if index > 0 {
string.push_str(", ");
}
string.push_str(&value.to_string());
}
string.push_str(")");
string
}
Value::Map(map) => format!("Map ({} items)", map.len()),
Value::Table(table) => format!("Table ({} items)", table.len()),
Value::Function(_) => "Function".to_string(),

View File

@ -1,9 +1,9 @@
use std::fmt::Display;
use std::fmt::{self, Debug, Display, Formatter};
use crate::Value;
/// The type of a `Value`.
#[derive(Clone, Debug)]
#[derive(Clone)]
pub enum ValueType {
Any,
String,
@ -53,7 +53,7 @@ impl PartialEq for ValueType {
}
impl Display for ValueType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match &self {
ValueType::Any => write!(f, "any"),
ValueType::String => write!(f, "string"),
@ -62,15 +62,19 @@ impl Display for ValueType {
ValueType::Boolean => write!(f, "boolean"),
ValueType::List => write!(f, "list"),
ValueType::ListOf(value_type) => {
write!(f, "list of {value_type}")
write!(f, "({value_type}s)")
}
ValueType::ListExact(list) => {
let items = list
.iter()
.map(|value_type| value_type.to_string() + " ")
.collect::<String>();
write!(f, "(")?;
for (index, item) in list.into_iter().enumerate() {
if index > 0 {
write!(f, ", ")?;
}
write!(f, "list of {items}")
write!(f, "{item}")?;
}
write!(f, ")")
}
ValueType::Empty => write!(f, "empty"),
ValueType::Map => write!(f, "map"),
@ -81,6 +85,12 @@ impl Display for ValueType {
}
}
impl Debug for ValueType {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "{self}")
}
}
impl From<&Value> for ValueType {
fn from(value: &Value) -> Self {
match value {