Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Serial-ATA committed Jul 23, 2024
1 parent 9a4d760 commit 8f049f0
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 26 deletions.
47 changes: 25 additions & 22 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down Expand Up @@ -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;
Expand Down
9 changes: 5 additions & 4 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -39,7 +39,8 @@ fn unescape_html(content: &str) -> String {
.build([
"&nbsp;", "&lt;", "&gt;", "&amp;", "&quot;", "&apos;", "&cent;", "&pound;",
"&yen;", "&euro;", "&copy;", "&reg;", "&ndash;", "&mdash;",
]).unwrap()
])
.unwrap()
});

MATCHER.replace_all(
Expand All @@ -51,10 +52,10 @@ fn unescape_html(content: &str) -> String {
}

#[rustfmt::skip]
fn unescape_utf8(content: &str) -> Cow<str> {
fn unescape_utf8(content: &str) -> Cow<'_, str> {
static UNESCAPE_UTF8_REGEX: Lazy<Regex> = 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();
Expand Down

0 comments on commit 8f049f0

Please sign in to comment.