Skip to content

Commit 9b375fa

Browse files
authored
Add support for lists in bevy_proto_bsn parsing (#204)
1 parent c055053 commit 9b375fa

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-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(Into::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: 20 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;
@@ -447,6 +447,7 @@ impl<'a, 'b> BsnReflector<'a, 'b> {
447447
ty,
448448
),
449449
BsnValue::Tuple(items) => self.reflect_tuple(items, ty),
450+
BsnValue::List(items) => self.reflect_list(items, ty),
450451
_ => Err(ReflectError::UnexpectedType(
451452
format!("{:?}", value),
452453
ty.type_path().into(),
@@ -517,6 +518,22 @@ impl<'a, 'b> BsnReflector<'a, 'b> {
517518
Ok(ReflectedValue::new(ty.type_id(), Box::new(dynamic_tuple)))
518519
}
519520

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

0 commit comments

Comments
 (0)