Skip to content

Commit

Permalink
Implement Display instead
Browse files Browse the repository at this point in the history
  • Loading branch information
WilliamRagstad committed Aug 16, 2024
1 parent 6668863 commit edd2fd3
Showing 1 changed file with 15 additions and 17 deletions.
32 changes: 15 additions & 17 deletions src/file/webx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,35 +260,33 @@ pub enum WXLiteralValue {
Object(Vec<(String, WXLiteralValue)>),
}

impl ToString for WXLiteralValue {
fn to_string(&self) -> String {
impl Display for WXLiteralValue {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match self {
WXLiteralValue::Identifier(name) => name.clone(),
WXLiteralValue::String(string) => format!("\"{}\"", string),
WXLiteralValue::Number(integer, decimal) => format!("{}.{}", integer, decimal),
WXLiteralValue::Boolean(boolean) => boolean.to_string(),
WXLiteralValue::Null => "null".to_string(),
WXLiteralValue::Identifier(name) => write!(f, "{}", name),
WXLiteralValue::String(string) => write!(f, "\"{}\"", string),
WXLiteralValue::Number(integer, decimal) => write!(f, "{}.{}", integer, decimal),
WXLiteralValue::Boolean(boolean) => write!(f, "{}", boolean),
WXLiteralValue::Null => write!(f, "null"),
WXLiteralValue::Array(array) => {
let mut s = "[".to_string();
write!(f, "[")?;
for (i, value) in array.iter().enumerate() {
if i > 0 {
s.push_str(", ");
write!(f, ", ")?;
}
s.push_str(&value.to_string());
write!(f, "{}", value)?;
}
s.push(']');
s
write!(f, "]")
}
WXLiteralValue::Object(object) => {
let mut s = "{".to_string();
write!(f, "{{")?;
for (i, (key, value)) in object.iter().enumerate() {
if i > 0 {
s.push_str(", ");
write!(f, ", ")?;
}
s.push_str(&format!("{}: {}", key, value.to_string()));
write!(f, "{}: {}", key, value)?;
}
s.push('}');
s
write!(f, "}}")
}
}
}
Expand Down

0 comments on commit edd2fd3

Please sign in to comment.