Skip to content

Commit

Permalink
Merge pull request #149 from NiklasEi/update-syn-v2
Browse files Browse the repository at this point in the history
Update to syn v2
  • Loading branch information
NiklasEi authored Aug 4, 2023
2 parents d048c88 + 454be80 commit ce52620
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 102 deletions.
4 changes: 2 additions & 2 deletions bevy_asset_loader/src/loading_state/systems.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub(crate) fn start_loading_collection<S: States, Assets: AssetCollection>(
);
let (mut asset_loader_configuration, state) = system_state.get_mut(world);

let mut config = asset_loader_configuration
let config = asset_loader_configuration
.state_configurations
.get_mut(state.get())
.unwrap_or_else(|| {
Expand Down Expand Up @@ -102,7 +102,7 @@ fn count_loaded_handles<S: States, Assets: AssetCollection>(cell: WorldCell) ->
let mut asset_loader_configuration = cell
.get_resource_mut::<AssetLoaderConfiguration<S>>()
.expect("Cannot get AssetLoaderConfiguration resource");
if let Some(mut config) = asset_loader_configuration
if let Some(config) = asset_loader_configuration
.state_configurations
.get_mut(state.get())
{
Expand Down
2 changes: 1 addition & 1 deletion bevy_asset_loader_derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ proc-macro = true

[dependencies]
proc-macro2 = "1.0"
syn = "1.0"
syn = "2.0"
quote = "1.0"
245 changes: 146 additions & 99 deletions bevy_asset_loader_derive/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! This crate adds support for auto deriving [`AssetCollection`]
//! This crate adds support for deriving [`AssetCollection`]
//!
//! You do not have to use it directly. Just import ``AssetCollection`` from ``bevy_asset_loader``
//! and use ``#[derive(AssetCollection)]`` to derive the trait.
//! You do not have to use it directly. Just import [`AssetCollection`] from `bevy_asset_loader`
//! and use `#[derive(AssetCollection)]` to derive the trait.
#![forbid(unsafe_code)]
#![warn(unused_imports)]
Expand All @@ -17,7 +17,8 @@ use std::result::Result::{Err, Ok};
use crate::assets::*;
use proc_macro2::Ident;
use quote::{quote, quote_spanned, ToTokens, TokenStreamExt};
use syn::{Data, Field, Fields, Index, Lit, Meta, NestedMeta};
use syn::punctuated::Punctuated;
use syn::{Data, Expr, ExprLit, Field, Fields, Index, Lit, LitStr, Meta, Token};

/// Derive macro for [`AssetCollection`]
///
Expand Down Expand Up @@ -230,75 +231,35 @@ enum ParseFieldError {
fn parse_field(field: &Field) -> Result<AssetField, Vec<ParseFieldError>> {
let mut builder = AssetBuilder::default();
let mut errors = vec![];
for attr in field.attrs.iter() {
if let Meta::List(ref asset_meta_list) = attr.parse_meta().unwrap() {
if *asset_meta_list.path.get_ident().unwrap() != ASSET_ATTRIBUTE {
continue;
}
builder.field_ident = Some(field.clone().ident.unwrap());
for attr in field
.attrs
.iter()
.filter(|attribute| attribute.path().is_ident(ASSET_ATTRIBUTE))
{
let asset_meta_list = attr.parse_args_with(Punctuated::<Meta, Token![,]>::parse_terminated);

for attribute in asset_meta_list.nested.iter() {
if let NestedMeta::Meta(Meta::NameValue(ref named_value)) = attribute {
let path = named_value.path.get_ident().unwrap().clone();
builder.field_ident = Some(field.clone().ident.unwrap());

if path == PATH_ATTRIBUTE {
if let Lit::Str(path_literal) = &named_value.lit {
builder.asset_path = Some(path_literal.value());
} else {
errors.push(ParseFieldError::WrongAttributeType(
named_value.into_token_stream(),
"str",
));
}
} else if path == KEY_ATTRIBUTE {
if let Lit::Str(path_literal) = &named_value.lit {
builder.key = Some(path_literal.value());
} else {
errors.push(ParseFieldError::WrongAttributeType(
named_value.into_token_stream(),
"str",
));
}
} else {
errors.push(ParseFieldError::UnknownAttribute(
named_value.into_token_stream(),
))
}
} else if let NestedMeta::Meta(Meta::Path(ref meta_path)) = attribute {
let path = meta_path.get_ident().unwrap().clone();
if path == STANDARD_MATERIAL_ATTRIBUTE {
#[cfg(not(feature = "3d"))]
errors.push(ParseFieldError::Missing3dFeature(
meta_path.into_token_stream(),
));
#[cfg(feature = "3d")]
{
builder.is_standard_material = true;
}
} else if path == OPTIONAL_ATTRIBUTE {
builder.is_optional = true;
} else if path == COLLECTION_ATTRIBUTE {
builder.is_collection = true;
} else if path == TYPED_ATTRIBUTE {
builder.is_typed = true;
} else {
errors.push(ParseFieldError::UnknownAttribute(
meta_path.into_token_stream(),
))
}
} else if let NestedMeta::Meta(Meta::List(ref meta_list)) = attribute {
let path = meta_list.path.get_ident().unwrap().clone();
if path == TEXTURE_ATLAS_ATTRIBUTE {
#[cfg(not(feature = "2d"))]
errors.push(ParseFieldError::Missing2dFeature(
meta_list.into_token_stream(),
));
#[cfg(feature = "2d")]
for attribute in meta_list.nested.iter() {
if let NestedMeta::Meta(Meta::NameValue(ref named_value)) = attribute {
for attribute in asset_meta_list.unwrap() {
match attribute {
Meta::List(meta_list) if meta_list.path.is_ident(TEXTURE_ATLAS_ATTRIBUTE) => {
let texture_atlas_meta_list =
meta_list.parse_args_with(Punctuated::<Meta, Token![,]>::parse_terminated);
#[cfg(not(feature = "2d"))]
errors.push(ParseFieldError::Missing2dFeature(
meta_list.into_token_stream(),
));
#[cfg(feature = "2d")]
for attribute in texture_atlas_meta_list.unwrap() {
match attribute {
Meta::NameValue(named_value) => {
let path = named_value.path.get_ident().unwrap().clone();
if path == TextureAtlasAttribute::TILE_SIZE_X {
if let Lit::Float(width) = &named_value.lit {
if let Expr::Lit(ExprLit {
lit: Lit::Float(width),
..
}) = &named_value.value
{
builder.tile_size_x =
Some(width.base10_parse::<f32>().unwrap());
} else {
Expand All @@ -308,7 +269,11 @@ fn parse_field(field: &Field) -> Result<AssetField, Vec<ParseFieldError>> {
));
}
} else if path == TextureAtlasAttribute::TILE_SIZE_Y {
if let Lit::Float(height) = &named_value.lit {
if let Expr::Lit(ExprLit {
lit: Lit::Float(height),
..
}) = &named_value.value
{
builder.tile_size_y =
Some(height.base10_parse::<f32>().unwrap());
} else {
Expand All @@ -318,7 +283,11 @@ fn parse_field(field: &Field) -> Result<AssetField, Vec<ParseFieldError>> {
));
}
} else if path == TextureAtlasAttribute::COLUMNS {
if let Lit::Int(columns) = &named_value.lit {
if let Expr::Lit(ExprLit {
lit: Lit::Int(columns),
..
}) = &named_value.value
{
builder.columns =
Some(columns.base10_parse::<usize>().unwrap());
} else {
Expand All @@ -328,7 +297,11 @@ fn parse_field(field: &Field) -> Result<AssetField, Vec<ParseFieldError>> {
));
}
} else if path == TextureAtlasAttribute::ROWS {
if let Lit::Int(rows) = &named_value.lit {
if let Expr::Lit(ExprLit {
lit: Lit::Int(rows),
..
}) = &named_value.value
{
builder.rows = Some(rows.base10_parse::<usize>().unwrap());
} else {
errors.push(ParseFieldError::WrongAttributeType(
Expand All @@ -337,7 +310,11 @@ fn parse_field(field: &Field) -> Result<AssetField, Vec<ParseFieldError>> {
));
}
} else if path == TextureAtlasAttribute::PADDING_X {
if let Lit::Float(padding_x) = &named_value.lit {
if let Expr::Lit(ExprLit {
lit: Lit::Float(padding_x),
..
}) = &named_value.value
{
builder.padding_x =
Some(padding_x.base10_parse::<f32>().unwrap());
} else {
Expand All @@ -347,7 +324,11 @@ fn parse_field(field: &Field) -> Result<AssetField, Vec<ParseFieldError>> {
));
}
} else if path == TextureAtlasAttribute::PADDING_Y {
if let Lit::Float(padding_y) = &named_value.lit {
if let Expr::Lit(ExprLit {
lit: Lit::Float(padding_y),
..
}) = &named_value.value
{
builder.padding_y =
Some(padding_y.base10_parse::<f32>().unwrap());
} else {
Expand All @@ -357,7 +338,11 @@ fn parse_field(field: &Field) -> Result<AssetField, Vec<ParseFieldError>> {
));
}
} else if path == TextureAtlasAttribute::OFFSET_X {
if let Lit::Float(offset_x) = &named_value.lit {
if let Expr::Lit(ExprLit {
lit: Lit::Float(offset_x),
..
}) = &named_value.value
{
builder.offset_x =
Some(offset_x.base10_parse::<f32>().unwrap());
} else {
Expand All @@ -367,7 +352,11 @@ fn parse_field(field: &Field) -> Result<AssetField, Vec<ParseFieldError>> {
));
}
} else if path == TextureAtlasAttribute::OFFSET_Y {
if let Lit::Float(offset_y) = &named_value.lit {
if let Expr::Lit(ExprLit {
lit: Lit::Float(offset_y),
..
}) = &named_value.value
{
builder.offset_y =
Some(offset_y.base10_parse::<f32>().unwrap());
} else {
Expand All @@ -381,16 +370,23 @@ fn parse_field(field: &Field) -> Result<AssetField, Vec<ParseFieldError>> {
named_value.into_token_stream(),
));
}
} else {
}
_ => {
errors.push(ParseFieldError::UnknownAttributeType(
attribute.into_token_stream(),
));
}
}
} else if path == COLLECTION_ATTRIBUTE {
builder.is_collection = true;
for attribute in meta_list.nested.iter() {
if let NestedMeta::Meta(Meta::Path(ref meta_path)) = attribute {
}
}
Meta::List(meta_list) if meta_list.path.is_ident(COLLECTION_ATTRIBUTE) => {
let collection_meta_list =
meta_list.parse_args_with(Punctuated::<Meta, Token![,]>::parse_terminated);

builder.is_collection = true;
for attribute in collection_meta_list.unwrap() {
match attribute {
Meta::Path(meta_path) => {
let path = meta_path.get_ident().unwrap().clone();
if path == TYPED_ATTRIBUTE {
builder.is_typed = true;
Expand All @@ -401,34 +397,85 @@ fn parse_field(field: &Field) -> Result<AssetField, Vec<ParseFieldError>> {
meta_path.into_token_stream(),
))
}
} else {
errors.push(ParseFieldError::UnknownAttributeType(
attribute.into_token_stream(),
));
}
}
} else if path == PATHS_ATTRIBUTE {
let mut paths = vec![];
for attribute in meta_list.nested.iter() {
if let NestedMeta::Lit(Lit::Str(path)) = attribute {
paths.push(path.value());
} else {
_ => {
errors.push(ParseFieldError::UnknownAttributeType(
attribute.into_token_stream(),
));
}
}
builder.asset_paths = Some(paths);
}
}
Meta::List(meta_list) if meta_list.path.is_ident(PATHS_ATTRIBUTE) => {
let paths_meta_list = meta_list
.parse_args_with(Punctuated::<LitStr, Token![,]>::parse_terminated);

let mut paths = vec![];
for path in paths_meta_list.unwrap() {
paths.push(path.value());
// } else {
// errors.push(ParseFieldError::UnknownAttributeType(
// attribute.into_token_stream(),
// ));
// }
}
builder.asset_paths = Some(paths);
}
Meta::List(meta_list) => errors.push(ParseFieldError::UnknownAttribute(
meta_list.into_token_stream(),
)),
Meta::NameValue(named_value) if named_value.path.is_ident(PATH_ATTRIBUTE) => {
if let Expr::Lit(ExprLit {
lit: Lit::Str(path),
..
}) = &named_value.value
{
builder.asset_path = Some(path.value());
} else {
errors.push(ParseFieldError::WrongAttributeType(
named_value.into_token_stream(),
"str",
));
}
}
Meta::NameValue(named_value) if named_value.path.is_ident(KEY_ATTRIBUTE) => {
if let Expr::Lit(ExprLit {
lit: Lit::Str(key), ..
}) = &named_value.value
{
builder.key = Some(key.value());
} else {
errors.push(ParseFieldError::UnknownAttribute(
meta_list.into_token_stream(),
))
errors.push(ParseFieldError::WrongAttributeType(
named_value.into_token_stream(),
"str",
));
}
} else {
errors.push(ParseFieldError::UnknownAttributeType(
attribute.into_token_stream(),
}
Meta::NameValue(named_value) => errors.push(ParseFieldError::UnknownAttribute(
named_value.into_token_stream(),
)),
Meta::Path(meta_path) if meta_path.is_ident(STANDARD_MATERIAL_ATTRIBUTE) => {
#[cfg(not(feature = "3d"))]
errors.push(ParseFieldError::Missing3dFeature(
meta_path.into_token_stream(),
));
#[cfg(feature = "3d")]
{
builder.is_standard_material = true;
}
}
Meta::Path(meta_path) if meta_path.is_ident(OPTIONAL_ATTRIBUTE) => {
builder.is_optional = true;
}
Meta::Path(meta_path) if meta_path.is_ident(COLLECTION_ATTRIBUTE) => {
builder.is_collection = true;
}
Meta::Path(meta_path) if meta_path.is_ident(TYPED_ATTRIBUTE) => {
builder.is_typed = true;
}
Meta::Path(meta_path) => errors.push(ParseFieldError::UnknownAttribute(
meta_path.into_token_stream(),
)),
}
}
}
Expand Down

0 comments on commit ce52620

Please sign in to comment.