Remove utils::human_sauce_info

This commit is contained in:
blank X 2020-12-20 17:14:55 +07:00
parent cf9adfd203
commit 9e43a5818d
6 changed files with 43 additions and 41 deletions

2
Cargo.lock generated
View File

@ -421,7 +421,7 @@ dependencies = [
[[package]]
name = "nhentairs"
version = "0.5.0"
version = "0.5.1"
dependencies = [
"reqwest",
"serde",

View File

@ -1,6 +1,6 @@
[package]
name = "nhentairs"
version = "0.5.0"
version = "0.5.1"
authors = ["blank X <theblankx@protonmail.com>"]
edition = "2018"

View File

@ -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)

View File

@ -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 {

View File

@ -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<i32, D::Error>
where
D: Deserializer<'de>

View File

@ -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<Vec<i32>, 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
}