Clippy fixes

This commit is contained in:
Piotr Siuszko 2025-06-23 18:06:11 +02:00
parent ef2ff40d75
commit 7530568498
4 changed files with 54 additions and 61 deletions

View File

@ -1,7 +1,7 @@
//! Built-in verbs for the Bevy Remote Protocol. //! Built-in verbs for the Bevy Remote Protocol.
use core::any::TypeId; use core::any::TypeId;
use std::borrow::Cow; use alloc::borrow::Cow;
use anyhow::{anyhow, Result as AnyhowResult}; use anyhow::{anyhow, Result as AnyhowResult};
use bevy_ecs::{ use bevy_ecs::{

View File

@ -8,7 +8,7 @@ use bevy_reflect::{
use core::any::TypeId; use core::any::TypeId;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serde_json::Value; use serde_json::Value;
use std::borrow::Cow; use alloc::borrow::Cow;
use crate::schemas::{ use crate::schemas::{
reflect_info::{SchemaInfoReflect, SchemaNumber}, reflect_info::{SchemaInfoReflect, SchemaNumber},

View File

@ -10,7 +10,7 @@ use bevy_reflect::{
TypeRegistration, TypeRegistration,
}; };
use core::any::TypeId; use core::any::TypeId;
use std::borrow::Cow; use alloc::borrow::Cow;
use crate::schemas::json_schema::JsonSchemaBevyType; use crate::schemas::json_schema::JsonSchemaBevyType;

View File

@ -2,7 +2,7 @@
use bevy_reflect::{GenericInfo, NamedField, Reflect, TypeInfo, UnnamedField, VariantInfo}; use bevy_reflect::{GenericInfo, NamedField, Reflect, TypeInfo, UnnamedField, VariantInfo};
use core::any::TypeId; use core::any::TypeId;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::{ use core::{
any::Any, any::Any,
fmt::Debug, fmt::Debug,
ops::{Bound, RangeBounds}, ops::{Bound, RangeBounds},
@ -148,7 +148,7 @@ impl MinMaxValues {
} }
true true
} }
/// Creates MinMaxValues from a reflected range type. /// Creates `MinMaxValues` from a reflected range type.
/// Attempts to downcast the reflected value to the specified range type T /// Attempts to downcast the reflected value to the specified range type T
/// and extract its bounds. /// and extract its bounds.
pub fn from_reflect<T, Y>(reflect_val: &dyn Reflect) -> Option<MinMaxValues> pub fn from_reflect<T, Y>(reflect_val: &dyn Reflect) -> Option<MinMaxValues>
@ -165,8 +165,8 @@ impl MinMaxValues {
))) )))
} }
/// Creates MinMaxValues from range bounds and a type identifier. /// Creates `MinMaxValues` from range bounds and a type identifier.
/// Takes a tuple containing start bound, end bound, and TypeId to construct /// Takes a tuple containing start bound, end bound, and `TypeId` to construct
/// the appropriate range constraints. /// the appropriate range constraints.
pub fn from_range<T>(value: (Bound<&T>, Bound<&T>, TypeId)) -> MinMaxValues pub fn from_range<T>(value: (Bound<&T>, Bound<&T>, TypeId)) -> MinMaxValues
where where
@ -226,7 +226,7 @@ impl From<TypeId> for MinMaxValues {
pub enum InternalSchemaType { pub enum InternalSchemaType {
/// Represents array-like types (Vec, arrays, lists, sets). /// Represents array-like types (Vec, arrays, lists, sets).
Array { Array {
/// The TypeId of the element type contained in the array. /// The `TypeId` of the element type contained in the array.
element_type: TypeId, element_type: TypeId,
/// Optional type information for the element type. /// Optional type information for the element type.
element_type_info: Option<TypeInfo>, element_type_info: Option<TypeInfo>,
@ -248,13 +248,13 @@ pub enum InternalSchemaType {
/// Generic information about the wrapped type T in Option<T>. /// Generic information about the wrapped type T in Option<T>.
generic: GenericInfo, generic: GenericInfo,
}, },
/// Represents a Map type (e.g., HashMap<K, V>). /// Represents a Map type (e.g., `HashMap`<K, V>).
Map { Map {
/// The TypeId of the key type contained in the map. /// The `TypeId` of the key type contained in the map.
key_type: TypeId, key_type: TypeId,
/// Optional type information for the key type. /// Optional type information for the key type.
key_type_info: Option<TypeInfo>, key_type_info: Option<TypeInfo>,
/// The TypeId of the value type contained in the map. /// The `TypeId` of the value type contained in the map.
value_type: TypeId, value_type: TypeId,
/// Optional type information for the value type. /// Optional type information for the value type.
value_type_info: Option<TypeInfo>, value_type_info: Option<TypeInfo>,
@ -297,10 +297,7 @@ impl From<&SchemaTypeInfo> for Option<SchemaTypeVariant> {
SchemaType::Null, SchemaType::Null,
])) ]))
} }
InternalSchemaType::Regular => match &value.type_id { InternalSchemaType::Regular => value.type_id.as_ref().map(|s| SchemaTypeVariant::Single((*s).into())),
Some(s) => Some(SchemaTypeVariant::Single(s.clone().into())),
_ => None,
},
} }
} }
} }
@ -318,45 +315,44 @@ pub struct SchemaTypeInfo {
pub kind: SchemaKind, pub kind: SchemaKind,
/// Optional Bevy reflection type information. /// Optional Bevy reflection type information.
pub type_info: Option<TypeInfo>, pub type_info: Option<TypeInfo>,
/// Optional TypeId for the type. /// Optional `TypeId` for the type.
pub type_id: Option<TypeId>, pub type_id: Option<TypeId>,
/// Numeric range constraints for the type. /// Numeric range constraints for the type.
pub range: MinMaxValues, pub range: MinMaxValues,
} }
impl Into<JsonSchemaVariant> for SchemaTypeInfo { impl From<SchemaTypeInfo> for JsonSchemaVariant {
fn into(self) -> JsonSchemaVariant { fn from(val: SchemaTypeInfo) -> Self {
let schema_type: Option<SchemaTypeVariant> = (&self).into(); let schema_type: Option<SchemaTypeVariant> = (&val).into();
let mut schema = JsonSchemaBevyType { let mut schema = JsonSchemaBevyType {
schema_type: schema_type.clone(), schema_type: schema_type.clone(),
kind: self.kind.clone(), kind: val.kind.clone(),
description: self.documentation.clone(), description: val.documentation.clone(),
type_path: self type_path: val
.type_info .type_info
.as_ref() .as_ref()
.and_then(|s| Some(s.type_path_table().path().into())) .map(|s| (s.type_path_table().path().into()))
.unwrap_or_default(), .unwrap_or_default(),
short_path: self short_path: val
.type_info .type_info
.as_ref() .as_ref().map(|s| s.type_path_table().short_path().into())
.and_then(|s| Some(s.type_path_table().short_path().into()))
.unwrap_or_default(), .unwrap_or_default(),
crate_name: self crate_name: val
.type_info .type_info
.as_ref() .as_ref()
.and_then(|s| s.type_path_table().crate_name().map(Into::into)), .and_then(|s| s.type_path_table().crate_name().map(Into::into)),
module_path: self module_path: val
.type_info .type_info
.as_ref() .as_ref()
.and_then(|s| s.type_path_table().module_path().map(Into::into)), .and_then(|s| s.type_path_table().module_path().map(Into::into)),
minimum: self.range.min.as_ref().and_then(|r| r.get_inclusive()), minimum: val.range.min.as_ref().and_then(BoundValue::get_inclusive),
maximum: self.range.max.as_ref().and_then(|r| r.get_inclusive()), maximum: val.range.max.as_ref().and_then(BoundValue::get_inclusive),
exclusive_minimum: self.range.min.as_ref().and_then(|r| r.get_exclusive()), exclusive_minimum: val.range.min.as_ref().and_then(BoundValue::get_exclusive),
exclusive_maximum: self.range.max.as_ref().and_then(|r| r.get_exclusive()), exclusive_maximum: val.range.max.as_ref().and_then(BoundValue::get_exclusive),
..Default::default() ..Default::default()
}; };
match self.internal_type { match val.internal_type {
InternalSchemaType::Map { InternalSchemaType::Map {
key_type, key_type,
key_type_info, key_type_info,
@ -378,7 +374,7 @@ impl Into<JsonSchemaVariant> for SchemaTypeInfo {
} }
InternalSchemaType::Regular => {} InternalSchemaType::Regular => {}
InternalSchemaType::EnumHolder(variants) => { InternalSchemaType::EnumHolder(variants) => {
schema.one_of = variants.iter().map(|v| v.build_schema()).collect(); schema.one_of = variants.iter().map(SchemaInfoReflect::build_schema).collect();
} }
InternalSchemaType::EnumVariant(variant_info) => match &variant_info { InternalSchemaType::EnumVariant(variant_info) => match &variant_info {
VariantInfo::Struct(struct_variant_info) => { VariantInfo::Struct(struct_variant_info) => {
@ -441,12 +437,12 @@ impl Into<JsonSchemaVariant> for SchemaTypeInfo {
if let JsonSchemaVariant::Schema(new_schema_type) = new_schema { if let JsonSchemaVariant::Schema(new_schema_type) = new_schema {
schema = *new_schema_type; schema = *new_schema_type;
schema.schema_type = schema_type.clone(); schema.schema_type = schema_type.clone();
schema.description = self.documentation.clone(); schema.description = val.documentation.clone();
} else { } else {
return new_schema; return new_schema;
} }
} else { } else {
schema.prefix_items = unnamed_fields.iter().map(|s| s.build_schema()).collect(); schema.prefix_items = unnamed_fields.iter().map(SchemaInfoReflect::build_schema).collect();
schema.min_items = Some(unnamed_fields.len() as u64); schema.min_items = Some(unnamed_fields.len() as u64);
schema.max_items = Some(unnamed_fields.len() as u64); schema.max_items = Some(unnamed_fields.len() as u64);
} }
@ -470,13 +466,13 @@ impl Into<JsonSchemaVariant> for SchemaTypeInfo {
if let JsonSchemaVariant::Schema(value) = schema_variant { if let JsonSchemaVariant::Schema(value) = schema_variant {
schema = *value; schema = *value;
schema.schema_type = schema_type.clone(); schema.schema_type = schema_type.clone();
schema.minimum = self.range.min.as_ref().and_then(|r| r.get_inclusive()); schema.minimum = val.range.min.as_ref().and_then(BoundValue::get_inclusive);
schema.maximum = self.range.max.as_ref().and_then(|r| r.get_inclusive()); schema.maximum = val.range.max.as_ref().and_then(BoundValue::get_inclusive);
schema.exclusive_minimum = schema.exclusive_minimum =
self.range.min.as_ref().and_then(|r| r.get_exclusive()); val.range.min.as_ref().and_then(BoundValue::get_exclusive);
schema.exclusive_maximum = schema.exclusive_maximum =
self.range.max.as_ref().and_then(|r| r.get_exclusive()); val.range.max.as_ref().and_then(BoundValue::get_exclusive);
schema.description = self.documentation; schema.description = val.documentation;
schema.kind = SchemaKind::Optional; schema.kind = SchemaKind::Optional;
} else { } else {
return schema_variant; return schema_variant;
@ -515,21 +511,18 @@ pub trait SchemaInfoReflect {
if SchemaType::try_get_primitive_type_from_type_id(self.get_type()).is_some() { if SchemaType::try_get_primitive_type_from_type_id(self.get_type()).is_some() {
return SchemaKind::Value; return SchemaKind::Value;
} }
match self.try_get_type_info() { if let Some(type_info) = self.try_get_type_info() {
Some(type_info) => { return match type_info {
return match type_info { TypeInfo::Struct(_) => SchemaKind::Struct,
TypeInfo::Struct(_) => SchemaKind::Struct, TypeInfo::TupleStruct(_) => SchemaKind::TupleStruct,
TypeInfo::TupleStruct(_) => SchemaKind::TupleStruct, TypeInfo::Tuple(_) => SchemaKind::Tuple,
TypeInfo::Tuple(_) => SchemaKind::Tuple, TypeInfo::List(_) => SchemaKind::List,
TypeInfo::List(_) => SchemaKind::List, TypeInfo::Array(_) => SchemaKind::Array,
TypeInfo::Array(_) => SchemaKind::Array, TypeInfo::Map(_) => SchemaKind::Map,
TypeInfo::Map(_) => SchemaKind::Map, TypeInfo::Set(_) => SchemaKind::Set,
TypeInfo::Set(_) => SchemaKind::Set, TypeInfo::Enum(_) => SchemaKind::Enum,
TypeInfo::Enum(_) => SchemaKind::Enum, TypeInfo::Opaque(_) => SchemaKind::Opaque,
TypeInfo::Opaque(_) => SchemaKind::Opaque,
}
} }
None => {}
} }
SchemaKind::Value SchemaKind::Value
} }
@ -627,7 +620,7 @@ pub trait SchemaInfoReflect {
None None
} }
/// Get the underlying TypeId /// Get the underlying `TypeId`
fn get_type(&self) -> TypeId; fn get_type(&self) -> TypeId;
/// Try to get the attribute by id /// Try to get the attribute by id
@ -635,7 +628,7 @@ pub trait SchemaInfoReflect {
None None
} }
/// Creates MinMaxValues from a reflected range type. /// Creates `MinMaxValues` from a reflected range type.
/// Attempts to downcast the reflected value to the specified range type T /// Attempts to downcast the reflected value to the specified range type T
/// and extract its bounds. /// and extract its bounds.
fn min_max_from_attribute<T, Y>(&self) -> Option<MinMaxValues> fn min_max_from_attribute<T, Y>(&self) -> Option<MinMaxValues>
@ -647,7 +640,7 @@ pub trait SchemaInfoReflect {
.and_then(|reflect_value| MinMaxValues::from_reflect::<T, Y>(reflect_value)) .and_then(|reflect_value| MinMaxValues::from_reflect::<T, Y>(reflect_value))
} }
/// Creates MinMaxValues from a reflected range type. /// Creates `MinMaxValues` from a reflected range type.
/// Attempts to downcast the reflected value to the specified range type T /// Attempts to downcast the reflected value to the specified range type T
/// and extract its bounds. /// and extract its bounds.
fn min_max_from_attribute_for_type<T>(&self) -> Option<MinMaxValues> fn min_max_from_attribute_for_type<T>(&self) -> Option<MinMaxValues>
@ -681,7 +674,7 @@ pub trait SchemaInfoReflect {
None None
} }
/// Creates MinMaxValues from a reflected range type. /// Creates `MinMaxValues` from a reflected range type.
/// Attempts to downcast the reflected value to the specified range type T /// Attempts to downcast the reflected value to the specified range type T
/// and extract its bounds. /// and extract its bounds.
fn get_range_by_id(&self) -> MinMaxValues { fn get_range_by_id(&self) -> MinMaxValues {
@ -724,7 +717,7 @@ pub trait SchemaInfoReflect {
impl SchemaInfoReflect for UnnamedField { impl SchemaInfoReflect for UnnamedField {
fn try_get_type_info(&self) -> Option<TypeInfo> { fn try_get_type_info(&self) -> Option<TypeInfo> {
self.type_info().and_then(|info| Some(info.clone())) self.type_info().cloned()
} }
#[cfg(feature = "documentation")] #[cfg(feature = "documentation")]
fn get_docs(&self) -> Option<&str> { fn get_docs(&self) -> Option<&str> {
@ -741,7 +734,7 @@ impl SchemaInfoReflect for UnnamedField {
impl SchemaInfoReflect for NamedField { impl SchemaInfoReflect for NamedField {
fn try_get_type_info(&self) -> Option<TypeInfo> { fn try_get_type_info(&self) -> Option<TypeInfo> {
self.type_info().and_then(|info| Some(info.clone())) self.type_info().cloned()
} }
#[cfg(feature = "documentation")] #[cfg(feature = "documentation")]
fn get_docs(&self) -> Option<&str> { fn get_docs(&self) -> Option<&str> {