diff options
Diffstat (limited to 'crates/rebel-lang/src/value.rs')
-rw-r--r-- | crates/rebel-lang/src/value.rs | 216 |
1 files changed, 108 insertions, 108 deletions
diff --git a/crates/rebel-lang/src/value.rs b/crates/rebel-lang/src/value.rs index c07f62c..735ae63 100644 --- a/crates/rebel-lang/src/value.rs +++ b/crates/rebel-lang/src/value.rs @@ -63,114 +63,6 @@ impl Value { } } -impl Display for Value { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - match self { - Value::Unit => f.write_str("()"), - Value::Boolean(value) => value.fmt(f), - Value::Integer(value) => value.fmt(f), - Value::Str(value) => write!(f, "{value:?}"), - Value::Tuple(elems) => { - let mut first = true; - f.write_str("(")?; - if elems.is_empty() { - f.write_str(",")?; - } - for elem in elems { - if !first { - f.write_str(", ")?; - } - first = false; - elem.fmt(f)?; - } - f.write_str(")") - } - Value::Array(elems) => { - let mut first = true; - f.write_str("[")?; - for elem in elems { - if !first { - f.write_str(", ")?; - } - first = false; - elem.fmt(f)?; - } - f.write_str("]") - } - Value::Map(entries) => { - let mut first = true; - f.write_str("{")?; - for (key, value) in entries { - if !first { - f.write_str(", ")?; - } - first = false; - write!(f, "{key} = {value}")?; - } - f.write_str("}") - } - Value::Fn(func) => func.typ.fmt(f), - } - } -} - -#[derive(Debug)] -struct Stringify<'a>(&'a Value); - -impl<'a> Display for Stringify<'a> { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - match self.0 { - Value::Boolean(value) => value.fmt(f), - Value::Integer(value) => value.fmt(f), - Value::Str(value) => value.fmt(f), - _ => Err(std::fmt::Error), - } - } -} - -#[derive(Debug)] -struct ScriptStringify<'a>(&'a Value); - -impl<'a> ScriptStringify<'a> { - fn fmt_list(elems: &'a [Value], f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - let mut first = true; - for elem in elems { - if !first { - f.write_char(' ')?; - } - ScriptStringify(elem).fmt(f)?; - first = false; - } - Ok(()) - } -} - -impl<'a> Display for ScriptStringify<'a> { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - match self.0 { - Value::Boolean(value) => { - value.fmt(f)?; - } - Value::Integer(value) => { - value.fmt(f)?; - } - Value::Str(value) => { - f.write_char('\'')?; - f.write_str(&value.replace('\'', "'\\''"))?; - f.write_char('\'')?; - } - Value::Array(elems) => { - Self::fmt_list(elems, f)?; - } - Value::Tuple(elems) => { - Self::fmt_list(elems, f)?; - } - _ => return Err(std::fmt::Error), - }; - Ok(()) - } -} - #[derive(Debug, Default)] pub struct Context { pub values: Module<Value>, @@ -394,6 +286,114 @@ impl Context { } } +impl Display for Value { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Value::Unit => f.write_str("()"), + Value::Boolean(value) => value.fmt(f), + Value::Integer(value) => value.fmt(f), + Value::Str(value) => write!(f, "{value:?}"), + Value::Tuple(elems) => { + let mut first = true; + f.write_str("(")?; + if elems.is_empty() { + f.write_str(",")?; + } + for elem in elems { + if !first { + f.write_str(", ")?; + } + first = false; + elem.fmt(f)?; + } + f.write_str(")") + } + Value::Array(elems) => { + let mut first = true; + f.write_str("[")?; + for elem in elems { + if !first { + f.write_str(", ")?; + } + first = false; + elem.fmt(f)?; + } + f.write_str("]") + } + Value::Map(entries) => { + let mut first = true; + f.write_str("{")?; + for (key, value) in entries { + if !first { + f.write_str(", ")?; + } + first = false; + write!(f, "{key} = {value}")?; + } + f.write_str("}") + } + Value::Fn(func) => func.typ.fmt(f), + } + } +} + +#[derive(Debug)] +struct Stringify<'a>(&'a Value); + +impl<'a> Display for Stringify<'a> { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self.0 { + Value::Boolean(value) => value.fmt(f), + Value::Integer(value) => value.fmt(f), + Value::Str(value) => value.fmt(f), + _ => Err(std::fmt::Error), + } + } +} + +#[derive(Debug)] +struct ScriptStringify<'a>(&'a Value); + +impl<'a> ScriptStringify<'a> { + fn fmt_list(elems: &'a [Value], f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let mut first = true; + for elem in elems { + if !first { + f.write_char(' ')?; + } + ScriptStringify(elem).fmt(f)?; + first = false; + } + Ok(()) + } +} + +impl<'a> Display for ScriptStringify<'a> { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self.0 { + Value::Boolean(value) => { + value.fmt(f)?; + } + Value::Integer(value) => { + value.fmt(f)?; + } + Value::Str(value) => { + f.write_char('\'')?; + f.write_str(&value.replace('\'', "'\\''"))?; + f.write_char('\'')?; + } + Value::Array(elems) => { + Self::fmt_list(elems, f)?; + } + Value::Tuple(elems) => { + Self::fmt_list(elems, f)?; + } + _ => return Err(std::fmt::Error), + }; + Ok(()) + } +} + #[derive(Debug)] struct StrDisplay<'a> { pieces: &'a [ast::StrPiece<'a>], |