diff --git a/src/main.rs b/src/main.rs index 3852057..252cdb0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -17,10 +17,10 @@ use std::path::PathBuf; use std::{fs, process}; use clap::{Parser, ValueHint}; -use lofty::tag::{Accessor, ItemKey}; -use lofty::file::{AudioFile, TaggedFileExt}; use lofty::config::{ParseOptions, WriteOptions}; +use lofty::file::{AudioFile, TaggedFileExt}; use lofty::probe::Probe; +use lofty::tag::{Accessor, ItemKey}; #[derive(Parser)] #[clap(name = "lyr")] @@ -62,31 +62,34 @@ fn main() { fn real_main(args: Args) -> Result<()> { let config = Config::read()?; - let (title, artist) = { - if let (Some(title), Some(artist)) = (args.title, args.artist) { - (title.to_lowercase(), artist.to_lowercase()) - } else { - let file = Probe::open(args.input.as_ref().unwrap())? - .options(ParseOptions::new().read_properties(false)) - .read()?; + let (title, artist); - let mut title = None; - let mut artist = None; - for tag in file.tags() { - if title.is_some() && artist.is_some() { - break; - } + if let (Some(arg_title), Some(arg_artist)) = (args.title, args.artist) { + title = arg_title.to_lowercase(); + artist = arg_artist.to_lowercase(); + } else { + let file = Probe::open(args.input.as_ref().unwrap())? + .options(ParseOptions::new().read_properties(false)) + .read()?; - title = tag.title().map(|title| title.to_lowercase()); - artist = tag.artist().map(|artist| artist.to_lowercase()); + let mut tag_title = None; + let mut tag_artist = None; + for tag in file.tags() { + if tag_title.is_some() && tag_artist.is_some() { + break; } - match (title, artist) { - (Some(title), Some(artist)) => (title, artist), - (None, _) | (_, None) => return Err(Error::InvalidTags), - } + tag_title = tag.title().map(|title| title.to_lowercase()); + tag_artist = tag.artist().map(|artist| artist.to_lowercase()); } - }; + + let (Some(tag_title), Some(tag_artist)) = (tag_title, tag_artist) else { + return Err(Error::InvalidTags); + }; + + title = tag_title; + artist = tag_artist; + } let mut fetchers = config.fetchers.iter(); let lyrics; diff --git a/src/utils.rs b/src/utils.rs index 941b918..e1ed0d8 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -2,8 +2,8 @@ use std::borrow::Cow; use std::io::Write; use aho_corasick::{AhoCorasick, AhoCorasickBuilder, MatchKind}; -use env_logger::{Builder, Target, WriteStyle}; use env_logger::fmt::style::{AnsiColor, Color, Style}; +use env_logger::{Builder, Target, WriteStyle}; use log::{Level, LevelFilter}; use once_cell::sync::Lazy; use regex::{Captures, Regex}; @@ -39,7 +39,8 @@ fn unescape_html(content: &str) -> String { .build([ " ", "<", ">", "&", """, "'", "¢", "£", "¥", "€", "©", "®", "–", "—", - ]).unwrap() + ]) + .unwrap() }); MATCHER.replace_all( @@ -51,10 +52,10 @@ fn unescape_html(content: &str) -> String { } #[rustfmt::skip] -fn unescape_utf8(content: &str) -> Cow { +fn unescape_utf8(content: &str) -> Cow<'_, str> { static UNESCAPE_UTF8_REGEX: Lazy = Lazy::new(|| Regex::new(r"&#(\w{0,5});").unwrap()); - UNESCAPE_UTF8_REGEX.replace_all(content, |c: &Captures| { + UNESCAPE_UTF8_REGEX.replace_all(content, |c: &Captures<'_>| { let cont = &c[1]; let radix = if &cont[..1] == "x" { 16 } else { 10 }; let n = u32::from_str_radix(&cont[1..], radix).unwrap();