Skip to content

Commit 89f8393

Browse files
committed
Add support for negative numbers in bevy_proto_bsn parsing
1 parent c055053 commit 89f8393

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

crates/bevy_proto_bsn/src/bsn_asset.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use thiserror::Error;
66

77
use bevy_proto_bsn_ast::{
88
quote::ToTokens,
9-
syn::{Expr, ExprCall, ExprLit, ExprStruct, Lit, Member},
9+
syn::{Expr, ExprCall, ExprLit, ExprStruct, ExprUnary, Lit, Member, UnOp},
1010
*,
1111
};
1212

@@ -262,6 +262,10 @@ impl From<&Expr> for BsnValue {
262262
Expr::Struct(strct) => strct.into(),
263263
Expr::Call(call) => call.into(),
264264
Expr::Paren(paren) => paren.expr.as_ref().into(),
265+
Expr::Unary(unary) => unary.try_into().unwrap_or_else(|err| {
266+
error!("{err:?}");
267+
BsnValue::UnknownExpr(expr.to_token_stream().to_string())
268+
}),
265269
expr => BsnValue::UnknownExpr(expr.to_token_stream().to_string()),
266270
}
267271
}
@@ -310,6 +314,27 @@ impl From<&ExprCall> for BsnValue {
310314
}
311315
}
312316

317+
impl TryFrom<&ExprUnary> for BsnValue {
318+
type Error = BsnLoaderError;
319+
320+
fn try_from(value: &ExprUnary) -> Result<Self, Self::Error> {
321+
if let UnOp::Neg(_) = value.op {
322+
if let Expr::Lit(lit) = value.expr.as_ref() {
323+
let mut bsn_value: BsnValue = lit.into();
324+
if let BsnValue::Number(ref mut f) = bsn_value {
325+
f.insert(0, '-');
326+
return Ok(bsn_value);
327+
}
328+
}
329+
}
330+
331+
Err(BsnLoaderError::SyntaxError(format!(
332+
"Could not parse unary expression: {}",
333+
value.to_token_stream()
334+
)))
335+
}
336+
}
337+
313338
trait ToTokensExt {
314339
fn to_compact_string(&self) -> String;
315340
}

0 commit comments

Comments
 (0)