Skip to content

Commit ac6730d

Browse files
kdeng00Serial-ATA
andcommitted
misc: Add list of file extensions
Co-authored-by: Serial <69764315+serial-ata@users.noreply.github.com>
1 parent 062ec3e commit ac6730d

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1414
- Support can be enabled with the new `serde` feature (not enabled by default)
1515
- **Probe**: `Probe::read_bound()` ([PR](https://github.com/Serial-ATA/lofty-rs/pull/557))
1616
- Same as `Probe::read()`, but returns a [`BoundTaggedFile`](https://docs.rs/lofty/latest/lofty/file/struct.BoundTaggedFile.html)
17+
- **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))
18+
- This is useful for filtering files when scanning directories. If your app uses extension filtering, **please consider switching to this**, as to not
19+
miss any supported files.
1720

1821
### Changed
1922
- **ID3v2**: Check `TXXX:ALBUMARTIST` and `TXXX:ALBUM ARTIST` for `ItemKey::AlbumArtist` conversions

lofty/src/file/file_type.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,44 @@ use crate::tag::TagType;
55
use std::ffi::OsStr;
66
use std::path::Path;
77

8+
/// List of common audio extensions
9+
///
10+
/// This contains a bunch of common extensions for all supported [`FileType`]s, and can be used a filter
11+
/// when scanning directories.
12+
///
13+
/// NOTE: This is **not** an exhaustive list, but it should work fine in most cases.
14+
///
15+
/// # Examples
16+
///
17+
/// ```rust,no_run
18+
/// use lofty::file::EXTENSIONS;
19+
/// use std::fs;
20+
///
21+
/// # fn main() -> lofty::error::Result<()> {
22+
/// for entry in fs::read_dir(".")? {
23+
/// let entry = entry?;
24+
///
25+
/// let path = entry.path();
26+
/// let Some(extension) = path.extension() else {
27+
/// continue;
28+
/// };
29+
///
30+
/// // Skip any non-audio file extensions
31+
/// if !EXTENSIONS.iter().any(|e| *e == extension) {
32+
/// continue;
33+
/// }
34+
///
35+
/// // `entry` is *most likely* a supported file at this point
36+
/// let parsed = lofty::read_from_path(path)?;
37+
/// }
38+
/// # Ok(()) }
39+
/// ```
40+
pub const EXTENSIONS: &[&str] = &[
41+
// Also update `FileType::from_ext()` below
42+
"aac", "ape", "aiff", "aif", "afc", "aifc", "mp3", "mp2", "mp1", "wav", "wv", "opus", "flac",
43+
"ogg", "mp4", "m4a", "m4b", "m4p", "m4r", "m4v", "3gp", "mpc", "mp+", "mpp", "spx",
44+
];
45+
846
/// The type of file read
947
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
1048
#[derive(PartialEq, Eq, Copy, Clone, Debug)]
@@ -129,6 +167,7 @@ impl FileType {
129167
}
130168
}
131169

170+
// Also update `EXTENSIONS` above
132171
match ext.as_str() {
133172
"aac" => Some(Self::Aac),
134173
"ape" => Some(Self::Ape),

lofty/src/file/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ mod file_type;
55
mod tagged_file;
66

77
pub use audio_file::AudioFile;
8-
pub use file_type::FileType;
8+
pub use file_type::{EXTENSIONS, FileType};
99
pub use tagged_file::{BoundTaggedFile, TaggedFile, TaggedFileExt};
1010

1111
pub(crate) use file_type::FileTypeGuessResult;

0 commit comments

Comments
 (0)