From 3369767340de17cdefa8b3a620d9656d59ff793c Mon Sep 17 00:00:00 2001 From: Blaine Date: Sat, 2 Aug 2025 14:08:36 -0400 Subject: [PATCH 1/5] feat: add optional serde support for *Type enums --- lofty/Cargo.toml | 6 ++++++ lofty/src/file/file_type.rs | 1 + lofty/src/picture.rs | 3 +++ lofty/src/tag/tag_type.rs | 1 + 4 files changed, 11 insertions(+) diff --git a/lofty/Cargo.toml b/lofty/Cargo.toml index 210748e98..2552234d2 100644 --- a/lofty/Cargo.toml +++ b/lofty/Cargo.toml @@ -30,6 +30,12 @@ paste = "1.0.15" [features] default = ["id3v2_compression_support"] id3v2_compression_support = ["dep:flate2"] +serde = ["dep:serde"] + +[dependencies.serde] +version = "1.0" +features = ["derive"] +optional = true [dev-dependencies] # WAV properties validity tests diff --git a/lofty/src/file/file_type.rs b/lofty/src/file/file_type.rs index 30f7a13e8..045a837f4 100644 --- a/lofty/src/file/file_type.rs +++ b/lofty/src/file/file_type.rs @@ -6,6 +6,7 @@ use std::ffi::OsStr; use std::path::Path; /// The type of file read +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] #[derive(PartialEq, Eq, Copy, Clone, Debug)] #[allow(missing_docs)] #[non_exhaustive] diff --git a/lofty/src/picture.rs b/lofty/src/picture.rs index faf1c7ba4..bb8498cc4 100644 --- a/lofty/src/picture.rs +++ b/lofty/src/picture.rs @@ -38,6 +38,7 @@ pub const APE_PICTURE_TYPES: [&str; 21] = [ ]; /// MIME types for pictures. +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] #[derive(Debug, Clone, Eq, PartialEq, Hash)] #[non_exhaustive] pub enum MimeType { @@ -130,6 +131,7 @@ impl Display for MimeType { /// The picture type, according to ID3v2 APIC #[allow(missing_docs)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] #[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)] #[non_exhaustive] pub enum PictureType { @@ -279,6 +281,7 @@ impl PictureType { /// /// This information is necessary for FLAC's `METADATA_BLOCK_PICTURE`. /// See [`Picture::as_flac_bytes`] for more information. +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] #[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Default)] pub struct PictureInformation { /// The picture's width in pixels diff --git a/lofty/src/tag/tag_type.rs b/lofty/src/tag/tag_type.rs index b3b2103a7..d54d3a318 100644 --- a/lofty/src/tag/tag_type.rs +++ b/lofty/src/tag/tag_type.rs @@ -10,6 +10,7 @@ use std::fs::OpenOptions; use std::path::Path; /// The tag's format +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] #[derive(Copy, Clone, Debug, PartialEq, Eq)] #[non_exhaustive] pub enum TagType { From 1a43c188746833d89bf12a1225593fe1764f0238 Mon Sep 17 00:00:00 2001 From: Blaine Date: Sat, 2 Aug 2025 14:35:32 -0400 Subject: [PATCH 2/5] chore: remove optional serde support for PictureInformation Forgot to save the change before commiting --- lofty/src/picture.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/lofty/src/picture.rs b/lofty/src/picture.rs index bb8498cc4..e8408a0ed 100644 --- a/lofty/src/picture.rs +++ b/lofty/src/picture.rs @@ -281,7 +281,6 @@ impl PictureType { /// /// This information is necessary for FLAC's `METADATA_BLOCK_PICTURE`. /// See [`Picture::as_flac_bytes`] for more information. -#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] #[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Default)] pub struct PictureInformation { /// The picture's width in pixels From 3bf743e670b88781cc251680bfaf5171c8d121d8 Mon Sep 17 00:00:00 2001 From: Blaine Date: Sat, 2 Aug 2025 16:44:20 -0400 Subject: [PATCH 3/5] refactor: allow unsafe derive deserialize for `*Type` enums --- lofty/src/file/file_type.rs | 1 + lofty/src/picture.rs | 2 ++ lofty/src/tag/tag_type.rs | 1 + 3 files changed, 4 insertions(+) diff --git a/lofty/src/file/file_type.rs b/lofty/src/file/file_type.rs index 045a837f4..d401a7adc 100644 --- a/lofty/src/file/file_type.rs +++ b/lofty/src/file/file_type.rs @@ -8,6 +8,7 @@ use std::path::Path; /// The type of file read #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] #[derive(PartialEq, Eq, Copy, Clone, Debug)] +#[allow(clippy::unsafe_derive_deserialize)] #[allow(missing_docs)] #[non_exhaustive] pub enum FileType { diff --git a/lofty/src/picture.rs b/lofty/src/picture.rs index e8408a0ed..539704c73 100644 --- a/lofty/src/picture.rs +++ b/lofty/src/picture.rs @@ -40,6 +40,7 @@ pub const APE_PICTURE_TYPES: [&str; 21] = [ /// MIME types for pictures. #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] #[derive(Debug, Clone, Eq, PartialEq, Hash)] +#[allow(clippy::unsafe_derive_deserialize)] #[non_exhaustive] pub enum MimeType { /// PNG image @@ -131,6 +132,7 @@ impl Display for MimeType { /// The picture type, according to ID3v2 APIC #[allow(missing_docs)] +#[allow(clippy::unsafe_derive_deserialize)] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] #[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)] #[non_exhaustive] diff --git a/lofty/src/tag/tag_type.rs b/lofty/src/tag/tag_type.rs index d54d3a318..cee014326 100644 --- a/lofty/src/tag/tag_type.rs +++ b/lofty/src/tag/tag_type.rs @@ -12,6 +12,7 @@ use std::path::Path; /// The tag's format #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] #[derive(Copy, Clone, Debug, PartialEq, Eq)] +#[allow(clippy::unsafe_derive_deserialize)] #[non_exhaustive] pub enum TagType { /// This covers both APEv1 and APEv2 as it doesn't matter much From 4414cc8c1675a8127c12f5b7624fcdcf0e15e4b3 Mon Sep 17 00:00:00 2001 From: Serial <69764315+Serial-ATA@users.noreply.github.com> Date: Sat, 9 Aug 2025 13:56:12 -0400 Subject: [PATCH 4/5] MimeType: Remove `serde` support Too confusing, doesn't actually serialize to the actual MIME strings, just the enum variant names. --- lofty/src/picture.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/lofty/src/picture.rs b/lofty/src/picture.rs index 539704c73..a813be783 100644 --- a/lofty/src/picture.rs +++ b/lofty/src/picture.rs @@ -38,9 +38,7 @@ pub const APE_PICTURE_TYPES: [&str; 21] = [ ]; /// MIME types for pictures. -#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] #[derive(Debug, Clone, Eq, PartialEq, Hash)] -#[allow(clippy::unsafe_derive_deserialize)] #[non_exhaustive] pub enum MimeType { /// PNG image From 59daf3a6ea27ca2bdf2f89c82293254c3cd16134 Mon Sep 17 00:00:00 2001 From: Serial <69764315+Serial-ATA@users.noreply.github.com> Date: Sat, 9 Aug 2025 14:02:43 -0400 Subject: [PATCH 5/5] changelog: Add entry for new `serde` feature --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7175880d1..0a4307232 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - **ItemKey**: `ItemKey::AlbumArtists`, available for ID3v2, Vorbis Comments, APE, and MP4 Ilst ([PR](https://github.com/Serial-ATA/lofty-rs/pull/523)) - This is a multi-value item that stores each artist for a track. It should be retrieved with `Tag::get_strings` or `Tag::take_strings`. - For example, a track has `ItemKey::TrackArtist` = "Foo & Bar", then `ItemKey::AlbumArtists` = ["Foo", "Bar"]. +- **Serde**: [Serde] support for `*Type` enums (`FileType`, `TagType`, `PictureType`) + - Support can be enabled with the new `serde` feature (not enabled by default) ### Changed - **ID3v2**: Check `TXXX:ALBUMARTIST` and `TXXX:ALBUM ARTIST` for `ItemKey::AlbumArtist` conversions @@ -1012,5 +1014,6 @@ See [ogg_pager's changelog](ogg_pager/CHANGELOG.md). [0.5.1]: https://github.com/Serial-ATA/lofty-rs/compare/0.5.0...0.5.1 [0.5.0]: https://github.com/Serial-ATA/lofty-rs/compare/64f0eff...0.5.0 +[serde]: https://docs.rs/serde [TagLib]: https://github.com/taglib/taglib [ogg_pager's changelog]: ogg_pager/CHANGELOG.md