diff --git a/CHANGELOG.md b/CHANGELOG.md index 6b89aebb7..076becf78 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Support can be enabled with the new `serde` feature (not enabled by default) - **Probe**: `Probe::read_bound()` ([PR](https://github.com/Serial-ATA/lofty-rs/pull/557)) - Same as `Probe::read()`, but returns a [`BoundTaggedFile`](https://docs.rs/lofty/latest/lofty/file/struct.BoundTaggedFile.html) +- **Other**: `EXTENSIONS` list containing common file extensions for all supported audio file types ([issue](https://github.com/Serial-ATA/lofty-rs/issues/509)) ([PR](https://github.com/Serial-ATA/lofty-rs/pull/558)) + - This is useful for filtering files when scanning directories. If your app uses extension filtering, **please consider switching to this**, as to not + miss any supported files. ### Changed - **ID3v2**: Check `TXXX:ALBUMARTIST` and `TXXX:ALBUM ARTIST` for `ItemKey::AlbumArtist` conversions diff --git a/lofty/src/file/file_type.rs b/lofty/src/file/file_type.rs index d401a7adc..7b3a12802 100644 --- a/lofty/src/file/file_type.rs +++ b/lofty/src/file/file_type.rs @@ -5,6 +5,44 @@ use crate::tag::TagType; use std::ffi::OsStr; use std::path::Path; +/// List of common audio extensions +/// +/// This contains a bunch of common extensions for all supported [`FileType`]s, and can be used a filter +/// when scanning directories. +/// +/// NOTE: This is **not** an exhaustive list, but it should work fine in most cases. +/// +/// # Examples +/// +/// ```rust,no_run +/// use lofty::file::EXTENSIONS; +/// use std::fs; +/// +/// # fn main() -> lofty::error::Result<()> { +/// for entry in fs::read_dir(".")? { +/// let entry = entry?; +/// +/// let path = entry.path(); +/// let Some(extension) = path.extension() else { +/// continue; +/// }; +/// +/// // Skip any non-audio file extensions +/// if !EXTENSIONS.iter().any(|e| *e == extension) { +/// continue; +/// } +/// +/// // `entry` is *most likely* a supported file at this point +/// let parsed = lofty::read_from_path(path)?; +/// } +/// # Ok(()) } +/// ``` +pub const EXTENSIONS: &[&str] = &[ + // Also update `FileType::from_ext()` below + "aac", "ape", "aiff", "aif", "afc", "aifc", "mp3", "mp2", "mp1", "wav", "wv", "opus", "flac", + "ogg", "mp4", "m4a", "m4b", "m4p", "m4r", "m4v", "3gp", "mpc", "mp+", "mpp", "spx", +]; + /// The type of file read #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] #[derive(PartialEq, Eq, Copy, Clone, Debug)] @@ -129,6 +167,7 @@ impl FileType { } } + // Also update `EXTENSIONS` above match ext.as_str() { "aac" => Some(Self::Aac), "ape" => Some(Self::Ape), diff --git a/lofty/src/file/mod.rs b/lofty/src/file/mod.rs index 2e6156fe8..55aae3adf 100644 --- a/lofty/src/file/mod.rs +++ b/lofty/src/file/mod.rs @@ -5,7 +5,7 @@ mod file_type; mod tagged_file; pub use audio_file::AudioFile; -pub use file_type::FileType; +pub use file_type::{EXTENSIONS, FileType}; pub use tagged_file::{BoundTaggedFile, TaggedFile, TaggedFileExt}; pub(crate) use file_type::FileTypeGuessResult;