lofty_attr: Infer FileType::Custom when using non-internal names

This commit is contained in:
Serial 2022-10-01 18:49:17 -04:00
parent f1ab92733a
commit 82a498f670
No known key found for this signature in database
GPG key ID: DA95198DC17C4568
4 changed files with 19 additions and 6 deletions

View file

@ -20,6 +20,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `read_from{_path}` will no longer take a `bool` for reading properties, and will do it by default. To
change this behavior, you must now use `Probe`.
- **FileType**: `primary_tag_type` will no longer change its return depending on the enabled features.
- **lofty_attr**: Simplify the `file_type` attribute:
- Before, you had to specify custom file types as `#[lofty(file_type = "Custom(\"MyFile\")")]`. Now
you can simply do `#[lofty(file_type = "MyFile")]` and it will infer the rest.
## Removed
- **lofty_attr**: The `#[lofty(always_present)]` attribute has been removed, and is now inferred.

View file

@ -14,7 +14,7 @@ use std::fs::File;
// See `lofty::AudioFile::read_from` for the expected signature.
#[lofty(read_fn = "Self::parse_my_file")]
// The `FileType` variant of the file
#[lofty(file_type = "Custom(\"MyFile\")")]
#[lofty(file_type = "MyFile")]
struct MyFile {
// A file has two requirements, at least one tag field, and a properties field.

View file

@ -65,14 +65,17 @@ fn parse(input: DeriveInput, errors: &mut Vec<syn::Error>) -> proc_macro2::Token
};
let struct_name = input.ident.clone();
// TODO: This is not readable in the slightest
let opt_file_type = internal::opt_internal_file_type(struct_name.to_string());
let has_internal_file_type = opt_file_type.is_some();
let is_internal = input
.attrs
.iter()
.any(|attr| util::has_path_attr(attr, "internal_write_module_do_not_use_anywhere_else"));
// TODO: This is not readable in the slightest
let opt_file_type = internal::opt_internal_file_type(struct_name.to_string());
if opt_file_type.is_none() && is_internal {
// TODO: This is the best check we can do for now I think?
// Definitely needs some work when a better solution comes out.
@ -186,6 +189,13 @@ fn parse(input: DeriveInput, errors: &mut Vec<syn::Error>) -> proc_macro2::Token
let getters = get_getters(&tag_fields, &struct_name);
let file_type_variant = if has_internal_file_type {
quote! { lofty::FileType::#file_type }
} else {
let file_ty_str = file_type.to_string();
quote! { lofty::FileType::Custom(#file_ty_str) }
};
let mut ret = quote! {
#assert_properties_impl
@ -196,7 +206,7 @@ fn parse(input: DeriveInput, errors: &mut Vec<syn::Error>) -> proc_macro2::Token
impl std::convert::From<#struct_name> for lofty::TaggedFile {
fn from(input: #struct_name) -> Self {
lofty::TaggedFile::new(
lofty::FileType::#file_type,
#file_type_variant,
lofty::FileProperties::from(input.properties),
{
let mut tags: Vec<lofty::Tag> = Vec::new();

View file

@ -141,7 +141,7 @@ mod tests {
#[derive(LoftyFile, Default)]
#[lofty(read_fn = "Self::read")]
#[lofty(file_type = "Custom(\"MyFile\")")]
#[lofty(file_type = "MyFile")]
struct MyFile {
#[lofty(tag_type = "ID3v2")]
id3v2_tag: Option<ID3v2Tag>,