Skip to content

KCL: Remove more write! macros from recaster #7877

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions rust/kcl-lib/src/parsing/ast/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
20 changes: 20 additions & 0 deletions rust/kcl-lib/src/parsing/token/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
28 changes: 13 additions & 15 deletions rust/kcl-lib/src/unparser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -506,11 +506,6 @@ impl TypeDeclaration {
}
}

fn write<W: std::fmt::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<W: std::fmt::Write>(f: &mut W, s: impl std::fmt::Debug) {
f.write_fmt(format_args!("{s:?}"))
.expect("writing to a string should always succeed")
Expand All @@ -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");
}
}
}
Expand Down Expand Up @@ -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(')');
Expand Down
Loading