Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix some clippy errors #11

Merged
merged 5 commits into from
Mar 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions bevy_proto_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ mod constants;
///
/// #[derive(Clone, Serialize, Deserialize, ProtoComponent)]
/// struct SomeComponent {
/// some_string: String,
/// some_int: i32,
/// some_string: String,
/// some_int: i32,
/// }
///
/// // Which generates:
Expand Down
2 changes: 1 addition & 1 deletion examples/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl ProtoComponent for Person {
/// ```
/// #[derive(Clone, Serialize, Deserialize, Component, ProtoComponent)]
/// struct Person {
/// pub name: String,
/// pub name: String,
/// }
/// ```
#[derive(Copy, Clone, Serialize, Deserialize, ProtoComponent, Component)]
Expand Down
2 changes: 1 addition & 1 deletion src/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ use crate::prototype::Prototypical;
pub trait ProtoComponent: Send + Sync + 'static {
fn insert_self(&self, commands: &mut ProtoCommands, asset_server: &Res<AssetServer>);
#[allow(unused_variables)]
fn prepare(&self, world: &mut World, prototype: &Box<dyn Prototypical>, data: &mut ProtoData) {}
fn prepare(&self, world: &mut World, prototype: &dyn Prototypical, data: &mut ProtoData) {}
}
59 changes: 29 additions & 30 deletions src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ impl Deref for HandlePath {
}
}

type UuidHandleMap = HashMap<Uuid, HandleUntyped>;

/// A resource containing data for all prototypes that need data stored
pub struct ProtoData {
/// Maps Prototype Name -> Component Type -> HandlePath -> Asset Type -> HandleUntyped
Expand All @@ -35,18 +37,15 @@ pub struct ProtoData {
TypeId, // Component Type
HashMap<
String, // Handle Path
HashMap<
Uuid, // Asset UUID
HandleUntyped, // Handle
>,
UuidHandleMap,
>,
>,
>,
prototypes: HashMap<String, Box<dyn Prototypical>>,
}

impl ProtoData {
pub fn new() -> Self {
pub fn empty() -> Self {
Self {
handles: HashMap::default(),
prototypes: HashMap::default(),
Expand All @@ -60,8 +59,8 @@ impl ProtoData {
/// * `name`: The name of the prototype
///
/// returns: Option<&Prototype>
pub fn get_prototype(&self, name: &str) -> Option<&Box<dyn Prototypical>> {
self.prototypes.get(name)
pub fn get_prototype(&self, name: &str) -> Option<&dyn Prototypical> {
self.prototypes.get(name).map(|b| b.as_ref())
}

/// Store a handle
Expand Down Expand Up @@ -104,21 +103,21 @@ impl ProtoData {
/// ```
pub fn insert_handle<T: Asset>(
&mut self,
protoytpe: &Box<dyn Prototypical>,
protoytpe: &dyn Prototypical,
component: &dyn ProtoComponent,
path: &HandlePath,
handle: Handle<T>,
) {
let proto_map = self
.handles
.entry(protoytpe.name().to_string())
.or_insert(HashMap::default());
.or_insert_with(HashMap::default);
let comp_map = proto_map
.entry(component.type_id())
.or_insert(HashMap::default());
.or_insert_with(HashMap::default);
let path_map = comp_map
.entry(path.to_string())
.or_insert(HashMap::default());
.or_insert_with(HashMap::default);
path_map.insert(T::TYPE_UUID, handle.clone_untyped());
}

Expand Down Expand Up @@ -230,7 +229,7 @@ impl FromWorld for ProtoData {
process_path(
world,
&options.extensions,
&options.deserializer,
options.deserializer.as_ref(),
&mut myself,
&directory,
options.recursive_loading,
Expand All @@ -248,9 +247,9 @@ impl FromWorld for ProtoData {
fn process_path(
world: &mut World,
extensions: &Option<Vec<&str>>,
deserializer: &Box<dyn ProtoDeserializer + Send + Sync>,
deserializer: &(dyn ProtoDeserializer + Send + Sync),
myself: &mut ProtoData,
directory: &String,
directory: &str,
recursive: bool,
) {
if let Ok(dir) = std::fs::read_dir(directory) {
Expand All @@ -275,7 +274,7 @@ fn process_path(

if let Some(filters) = &extensions {
if let Some(ext) = path.extension().and_then(OsStr::to_str) {
if filters.iter().find(|filter| *filter == &ext).is_none() {
if !filters.iter().any(|filter| filter == &ext) {
continue;
}
}
Expand All @@ -284,7 +283,7 @@ fn process_path(
if let Ok(data) = std::fs::read_to_string(path) {
if let Some(proto) = deserializer.deserialize(&data) {
for component in proto.iter_components() {
component.prepare(world, &proto, myself);
component.prepare(world, proto.as_ref(), myself);
}

myself.prototypes.insert(proto.name().to_string(), proto);
Expand All @@ -298,12 +297,12 @@ fn process_path(
fn analyze_deps(data: &ProtoData) {
// === Perform Analysis === //
for proto in data.iter() {
check_for_cycles(proto, data, &mut IndexSet::default());
check_for_cycles(proto.as_ref(), data, &mut IndexSet::default());
}

// === Analysis Functions === //
fn check_for_cycles<'a>(
proto: &'a Box<dyn Prototypical>,
proto: &'a dyn Prototypical,
data: &'a ProtoData,
traversed: &mut IndexSet<&'a str>,
) {
Expand Down Expand Up @@ -439,11 +438,11 @@ pub trait ProtoDeserializer: DynClone {
/// // The default implementation:
/// use bevy_proto::{Prototype, Prototypical};
/// fn example_deserialize(data: &str) -> Option<Box<dyn Prototypical>> {
/// if let Ok(value) = serde_yaml::from_str::<Prototype>(data) {
/// Some(Box::new(value))
/// } else {
/// None
/// }
/// if let Ok(value) = serde_yaml::from_str::<Prototype>(data) {
/// Some(Box::new(value))
/// } else {
/// None
/// }
/// }
/// ```
fn deserialize(&self, data: &str) -> Option<Box<dyn Prototypical>>;
Expand All @@ -466,10 +465,10 @@ pub struct ProtoDataOptions {
/// use bevy_proto::ProtoDataOptions;
///
/// let opts = ProtoDataOptions {
/// directories: vec![String::from("assets/prototypes")],
/// recursive_loading: true,
/// extensions: Some(vec!["yaml"]),
/// ..Default::default()
/// directories: vec![String::from("assets/prototypes")],
/// recursive_loading: true,
/// extensions: Some(vec!["yaml"]),
/// ..Default::default()
/// };
/// ```
pub recursive_loading: bool,
Expand All @@ -486,9 +485,9 @@ pub struct ProtoDataOptions {
/// use bevy_proto::ProtoDataOptions;
///
/// let opts = ProtoDataOptions {
/// // Only allow .yaml or .json files
/// extensions: Some(vec!["yaml", "json"]),
/// ..Default::default()
/// // Only allow .yaml or .json files
/// extensions: Some(vec!["yaml", "json"]),
/// ..Default::default()
/// };
/// ```
pub extensions: Option<Vec<&'static str>>,
Expand Down
7 changes: 1 addition & 6 deletions src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::{
prototype::{Prototype, Prototypical},
};

#[derive(Default)]
pub struct ProtoPlugin {
pub options: Option<ProtoDataOptions>,
}
Expand Down Expand Up @@ -121,12 +122,6 @@ impl ProtoPlugin {
}
}

impl Default for ProtoPlugin {
fn default() -> Self {
Self { options: None }
}
}

impl Plugin for ProtoPlugin {
fn build(&self, app: &mut App) {
if let Some(opts) = &self.options {
Expand Down
6 changes: 3 additions & 3 deletions src/prototype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::slice::Iter;

use bevy::ecs::prelude::Commands;
use bevy::ecs::system::EntityCommands;
use bevy::prelude::{AssetServer, Entity, Res};
use bevy::prelude::{AssetServer, Res};
use indexmap::IndexSet;
use serde::{
de::{self, Error, SeqAccess, Visitor},
Expand Down Expand Up @@ -124,7 +124,7 @@ pub trait Prototypical: 'static + Send + Sync {
/// "#).unwrap();
///
/// // Get the EntityCommands for the player entity
/// let entity = commands.entity(player.single().0);
/// let entity = commands.entity(player.single().0);
///
/// // Insert the new components
/// let entity = proto.insert(entity, &data, &asset_server).id();
Expand Down Expand Up @@ -278,7 +278,7 @@ where
{
// Split string by commas
// Allowing for: "A, B, C" to become [A, B, C]
Ok(v.split(",").map(|s| s.trim().to_string()).collect())
Ok(v.split(',').map(|s| s.trim().to_string()).collect())
}

fn visit_seq<A>(self, seq: A) -> Result<Self::Value, A::Error>
Expand Down