From 10dabadd084672875ae11ada5b4f2245cefb1298 Mon Sep 17 00:00:00 2001 From: Piotr Siuszko Date: Sun, 8 Jun 2025 10:37:27 +0200 Subject: [PATCH] Apply feedback --- crates/bevy_remote/Cargo.toml | 5 ++- crates/bevy_remote/src/builtin_methods.rs | 14 +++--- crates/bevy_remote/src/lib.rs | 2 + crates/bevy_remote/src/schemas/json_schema.rs | 27 +++++------- crates/bevy_remote/src/schemas/mod.rs | 44 +++++++++---------- 5 files changed, 44 insertions(+), 48 deletions(-) diff --git a/crates/bevy_remote/Cargo.toml b/crates/bevy_remote/Cargo.toml index 8f88f5b41e..08019219f3 100644 --- a/crates/bevy_remote/Cargo.toml +++ b/crates/bevy_remote/Cargo.toml @@ -9,8 +9,9 @@ license = "MIT OR Apache-2.0" keywords = ["bevy"] [features] -default = ["http"] +default = ["http","bevy_asset"] http = ["dep:async-io", "dep:smol-hyper"] +bevy_asset = ["dep:bevy_asset"] [dependencies] # bevy @@ -26,7 +27,7 @@ bevy_platform = { path = "../bevy_platform", version = "0.16.0-dev", default-fea "std", "serialize", ] } -bevy_asset = { path = "../bevy_asset", version = "0.16.0-dev" } +bevy_asset = { path = "../bevy_asset", version = "0.16.0-dev", optional = true} # other anyhow = "1" diff --git a/crates/bevy_remote/src/builtin_methods.rs b/crates/bevy_remote/src/builtin_methods.rs index 5f6be9904e..c599894b89 100644 --- a/crates/bevy_remote/src/builtin_methods.rs +++ b/crates/bevy_remote/src/builtin_methods.rs @@ -1231,25 +1231,21 @@ pub fn export_registry_types(In(params): In>, world: &World) -> Br let types = types.read(); let schemas = types .iter() - .filter(|type_reg| { + .filter_map(|type_reg| { let path_table = type_reg.type_info().type_path_table(); if let Some(crate_name) = &path_table.crate_name() { if !filter.with_crates.is_empty() && !filter.with_crates.iter().any(|c| crate_name.eq(c)) { - return false; + return None; } if !filter.without_crates.is_empty() && filter.without_crates.iter().any(|c| crate_name.eq(c)) { - return false; + return None; } } - - true - }) - .flat_map(|e| { - let (id, schema) = export_type(e, extra_info); + let (id, schema) = export_type(type_reg, extra_info); if !filter.type_limit.with.is_empty() && !filter @@ -1269,7 +1265,7 @@ pub fn export_registry_types(In(params): In>, world: &World) -> Br { return None; } - Some((id, schema)) + Some((id.to_string(), schema)) }) .collect::>(); diff --git a/crates/bevy_remote/src/lib.rs b/crates/bevy_remote/src/lib.rs index cce512d34b..348be8089d 100644 --- a/crates/bevy_remote/src/lib.rs +++ b/crates/bevy_remote/src/lib.rs @@ -364,6 +364,8 @@ //! [fully-qualified type names]: bevy_reflect::TypePath::type_path //! [fully-qualified type name]: bevy_reflect::TypePath::type_path +extern crate alloc; + use async_channel::{Receiver, Sender}; use bevy_app::{prelude::*, MainScheduleOrder}; use bevy_derive::{Deref, DerefMut}; diff --git a/crates/bevy_remote/src/schemas/json_schema.rs b/crates/bevy_remote/src/schemas/json_schema.rs index e3ab2787e5..4e56625bc8 100644 --- a/crates/bevy_remote/src/schemas/json_schema.rs +++ b/crates/bevy_remote/src/schemas/json_schema.rs @@ -1,5 +1,6 @@ //! Module with JSON Schema type for Bevy Registry Types. //! It tries to follow this standard: +use alloc::borrow::Cow; use bevy_platform::collections::HashMap; use bevy_reflect::{ GetTypeRegistration, NamedField, OpaqueInfo, TypeInfo, TypeRegistration, TypeRegistry, @@ -14,11 +15,11 @@ use crate::schemas::SchemaTypesMetadata; /// Helper trait for converting `TypeRegistration` to `JsonSchemaBevyType` pub trait TypeRegistrySchemaReader { /// Export type JSON Schema. - fn export_type_json_schema( + fn export_type_json_schema( &self, extra_info: &SchemaTypesMetadata, ) -> Option { - self.export_type_json_schema_for_id(extra_info, T::get_type_registration().type_id()) + self.export_type_json_schema_for_id(extra_info, TypeId::of::()) } /// Export type JSON Schema. fn export_type_json_schema_for_id( @@ -43,11 +44,8 @@ impl TypeRegistrySchemaReader for TypeRegistry { pub fn export_type( reg: &TypeRegistration, metadata: &SchemaTypesMetadata, -) -> (String, JsonSchemaBevyType) { - ( - reg.type_info().type_path().to_owned(), - (reg, metadata).into(), - ) +) -> (Cow<'static, str>, JsonSchemaBevyType) { + (reg.type_info().type_path().into(), (reg, metadata).into()) } impl From<(&TypeRegistration, &SchemaTypesMetadata)> for JsonSchemaBevyType { @@ -517,7 +515,7 @@ mod tests { register.register_type_data::(); } let mut metadata = SchemaTypesMetadata::default(); - metadata.register_type::("CustomData"); + metadata.map_type_data::("CustomData"); let type_registry = atr.read(); let foo_registration = type_registry .get(TypeId::of::()) @@ -525,19 +523,19 @@ mod tests { .clone(); let (_, schema) = export_type(&foo_registration, &metadata); assert!( - !metadata.has_data_type::(&schema.reflect_types), + !metadata.has_type_data::(&schema.reflect_types), "Should not be a component" ); assert!( - !metadata.has_data_type::(&schema.reflect_types), + !metadata.has_type_data::(&schema.reflect_types), "Should not be a resource" ); assert!( - metadata.has_data_type::(&schema.reflect_types), + metadata.has_type_data::(&schema.reflect_types), "Should have default" ); assert!( - metadata.has_data_type::(&schema.reflect_types), + metadata.has_type_data::(&schema.reflect_types), "Should have CustomData" ); assert!(schema.properties.is_empty(), "Should not have any field"); @@ -619,10 +617,9 @@ mod tests { assert_normalized_values(schema_as_value, value); } - fn assert_normalized_values(one: Value, two: Value) { - let mut one = one.clone(); + /// This function exist to avoid false failures due to ordering differences between `serde_json` values. + fn assert_normalized_values(mut one: Value, mut two: Value) { normalize_json(&mut one); - let mut two = two.clone(); normalize_json(&mut two); assert_eq!(one, two); diff --git a/crates/bevy_remote/src/schemas/mod.rs b/crates/bevy_remote/src/schemas/mod.rs index af7a82eb03..10cb2e9421 100644 --- a/crates/bevy_remote/src/schemas/mod.rs +++ b/crates/bevy_remote/src/schemas/mod.rs @@ -1,6 +1,4 @@ //! Module with schemas used for various BRP endpoints - -use bevy_asset::{ReflectAsset, ReflectHandle}; use bevy_ecs::{ reflect::{ReflectComponent, ReflectResource}, resource::Resource, @@ -20,48 +18,50 @@ pub mod open_rpc; #[derive(Debug, Resource, Reflect)] #[reflect(Resource)] pub struct SchemaTypesMetadata { - /// data types id mapping to strings. - pub data_types: HashMap, + /// Type Data id mapping to strings. + pub type_data_map: HashMap, } impl Default for SchemaTypesMetadata { fn default() -> Self { let mut data_types = Self { - data_types: Default::default(), + type_data_map: Default::default(), }; - data_types.register_type::("Component"); - data_types.register_type::("Resource"); - data_types.register_type::("Default"); - data_types.register_type::("Asset"); - data_types.register_type::("AssetHandle"); - data_types.register_type::("Serialize"); - data_types.register_type::("Deserialize"); + data_types.map_type_data::("Component"); + data_types.map_type_data::("Resource"); + data_types.map_type_data::("Default"); + #[cfg(feature = "bevy_asset")] + data_types.map_type_data::("Asset"); + #[cfg(feature = "bevy_asset")] + data_types.map_type_data::("AssetHandle"); + data_types.map_type_data::("Serialize"); + data_types.map_type_data::("Deserialize"); data_types } } impl SchemaTypesMetadata { /// Map `TypeId` of `TypeData` to string - pub fn register_type(&mut self, name: impl Into) { - self.data_types.insert(TypeId::of::(), name.into()); + pub fn map_type_data(&mut self, name: impl Into) { + self.type_data_map.insert(TypeId::of::(), name.into()); } - /// build reflect types list for a given type registration + /// Build reflect types list for a given type registration pub fn get_registered_reflect_types(&self, reg: &TypeRegistration) -> Vec { - self.data_types + self.type_data_map .iter() - .flat_map(|(id, name)| reg.data_by_id(*id).and(Some(name.clone()))) + .filter_map(|(id, name)| reg.data_by_id(*id).and(Some(name.clone()))) .collect() } - /// checks if slice contains string value that matches checked `TypeData` - pub fn has_data_type(&self, types_string_slice: &[String]) -> bool { - self.has_data_type_by_id(TypeId::of::(), types_string_slice) + /// Checks if slice contains string value that matches checked `TypeData` + pub fn has_type_data(&self, types_string_slice: &[String]) -> bool { + self.has_type_data_by_id(TypeId::of::(), types_string_slice) } /// Checks if slice contains string value that matches checked `TypeData` by id. - pub fn has_data_type_by_id(&self, id: TypeId, types_string_slice: &[String]) -> bool { - self.data_types + pub fn has_type_data_by_id(&self, id: TypeId, types_string_slice: &[String]) -> bool { + self.type_data_map .get(&id) .is_some_and(|data_s| types_string_slice.iter().any(|e| e.eq(data_s))) }