//! Module with schemas used for various BRP endpoints use bevy_ecs::{ reflect::{ReflectComponent, ReflectResource}, resource::Resource, }; use bevy_platform::collections::HashMap; use bevy_reflect::{ prelude::ReflectDefault, Reflect, ReflectDeserialize, ReflectSerialize, TypeData, TypeRegistration, }; use core::any::TypeId; pub mod json_schema; pub mod open_rpc; /// Holds mapping of reflect [type data](TypeData) to strings, /// later on used in Bevy Json Schema. #[derive(Debug, Resource, Reflect)] #[reflect(Resource)] pub struct SchemaTypesMetadata { /// Type Data id mapping to strings. pub type_data_map: HashMap, } impl Default for SchemaTypesMetadata { fn default() -> Self { let mut data_types = Self { type_data_map: Default::default(), }; 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 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 pub fn get_registered_reflect_types(&self, reg: &TypeRegistration) -> Vec { self.type_data_map .iter() .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_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_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))) } }