use match_ignore_case macro

This commit is contained in:
Yannik Böttcher 2020-08-07 12:44:36 +02:00
parent 86a935fd79
commit b2152cecc3

View file

@ -14,6 +14,16 @@ use opus_headers;
use crate::utils; use crate::utils;
use crate::utils::AudioFormat; use crate::utils::AudioFormat;
macro_rules! match_ignore_case {
(match $v:ident {
$( $lit:literal => $res:expr, )*
_ => $catch_all:expr $(,)?
}) => {{
$( if $lit.eq_ignore_ascii_case(&$v) { $res } else )*
{ $catch_all }
}};
}
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
pub struct SongTags { pub struct SongTags {
pub disc_number: Option<u32>, pub disc_number: Option<u32>,
@ -162,15 +172,17 @@ fn read_vorbis(path: &Path) -> Result<SongTags> {
}; };
for (key, value) in source.comment_hdr.comment_list { for (key, value) in source.comment_hdr.comment_list {
match key.as_str() { match_ignore_case! {
"TITLE" => tags.title = Some(value), match key {
"ALBUM" => tags.album = Some(value), "TITLE" => tags.title = Some(value),
"ARTIST" => tags.artist = Some(value), "ALBUM" => tags.album = Some(value),
"ALBUMARTIST" => tags.album_artist = Some(value), "ARTIST" => tags.artist = Some(value),
"TRACKNUMBER" => tags.track_number = value.parse::<u32>().ok(), "ALBUMARTIST" => tags.album_artist = Some(value),
"DISCNUMBER" => tags.disc_number = value.parse::<u32>().ok(), "TRACKNUMBER" => tags.track_number = value.parse::<u32>().ok(),
"DATE" => tags.year = value.parse::<i32>().ok(), "DISCNUMBER" => tags.disc_number = value.parse::<u32>().ok(),
_ => (), "DATE" => tags.year = value.parse::<i32>().ok(),
_ => (),
}
} }
} }
@ -193,20 +205,17 @@ fn read_opus(path: &Path) -> Result<SongTags> {
}; };
for (key, value) in headers.comments.user_comments { for (key, value) in headers.comments.user_comments {
if "TITLE".eq_ignore_ascii_case(&key) { match_ignore_case! {
tags.title = Some(value); match key {
} else if "ALBUM".eq_ignore_ascii_case(&key) { "TITLE" => tags.title = Some(value),
tags.album = Some(value); "ALBUM" => tags.album = Some(value),
} else if "ARTIST".eq_ignore_ascii_case(&key) { "ARTIST" => tags.artist = Some(value),
tags.artist = Some(value); "ALBUMARTIST" => tags.album_artist = Some(value),
} else if "ALBUMARTIST".eq_ignore_ascii_case(&key) { "TRACKNUMBER" => tags.track_number = value.parse::<u32>().ok(),
tags.album_artist = Some(value); "DISCNUMBER" => tags.disc_number = value.parse::<u32>().ok(),
} else if "TRACKNUMBER".eq_ignore_ascii_case(&key) { "DATE" => tags.year = value.parse::<i32>().ok(),
tags.track_number = value.parse().ok(); _ => (),
} else if "DISCNUMBER".eq_ignore_ascii_case(&key) { }
tags.disc_number = value.parse().ok();
} else if "DATE".eq_ignore_ascii_case(&key) {
tags.year = value.parse().ok();
} }
} }