From 9e43a5818d7dd2adc66a7e406dafb982e2f3c5a4 Mon Sep 17 00:00:00 2001 From: blank X Date: Sun, 20 Dec 2020 17:14:55 +0700 Subject: [PATCH] Remove utils::human_sauce_info --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/commands/download.rs | 2 +- src/commands/view.rs | 2 +- src/structs.rs | 39 +++++++++++++++++++++++++++++++++++++++ src/utils.rs | 37 ------------------------------------- 6 files changed, 43 insertions(+), 41 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3c83a48..56dcfca 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -421,7 +421,7 @@ dependencies = [ [[package]] name = "nhentairs" -version = "0.5.0" +version = "0.5.1" dependencies = [ "reqwest", "serde", diff --git a/Cargo.toml b/Cargo.toml index 019026f..38d16bc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "nhentairs" -version = "0.5.0" +version = "0.5.1" authors = ["blank X "] edition = "2018" diff --git a/src/commands/download.rs b/src/commands/download.rs index 8353b99..7a7fc54 100644 --- a/src/commands/download.rs +++ b/src/commands/download.rs @@ -40,7 +40,7 @@ pub async fn run(args: env::Args) { let base_path = sauce_info.id.to_string(); let base_path = Path::new(&base_path); match create_dir(base_path) { - Ok(()) => write(base_path.join("info.txt"), format!("{}\n", utils::human_sauce_info(&sauce_info))).unwrap(), + Ok(()) => write(base_path.join("info.txt"), format!("{}\n", &sauce_info)).unwrap(), Err(err) => match err.kind() { std::io::ErrorKind::AlreadyExists => (), _ => panic!("Got a weird error while creating dir: {}", err) diff --git a/src/commands/view.rs b/src/commands/view.rs index 55ac7b4..95e3b47 100644 --- a/src/commands/view.rs +++ b/src/commands/view.rs @@ -30,7 +30,7 @@ pub async fn run(args: env::Args) { if one_done { println!(""); } - println!("{}", utils::human_sauce_info(&sauce_info)); + println!("{}", &sauce_info); }, structs::GalleryInfo::Error(sauce_error) => { if one_done { diff --git a/src/structs.rs b/src/structs.rs index 46fd49e..ed7e33a 100644 --- a/src/structs.rs +++ b/src/structs.rs @@ -1,5 +1,6 @@ use std::fmt; use std::marker::PhantomData; +use std::collections::BTreeMap; use serde::de::{self, Visitor}; use serde::{Deserialize, Deserializer}; @@ -66,6 +67,44 @@ pub struct SearchInfo { pub per_page: i32 } +impl fmt::Display for GalleryInfoSuccess { + fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + let mut text = format!("Sauce: {}\nTitle: ", self.id); + let japanese_title = self.title.japanese.as_ref(); + let english_title = self.title.english.as_ref(); + if english_title.is_some() { + text.push_str(english_title.unwrap()); + if japanese_title.is_some() { + text.push_str(&format!("\nJapanese Title: {}", japanese_title.unwrap())); + } + } else { + text.push_str(japanese_title.unwrap()); + } + let mut tag_hashmap = BTreeMap::new(); + for tag_info in &self.tags { + let tag_key = tag_info.r#type.as_str(); + let tag_value = tag_info.name.as_str(); + let tag_vec = tag_hashmap.entry(tag_key).or_insert(Vec::new()); + tag_vec.push(tag_value); + } + for (tag_key, tag_value) in tag_hashmap { + let tag_key = match tag_key { + "tag" => "Tags", + "artist" => "Artists", + "parody" => "Parodies", + "character" => "Characters", + "group" => "Groups", + "language" => "Languages", + "category" => "Categories", + _ => tag_key + }; + text.push_str(&format!("\n{}: {}", tag_key, tag_value.join(", "))); + } + text.push_str(&format!("\nPages: {}\nFavorites: {}", self.num_pages, self.num_favorites)); + formatter.write_str(&text) + } +} + fn convert_to_i32<'de, D>(deserializer: D) -> Result where D: Deserializer<'de> diff --git a/src/utils.rs b/src/utils.rs index 49d3b78..da18eb8 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -4,7 +4,6 @@ use std::env; use std::fs::File; use std::io::Write; use tokio::stream::StreamExt; -use std::collections::BTreeMap; extern crate serde_json; extern crate reqwest; @@ -53,39 +52,3 @@ pub fn get_arg_sauces(args: env::Args) -> Result, String> { } Ok(sauces) } - -pub fn human_sauce_info(sauce_info: &structs::GalleryInfoSuccess) -> String { - let mut text = format!("Sauce: {}\nTitle: ", sauce_info.id); - let japanese_title = sauce_info.title.japanese.as_ref(); - let english_title = sauce_info.title.english.as_ref(); - if english_title.is_some() { - text.push_str(english_title.unwrap()); - if japanese_title.is_some() { - text.push_str(&format!("\nJapanese Title: {}", japanese_title.unwrap())); - } - } else { - text.push_str(japanese_title.unwrap()); - } - let mut tag_hashmap = BTreeMap::new(); - for tag_info in &sauce_info.tags { - let tag_key = tag_info.r#type.as_str(); - let tag_value = tag_info.name.as_str(); - let tag_vec = tag_hashmap.entry(tag_key).or_insert(Vec::new()); - tag_vec.push(tag_value); - } - for (tag_key, tag_value) in tag_hashmap { - let tag_key = match tag_key { - "tag" => "Tags", - "artist" => "Artists", - "parody" => "Parodies", - "character" => "Characters", - "group" => "Groups", - "language" => "Languages", - "category" => "Categories", - _ => tag_key - }; - text.push_str(&format!("\n{}: {}", tag_key, tag_value.join(", "))); - } - text.push_str(&format!("\nPages: {}\nFavorites: {}", sauce_info.num_pages, sauce_info.num_favorites)); - text -}