1
0

Implement to json; Improve map display

This commit is contained in:
Jeff 2023-10-14 14:18:13 -04:00
parent ca4fd34bc1
commit 4e8d320c77
3 changed files with 25 additions and 6 deletions

View File

@ -174,6 +174,14 @@ impl Tool {
Value::String("read".to_string()), Value::String("read".to_string()),
Value::String("Get a file's content.".to_string()), Value::String("Get a file's content.".to_string()),
])?; ])?;
help_table.insert(vec![
Value::String("from_json".to_string()),
Value::String("Convert a JSON string to a value.".to_string()),
])?;
help_table.insert(vec![
Value::String("to_json".to_string()),
Value::String("Convert a value to a JSON string.".to_string()),
])?;
Value::Table(help_table) Value::Table(help_table)
} }
@ -191,7 +199,14 @@ impl Tool {
serde_json::from_str(json_string)? serde_json::from_str(json_string)?
} }
Tool::ToJson => todo!(), Tool::ToJson => {
Error::expect_tool_argument_amount("to_json", 1, values.len())?;
let value = &values[0];
let json_string = serde_json::to_string(value)?;
Value::String(json_string)
}
}; };
Ok(value) Ok(value)

View File

@ -413,10 +413,10 @@ impl Display for Value {
fn fmt(&self, f: &mut Formatter) -> fmt::Result { fn fmt(&self, f: &mut Formatter) -> fmt::Result {
match self { match self {
Value::String(string) => write!(f, "{string}"), Value::String(string) => write!(f, "{string}"),
Value::Float(float) => write!(f, "{}", float), Value::Float(float) => write!(f, "{float}"),
Value::Integer(int) => write!(f, "{}", int), Value::Integer(int) => write!(f, "{int}"),
Value::Boolean(boolean) => write!(f, "{}", boolean), Value::Boolean(boolean) => write!(f, "{boolean}"),
Value::Empty => write!(f, "()"), Value::Empty => write!(f, "empty"),
Value::List(list) => { Value::List(list) => {
write!(f, "[")?; write!(f, "[")?;
for value in list { for value in list {

View File

@ -139,7 +139,11 @@ impl Default for VariableMap {
impl Display for VariableMap { impl Display for VariableMap {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
Table::from(self).fmt(f) write!(f, "{{\n")?;
for (key, value) in &self.variables {
write!(f, " {key} = {value}\n")?;
}
write!(f, "}}")
} }
} }