Skip to content

Commit

Permalink
fix: fix various warnings and improve code using clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
Coko7 committed Nov 21, 2024
1 parent 8838f01 commit 8d47ea9
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 64 deletions.
56 changes: 14 additions & 42 deletions src/card_scraper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ impl CardScraper {
let id = Self::fetch_id(dl_elem)?;
let pack_id = pack_id.to_string();
let name = Self::fetch_name(dl_elem)?;
let rarity = Self::fetch_rarity(&localizer, dl_elem)?;
let category = Self::fetch_category(&localizer, dl_elem)?;
let rarity = Self::fetch_rarity(localizer, dl_elem)?;
let category = Self::fetch_category(localizer, dl_elem)?;
let img_url = Self::fetch_img_url(dl_elem)?;
let img_full_url = None;

let colors = Self::fetch_colors(&localizer, dl_elem)?;
let colors = Self::fetch_colors(localizer, dl_elem)?;
let cost = Self::fetch_cost(dl_elem)?;
let attributes = Self::fetch_attributes_2(&localizer, dl_elem)?;
let attributes = Self::fetch_attributes(localizer, dl_elem)?;
let power = Self::fetch_power(dl_elem)?;
let counter = Self::fetch_counter(dl_elem)?;
let types = Self::fetch_types(dl_elem)?;
Expand Down Expand Up @@ -88,7 +88,7 @@ impl CardScraper {
let raw_rarity = Self::get_child_node(element, sel.to_string())?.inner_html();

trace!("fetched card.rarity: {}", raw_rarity);
let rarity = CardRarity::parse(&localizer, &raw_rarity)?;
let rarity = CardRarity::parse(localizer, &raw_rarity)?;

trace!("processed card.rarity");
Ok(rarity)
Expand All @@ -101,7 +101,7 @@ impl CardScraper {
let raw_category = Self::get_child_node(element, sel.to_string())?.inner_html();

trace!("fetched card.category: {}", raw_category);
let category = CardCategory::parse(&localizer, &raw_category)?;
let category = CardCategory::parse(localizer, &raw_category)?;

trace!("processed card.category");
Ok(category)
Expand Down Expand Up @@ -134,7 +134,7 @@ impl CardScraper {
let mut colors = Vec::new();
for (index, raw_color) in raw_colors.iter().enumerate() {
trace!("processing card.colors[{}]: {}", index, raw_color);
let color = CardColor::parse(localizer, &raw_color)?;
let color = CardColor::parse(localizer, raw_color)?;
colors.push(color);
}

Expand All @@ -158,7 +158,7 @@ impl CardScraper {
match raw_cost.parse::<i32>() {
Ok(val) => {
trace!("processed card.cost");
return Ok(Some(val));
Ok(Some(val))
}
Err(e) => Err(anyhow!(
"failed to parse card.cost value `{}`: {}",
Expand All @@ -168,7 +168,7 @@ impl CardScraper {
}
}

pub fn fetch_attributes_2(
pub fn fetch_attributes(
localizer: &Localizer,
element: ElementRef,
) -> Result<Vec<CardAttribute>> {
Expand All @@ -189,7 +189,7 @@ impl CardScraper {
let mut attributes = Vec::new();
for (index, raw_attribute) in raw_attributes.iter().enumerate() {
trace!("processing card.attributes[{}]: {}", index, raw_attribute);
let attribute = CardAttribute::parse(&localizer, &raw_attribute)?;
let attribute = CardAttribute::parse(localizer, raw_attribute)?;
attributes.push(attribute);
}

Expand All @@ -201,34 +201,6 @@ impl CardScraper {
Ok(Vec::new())
}

pub fn fetch_attributes(
localizer: &Localizer,
element: ElementRef,
) -> Result<Vec<CardAttribute>> {
let sel = "dd>div.backCol>div.col2>div.attribute>i";
trace!("fetching card.attributes ({})...", sel);

let raw_attributes = Self::get_child_node(element, sel.to_string())?.inner_html();
trace!("fetched card.attributes: {}", raw_attributes);

if raw_attributes.is_empty() {
trace!("card.attributes empty");
return Ok(Vec::new());
}

let raw_attributes: Vec<&str> = raw_attributes.split('/').collect();

let mut attributes = Vec::new();
for (index, raw_attribute) in raw_attributes.iter().enumerate() {
trace!("processing card.attributes[{}]: {}", index, raw_attribute);
let attribute = CardAttribute::parse(&localizer, &raw_attribute)?;
attributes.push(attribute);
}

trace!("processed card.attributes");
Ok(attributes)
}

pub fn fetch_power(element: ElementRef) -> Result<Option<i32>> {
let sel = "dd>div.backCol>div.col2>div.power";
trace!("fetching card.power ({})...", sel);
Expand All @@ -245,7 +217,7 @@ impl CardScraper {
match raw_power.parse::<i32>() {
Ok(val) => {
trace!("processed card.power");
return Ok(Some(val));
Ok(Some(val))
}
Err(e) => Err(anyhow!(
"failed to parse card.power value `{}`: {}",
Expand All @@ -271,7 +243,7 @@ impl CardScraper {
match raw_counter.parse::<i32>() {
Ok(val) => {
trace!("processed card.counter");
return Ok(Some(val));
Ok(Some(val))
}
Err(e) => Err(anyhow!(
"failed to parse card.counter value `{}`: {}",
Expand Down Expand Up @@ -324,7 +296,7 @@ impl CardScraper {

fn strip_html_tags(value: &str) -> Result<String> {
let reg = Regex::new(r"<[^>]*>.*?</[^>]*>")?;
let result = reg.replace_all(&value, "").trim().to_string();
let result = reg.replace_all(value, "").trim().to_string();
Ok(result)
}

Expand All @@ -334,7 +306,7 @@ impl CardScraper {

match results.len() {
0 => Err(anyhow!("Expected `{}` but got nothing", selector)),
1 => Ok(*results.iter().next().unwrap()),
1 => Ok(*results.first().unwrap()),
_ => Err(anyhow!("Expected single `{}` but got many", selector)),
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{ffi::OsString, path::PathBuf, str::FromStr};

use anyhow::{bail, Result};
use anyhow::Result;
use clap::{command, Parser, Subcommand, ValueEnum};

#[derive(Debug, Parser)]
Expand Down
20 changes: 10 additions & 10 deletions src/interactive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ pub fn show_interactive() -> Result<()> {
let input = input_prompt("Enter language (english): ")?;
info!("user input: {}", input);

let value = input.is_empty().then(|| "english").unwrap_or(&input);
let value = if input.is_empty() { "english" } else { &input };
info!("value to use: {}", value);

let language = match LanguageCode::from_str(&value, true) {
let language = match LanguageCode::from_str(value, true) {
Ok(val) => val,
Err(_) => bail!("Failed to parse language code, invalid value: {}", value),
};
Expand All @@ -33,7 +33,7 @@ pub fn show_interactive() -> Result<()> {
let input = input_prompt("Enter location to save data (./data): ")?;
info!("user input: {}", input);

let value = input.is_empty().then(|| "./data").unwrap_or(&input);
let value = if input.is_empty() { "./data" } else { &input };
info!("value to use: {}", value);

let data_dir = PathBuf::from(&value);
Expand All @@ -50,10 +50,10 @@ pub fn show_interactive() -> Result<()> {
let input = input_prompt(&prompt)?;
info!("user input: {}", input);

let value = input.is_empty().then(|| "no").unwrap_or(&input);
let value = if input.is_empty() { "no" } else { &input };
info!("value to use: {}", value);

if is_yes(&value) {
if is_yes(value) {
println!("Cleared directory: `{}`", data_dir.display());
fs::remove_dir_all(&data_dir)?;
info!("removed directory: {}", data_dir.display());
Expand All @@ -67,10 +67,10 @@ pub fn show_interactive() -> Result<()> {
let input = input_prompt("Download images as well (y/N): ")?;
info!("user input: {}", input);

let value = input.is_empty().then(|| "no").unwrap_or(&input);
let value = if input.is_empty() { "no" } else { &input };
info!("value to use: {}", value);

let download_images = is_yes(&value);
let download_images = is_yes(value);

let localizer = Localizer::load(language)?;
let scraper = OpTcgScraper::new(&localizer);
Expand Down Expand Up @@ -98,7 +98,7 @@ pub fn show_interactive() -> Result<()> {
io::stdout().flush()?;

let cards = scraper.fetch_all_cards(&pack.id)?;
if cards.len() == 0 {
if cards.is_empty() {
error!("no cards available for pack `{}`", &pack.id);
bail!("No cards found");
}
Expand All @@ -118,8 +118,8 @@ pub fn show_interactive() -> Result<()> {
);
io::stdout().flush()?;

let img_data = scraper.download_card_image(&card)?;
store.write_image(&card, img_data)?;
let img_data = scraper.download_card_image(card)?;
store.write_image(card, img_data)?;
println!(" OK");
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ fn download_images(language: LanguageCode, pack_id: &str, output_dir: PathBuf) -
let start = Instant::now();

let cards = scraper.fetch_all_cards(pack_id)?;
if cards.len() == 0 {
if cards.is_empty() {
error!("no cards available for pack `{}`", pack_id);
bail!("no cards found for pack `{}`", pack_id);
}
Expand All @@ -82,7 +82,7 @@ fn download_images(language: LanguageCode, pack_id: &str, output_dir: PathBuf) -
let img_filename = DataStore::get_img_filename(card)?;
let img_path = output_dir.join(img_filename);

let img_data = scraper.download_card_image(&card)?;
let img_data = scraper.download_card_image(card)?;
DataStore::write_image_to_file(img_data, &img_path)?;

info!(
Expand Down Expand Up @@ -125,8 +125,8 @@ fn list_cards(language: LanguageCode, pack_id: &str) -> Result<()> {
info!("fetching all cards...");
let start = Instant::now();

let cards = scraper.fetch_all_cards(&pack_id)?;
if cards.len() == 0 {
let cards = scraper.fetch_all_cards(pack_id)?;
if cards.is_empty() {
error!("No cards available for pack `{}`", pack_id);
return Err(anyhow!("No cards found"));
}
Expand Down
6 changes: 3 additions & 3 deletions src/scraper.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use anyhow::{anyhow, bail, Context, Result};
use anyhow::{bail, Context, Result};
use log::{debug, info};
use reqwest::blocking::{Client, Response};
use std::collections::HashMap;
Expand Down Expand Up @@ -48,7 +48,7 @@ impl<'a> OpTcgScraper<'a> {
for element in document.select(&series_selector) {
match Pack::new(element) {
Ok(pack) => {
if pack.id != "" {
if !pack.id.is_empty() {
packs.push(pack);
}
}
Expand Down Expand Up @@ -91,7 +91,7 @@ impl<'a> OpTcgScraper<'a> {

let card_id = &card_id[1..];

match CardScraper::create_card(&self.localizer, &document, &card_id, &pack_id) {
match CardScraper::create_card(self.localizer, &document, card_id, pack_id) {
Ok(mut card) => {
debug!("computing img_full_url for card: {}", card);
card.img_full_url = Some(self.get_img_full_url(&card.img_url));
Expand Down
11 changes: 7 additions & 4 deletions src/storage.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use anyhow::{bail, Context, Result};
use log::{debug, info, trace};
use reqwest::blocking::Response;
use std::{fs, path::PathBuf};
use std::{
fs,
path::{Path, PathBuf},
};

use crate::{card::Card, cli::LanguageCode, pack::Pack};

Expand All @@ -21,7 +24,7 @@ pub enum StoreLocation<'a> {
}

impl DataStore {
pub fn new(root_dir: &PathBuf, locale: LanguageCode) -> Self {
pub fn new(root_dir: &Path, locale: LanguageCode) -> Self {
DataStore {
root_dir: root_dir.to_path_buf(),
locale,
Expand All @@ -40,7 +43,7 @@ impl DataStore {
StoreLocation::PacksListFile => {
&self.get_path(StoreLocation::JsonDir)?.join("packs.json")
}
StoreLocation::CardsFile(pack_id) => &self.get_cards_filename(&pack_id)?,
StoreLocation::CardsFile(pack_id) => &self.get_cards_filename(pack_id)?,
StoreLocation::ImageFile(card) => {
let filename = Self::get_img_filename(card)?;
&self.get_path(StoreLocation::ImagesDir)?.join(filename)
Expand Down Expand Up @@ -106,7 +109,7 @@ impl DataStore {
pub fn write_cards(&self, pack_id: &str, cards: &Vec<Card>) -> Result<()> {
self.ensure_created(StoreLocation::JsonDir)?;

let path = self.get_path(StoreLocation::CardsFile(&pack_id))?;
let path = self.get_path(StoreLocation::CardsFile(pack_id))?;
debug!(
"about to write {} cards from `{}` to file: `{}`",
cards.len(),
Expand Down

0 comments on commit 8d47ea9

Please sign in to comment.