Skip to content

Commit ee4a363

Browse files
committed
Add support for negative numbers in bevy_proto_bsn parsing
1 parent 6942532 commit ee4a363

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

@@ -265,6 +265,10 @@ impl From<&Expr> for BsnValue {
265265
Expr::Call(call) => call.into(),
266266
Expr::Paren(paren) => paren.expr.as_ref().into(),
267267
Expr::Array(array) => BsnValue::List(array.elems.iter().map(Into::into).collect()),
268+
Expr::Unary(unary) => unary.try_into().unwrap_or_else(|err| {
269+
error!("{err:?}");
270+
BsnValue::UnknownExpr(expr.to_token_stream().to_string())
271+
}),
268272
expr => BsnValue::UnknownExpr(expr.to_token_stream().to_string()),
269273
}
270274
}
@@ -313,6 +317,27 @@ impl From<&ExprCall> for BsnValue {
313317
}
314318
}
315319

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

0 commit comments

Comments
 (0)