2021-10-03 19:23:44 +00:00
|
|
|
use syn::DeriveInput;
|
|
|
|
|
2022-04-25 13:16:27 +00:00
|
|
|
use crate::symbol::Symbol;
|
2021-10-03 19:23:44 +00:00
|
|
|
|
|
|
|
pub fn parse_attrs(ast: &DeriveInput, attr_name: Symbol) -> syn::Result<Vec<syn::NestedMeta>> {
|
|
|
|
let mut list = Vec::new();
|
|
|
|
for attr in ast.attrs.iter().filter(|a| a.path == attr_name) {
|
|
|
|
match attr.parse_meta()? {
|
|
|
|
syn::Meta::List(meta) => list.extend(meta.nested.into_iter()),
|
|
|
|
other => {
|
|
|
|
return Err(syn::Error::new_spanned(
|
|
|
|
other,
|
2022-10-28 21:03:01 +00:00
|
|
|
format!("expected #[{attr_name}(...)]"),
|
2021-10-03 19:23:44 +00:00
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(list)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_lit_str(attr_name: Symbol, lit: &syn::Lit) -> syn::Result<&syn::LitStr> {
|
|
|
|
if let syn::Lit::Str(lit) = lit {
|
|
|
|
Ok(lit)
|
|
|
|
} else {
|
|
|
|
Err(syn::Error::new_spanned(
|
|
|
|
lit,
|
|
|
|
format!(
|
|
|
|
"expected {} attribute to be a string: `{} = \"...\"`",
|
|
|
|
attr_name, attr_name
|
|
|
|
),
|
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
Derive AsBindGroup Improvements: Better errors, more options, update examples (#5364)
# Objective
- Provide better compile-time errors and diagnostics.
- Add more options to allow more textures types and sampler types.
- Update array_texture example to use upgraded AsBindGroup derive macro.
## Solution
Split out the parsing of the inner struct/field attributes (the inside part of a `#[foo(...)]` attribute) for better clarity
Parse the binding index for all inner attributes, as it is part of all attributes (`#[foo(0, ...)`), then allow each attribute implementer to parse the rest of the attribute metadata as needed. This should make it very trivial to extend/change if needed in the future.
Replaced invocations of `panic!` with the `syn::Error` type, providing fine-grained errors that retains span information. This provides much nicer compile-time errors, and even better IDE errors.
![image](https://user-images.githubusercontent.com/7478134/179452241-6d85d440-4b67-44da-80a7-9d47e8c88b8a.png)
Updated the array_texture example to demonstrate the new changes.
## New AsBindGroup attribute options
### `#[texture(u32, ...)]`
Where `...` is an optional list of arguments.
| Arguments | Values | Default |
|-------------- |---------------------------------------------------------------- | ----------- |
| dimension = "..." | `"1d"`, `"2d"`, `"2d_array"`, `"3d"`, `"cube"`, `"cube_array"` | `"2d"` |
| sample_type = "..." | `"float"`, `"depth"`, `"s_int"` or `"u_int"` | `"float"` |
| filterable = ... | `true`, `false` | `true` |
| multisampled = ... | `true`, `false` | `false` |
| visibility(...) | `all`, `none`, or a list-combination of `vertex`, `fragment`, `compute` | `vertex`, `fragment` |
Example: `#[texture(0, dimension = "2d_array", visibility(vertex, fragment))]`
### `#[sampler(u32, ...)]`
Where `...` is an optional list of arguments.
| Arguments | Values | Default |
|----------- |--------------------------------------------------- | ----------- |
| sampler_type = "..." | `"filtering"`, `"non_filtering"`, `"comparison"`. | `"filtering"` |
| visibility(...) | `all`, `none`, or a list-combination of `vertex`, `fragment`, `compute` | `vertex`, `fragment` |
Example: `#[sampler(0, sampler_type = "filtering", visibility(vertex, fragment)]`
## Changelog
- Added more options to `#[texture(...)]` and `#[sampler(...)]` attributes, supporting more kinds of materials. See above for details.
- Upgraded IDE and compile-time error messages.
- Updated array_texture example using the new options.
2022-07-19 22:05:43 +00:00
|
|
|
|
|
|
|
pub fn get_lit_bool(attr_name: Symbol, lit: &syn::Lit) -> syn::Result<bool> {
|
|
|
|
if let syn::Lit::Bool(lit) = lit {
|
|
|
|
Ok(lit.value())
|
|
|
|
} else {
|
|
|
|
Err(syn::Error::new_spanned(
|
|
|
|
lit,
|
|
|
|
format!(
|
|
|
|
"expected {} attribute to be a bool value, `true` or `false`: `{} = ...`",
|
|
|
|
attr_name, attr_name
|
|
|
|
),
|
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|