Skip to content

Commit b89f763

Browse files
committed
Add support for lists in bevy_proto_bsn parsing
1 parent c055053 commit b89f763

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

crates/bevy_proto_bsn/src/bsn_asset.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,8 @@ pub enum BsnValue {
157157
Call(String, Vec<BsnValue>),
158158
/// A tuple of values.
159159
Tuple(Vec<BsnValue>),
160+
/// A list of values.
161+
List(Vec<BsnValue>),
160162
/// An unknown expression.
161163
UnknownExpr(String),
162164
}
@@ -262,6 +264,7 @@ impl From<&Expr> for BsnValue {
262264
Expr::Struct(strct) => strct.into(),
263265
Expr::Call(call) => call.into(),
264266
Expr::Paren(paren) => paren.expr.as_ref().into(),
267+
Expr::Array(array) => BsnValue::List(array.elems.iter().map(|e| e.into()).collect()),
265268
expr => BsnValue::UnknownExpr(expr.to_token_stream().to_string()),
266269
}
267270
}
@@ -419,6 +422,7 @@ impl ToBsnString for BsnValue {
419422
format!("{}({})", path, args.joined(", "))
420423
}
421424
BsnValue::Tuple(fields) => format!("({})", fields.joined(", ")),
425+
BsnValue::List(values) => format!("[{}]", values.joined(", ")),
422426
BsnValue::UnknownExpr(expr) => expr.clone(),
423427
}
424428
}

crates/bevy_proto_bsn/src/bsn_reflect.rs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ use bevy::{
99
},
1010
platform::hash::FixedState,
1111
reflect::{
12-
DynamicEnum, DynamicStruct, DynamicTuple, DynamicTupleStruct, DynamicVariant, FromType,
13-
NamedField, PartialReflect, Reflect, ReflectKind, StructInfo, StructVariantInfo, TypeInfo,
14-
TypePath, TypeRegistration, TypeRegistry, TypeRegistryArc,
12+
DynamicEnum, DynamicList, DynamicStruct, DynamicTuple, DynamicTupleStruct, DynamicVariant,
13+
FromType, NamedField, PartialReflect, Reflect, ReflectKind, StructInfo, StructVariantInfo,
14+
TypeInfo, TypePath, TypeRegistration, TypeRegistry, TypeRegistryArc,
1515
},
1616
};
1717
use thiserror::Error;
@@ -451,6 +451,11 @@ impl<'a, 'b> BsnReflector<'a, 'b> {
451451
format!("{:?}", value),
452452
ty.type_path().into(),
453453
)),
454+
BsnValue::List(items) => self.reflect_list(items, ty),
455+
_ => Err(ReflectError::UnexpectedType(
456+
format!("{:?}", value),
457+
ty.type_path().into(),
458+
)),
454459
}
455460
}
456461

@@ -517,6 +522,22 @@ impl<'a, 'b> BsnReflector<'a, 'b> {
517522
Ok(ReflectedValue::new(ty.type_id(), Box::new(dynamic_tuple)))
518523
}
519524

525+
fn reflect_list(&self, items: &[BsnValue], ty: &TypeInfo) -> ReflectResult<ReflectedValue> {
526+
if let Ok(list_info) = ty.as_list() {
527+
let mut dynamic_list = DynamicList::default();
528+
let item_type_info = list_info.item_info().expect("Expected typed list");
529+
for item in items.iter() {
530+
dynamic_list.push_box(self.reflect_value(item, item_type_info)?.instance);
531+
}
532+
Ok(ReflectedValue::new(ty.type_id(), Box::new(dynamic_list)))
533+
} else {
534+
return Err(ReflectError::UnexpectedType(
535+
format!("{:?}", items),
536+
format!("{:?}", ty),
537+
));
538+
}
539+
}
540+
520541
fn reflect_path(&self, path: &str, ty: Option<&TypeInfo>) -> ReflectResult<ReflectedValue> {
521542
let ty = match ty {
522543
Some(ty) => ty,

0 commit comments

Comments
 (0)