Update ape dependency to new version

This commit is contained in:
Antoine Gersant 2025-02-03 20:58:28 -08:00
parent 1b0b5bd164
commit 7625449434
3 changed files with 25 additions and 29 deletions

4
Cargo.lock generated
View file

@ -52,9 +52,9 @@ checksum = "b3d1d046238990b9cf5bcde22a3fb3584ee5cf65fb2765f454ed428c7a0063da"
[[package]]
name = "ape"
version = "0.5.0"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1cdc864958f3a41f67f998dd2fe9635a525d2f232787d0268690b5e0876a2262"
checksum = "8fb0a5091f05526b27cd637f680c0b20ecc3af600bc74bbf1069fef11d323ab5"
dependencies = [
"byteorder",
]

View file

@ -12,7 +12,7 @@ ui = ["native-windows-gui", "native-windows-derive"]
lto = "thin"
[dependencies]
ape = "0.5"
ape = "0.6"
axum-extra = { version = "0.10.0", features = ["typed-header"] }
axum-range = { version = "0.5.0" }
bitcode = { version = "0.6.3", features = ["serde"] }

View file

@ -125,57 +125,53 @@ mod ape_ext {
use std::sync::LazyLock;
pub fn read_string(item: &ape::Item) -> Option<String> {
match item.value {
ape::ItemValue::Text(ref s) => Some(s.clone()),
_ => None,
}
item.try_into().ok().map(str::to_string)
}
pub fn read_strings(items: Vec<&ape::Item>) -> Vec<String> {
items
.iter()
.filter_map(|i| read_string(i))
// TODO This is a workaround for https://github.com/rossnomann/ape/issues/10
.flat_map(|s| s.split('\0').map(str::to_string).collect::<Vec<_>>())
.collect()
pub fn read_strings(item: Option<&ape::Item>) -> Vec<String> {
let Some(item) = item else {
return vec![];
};
let strings: Vec<&str> = item.try_into().unwrap_or_default();
strings.into_iter().map(str::to_string).collect()
}
pub fn read_i32(item: &ape::Item) -> Option<i32> {
match item.value {
ape::ItemValue::Text(ref s) => s.parse::<i32>().ok(),
_ => None,
}
item.try_into()
.ok()
.map(|s: &str| s.parse::<i32>().ok())
.flatten()
}
static X_OF_Y_REGEX: LazyLock<Regex> = LazyLock::new(|| Regex::new(r#"^\d+"#).unwrap());
pub fn read_x_of_y(item: &ape::Item) -> Option<u32> {
match item.value {
ape::ItemValue::Text(ref s) => {
item.try_into()
.ok()
.map(|s: &str| {
if let Some(m) = X_OF_Y_REGEX.find(s) {
s[m.start()..m.end()].parse().ok()
} else {
None
}
}
_ => None,
}
})
.flatten()
}
}
fn read_ape<P: AsRef<Path>>(path: P) -> Result<SongMetadata, Error> {
let tag = ape::read_from_path(path)?;
let artists = ape_ext::read_strings(tag.items("Artist"));
let artists = ape_ext::read_strings(tag.item("Artist"));
let album = tag.item("Album").and_then(ape_ext::read_string);
let album_artists = ape_ext::read_strings(tag.items("Album artist"));
let album_artists = ape_ext::read_strings(tag.item("Album artist"));
let title = tag.item("Title").and_then(ape_ext::read_string);
let year = tag.item("Year").and_then(ape_ext::read_i32);
let disc_number = tag.item("Disc").and_then(ape_ext::read_x_of_y);
let track_number = tag.item("Track").and_then(ape_ext::read_x_of_y);
let lyricists = ape_ext::read_strings(tag.items("LYRICIST"));
let composers = ape_ext::read_strings(tag.items("COMPOSER"));
let genres = ape_ext::read_strings(tag.items("GENRE"));
let labels = ape_ext::read_strings(tag.items("PUBLISHER"));
let lyricists = ape_ext::read_strings(tag.item("LYRICIST"));
let composers = ape_ext::read_strings(tag.item("COMPOSER"));
let genres = ape_ext::read_strings(tag.item("GENRE"));
let labels = ape_ext::read_strings(tag.item("PUBLISHER"));
Ok(SongMetadata {
artists,
album_artists,