diff --git a/rust/kcl-lib/src/parsing/ast/types/mod.rs b/rust/kcl-lib/src/parsing/ast/types/mod.rs index 4e4965e16e1..7f33b205bab 100644 --- a/rust/kcl-lib/src/parsing/ast/types/mod.rs +++ b/rust/kcl-lib/src/parsing/ast/types/mod.rs @@ -3042,6 +3042,13 @@ impl UnaryOperator { UnaryOperator::Not => *b"not", } } + + pub fn write_to(&self, buf: &mut String) { + match self { + UnaryOperator::Neg => buf.push('-'), + UnaryOperator::Not => buf.push('!'), + } + } } #[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)] diff --git a/rust/kcl-lib/src/parsing/token/mod.rs b/rust/kcl-lib/src/parsing/token/mod.rs index 4be1e92e9c7..9944802e06a 100644 --- a/rust/kcl-lib/src/parsing/token/mod.rs +++ b/rust/kcl-lib/src/parsing/token/mod.rs @@ -98,6 +98,26 @@ impl FromStr for NumericSuffix { } } +impl NumericSuffix { + pub fn write_to(&self, buf: &mut String) { + match self { + NumericSuffix::None => {} + NumericSuffix::Count => buf.push('_'), + NumericSuffix::Unknown => buf.push_str("_?"), + NumericSuffix::Length => buf.push_str("Length"), + NumericSuffix::Angle => buf.push_str("Angle"), + NumericSuffix::Mm => buf.push_str("mm"), + NumericSuffix::Cm => buf.push_str("cm"), + NumericSuffix::M => buf.push('m'), + NumericSuffix::Inch => buf.push_str("in"), + NumericSuffix::Ft => buf.push_str("ft"), + NumericSuffix::Yd => buf.push_str("yd"), + NumericSuffix::Deg => buf.push_str("deg"), + NumericSuffix::Rad => buf.push_str("rad"), + } + } +} + impl fmt::Display for NumericSuffix { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { diff --git a/rust/kcl-lib/src/unparser.rs b/rust/kcl-lib/src/unparser.rs index c1e87ebc2a2..782edc7d83c 100644 --- a/rust/kcl-lib/src/unparser.rs +++ b/rust/kcl-lib/src/unparser.rs @@ -506,11 +506,6 @@ impl TypeDeclaration { } } -fn write(f: &mut W, s: impl std::fmt::Display) { - f.write_fmt(format_args!("{s}")) - .expect("writing to a string should always succeed") -} - fn write_dbg(f: &mut W, s: impl std::fmt::Debug) { f.write_fmt(format_args!("{s:?}")) .expect("writing to a string should always succeed") @@ -522,22 +517,25 @@ impl Literal { LiteralValue::Number { value, suffix } => { if self.raw.contains('.') && value.fract() == 0.0 { write_dbg(buf, value); - write(buf, suffix); + suffix.write_to(buf); } else { - write(buf, &self.raw); + buf.push_str(&self.raw); } } LiteralValue::String(ref s) => { if let Some(suggestion) = deprecation(s, DeprecationKind::String) { - return write!(buf, "{suggestion}").unwrap(); + return buf.push_str(suggestion); } let quote = if self.raw.trim().starts_with('"') { '"' } else { '\'' }; - write(buf, quote); - write(buf, s); - write(buf, quote); + buf.push(quote); + buf.push_str(s); + buf.push(quote); + } + LiteralValue::Bool(true) => { + buf.push_str("true"); } - LiteralValue::Bool(_) => { - write(buf, &self.raw); + LiteralValue::Bool(false) => { + buf.push_str("false"); } } } @@ -814,11 +812,11 @@ impl UnaryExpression { | BinaryPart::IfExpression(_) | BinaryPart::AscribedExpression(_) | BinaryPart::CallExpressionKw(_) => { - write!(buf, "{}", self.operator).no_fail(); + self.operator.write_to(buf); self.argument.recast(buf, options, 0, ctxt) } BinaryPart::BinaryExpression(_) | BinaryPart::UnaryExpression(_) => { - write!(buf, "{}", self.operator).no_fail(); + self.operator.write_to(buf); buf.push('('); self.argument.recast(buf, options, 0, ctxt); buf.push(')');