diff --git a/src/main.rs b/src/main.rs index 0d78b8b..c044327 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,7 @@ use postgres::{Client, NoTls}; use std::{ env::args, + error::Error, fs::{self, File}, io::{self, BufRead, BufReader, Write}, }; @@ -15,250 +16,218 @@ fn show_usage() { println!("add_custom_name"); } -fn run_as_cli() -> i32 { - let mut result: i32; - let mut client = Client::connect("host=192.168.0.10 port=28945 user=hentai password=h99nqaNPhpfbuuhCDwQXLpZAnoVTjSQP7taoqmQhpzc2rPLVC4JUAKxAHfuuhuU9", NoTls).unwrap(); - let mut option: String = String::new(); - +fn run_as_cli(client: &mut postgres::Client) -> Result<(), Box> { show_usage(); - println!("Please input any of these options or type \'exit\' to leave"); + println!("Please input any of these options or type \'exit\' to leave\n"); loop { - result = 0; + let mut option = String::new(); - io::stdin() - .read_line(&mut option) - .expect("Failed to read input"); + io::stdin().read_line(&mut option)?; - let option: &str = option.trim_end(); // to get rid of '\n' - - match option { + match option.trim_end() { "usage" => show_usage(), - "generate" => result = generate_file(&mut client, None), - "convert" => result = convert_folders(&mut client, None), - "import" => result = import_file(&mut client, None), - "add_url" => result = add_url(&mut client, None), - "add_custom_name" => result = add_artist_with_custom_name(&mut client, None), - "exit" => break, + "generate" => generate_file(client, None)?, + "convert" => convert_folders(client, None)?, + "import" => import_file(client, None)?, + "add_url" => add_url(client, None)?, + "add_custom_name" => add_artist_with_custom_name(client, None)?, + "exit" => { + return Ok(()); + } _ => { eprintln!("Invalid option!"); } - } + }; - if result != 0 { - return result; - } + println!("Done!"); } - - result } -fn run_with_input(arguments: Vec) -> i32 { - let mut result: i32 = 0; - +fn run_with_input( + client: &mut postgres::Client, + arguments: Vec, +) -> Result<(), Box> { if arguments[1] == "usage" { show_usage(); - return result; + return Ok(()); } - - let mut client = Client::connect("host=192.168.0.10 port=28945 user=hentai password=h99nqaNPhpfbuuhCDwQXLpZAnoVTjSQP7taoqmQhpzc2rPLVC4JUAKxAHfuuhuU9", NoTls).unwrap(); let value: String = arguments[2].clone(); match arguments[1].as_str() { - "generate" => result = generate_file(&mut client, Option::Some(value)), - "convert" => result = convert_folders(&mut client, Option::Some(value)), - "import" => result = import_file(&mut client, Option::Some(value)), - "add_url" => result = add_url(&mut client, Option::Some(value)), + "generate" => generate_file(client, Option::Some(value))?, + "convert" => convert_folders(client, Option::Some(value))?, + "import" => import_file(client, Option::Some(value))?, + "add_url" => add_url(client, Option::Some(value))?, "add_custom_name" => { let values: Vec = vec![arguments[2].clone(), arguments[3].clone()]; - result = add_artist_with_custom_name(&mut client, Option::Some(values)); + add_artist_with_custom_name(client, Option::Some(values))? } _ => { - eprintln!("Invalid argument"); - result = 1; + return Err(Box::new(io::Error::new( + io::ErrorKind::InvalidInput, + "Invalid argument", + ))); } - } + }; - result + println!("Done!"); + + Ok(()) } -fn generate_file(client: &mut postgres::Client, website: Option) -> i32 { - let mut website_name: String = String::new(); +fn generate_file( + client: &mut postgres::Client, + website: Option, +) -> Result<(), Box> { + let mut website_name = String::new(); match website { Some(val) => website_name = val, None => { - io::stdin() - .read_line(&mut website_name) - .expect("Failed to read input"); + println!( + "Please input the desired website name. Options currently are (Rule34, Iwara)" + ); + io::stdin().read_line(&mut website_name)?; } }; - let output = client - .query_one("SELECT website_exists($1);", &[&website_name]) - .unwrap(); + let website_name = website_name.trim(); + + let output = client.query_one("SELECT website_exists($1);", &[&website_name])?; let found: bool = output.get(0); if !found { - eprintln!("Invalid website_name"); - return 1; + return Err(Box::new(io::Error::new( + io::ErrorKind::InvalidInput, + "Invalid website_name", + ))); } let filename = format!("{website_name}_generated.txt"); - let mut file = match File::create_new(filename) { - Ok(file) => file, - Err(error) => { - let message = error.to_string(); - eprintln!("Problem creating the file: {message}"); - return 1; - } - }; + let mut file = File::create_new(filename)?; - let output = client - .query("SELECT * FROM get_urls_and_paths($1);", &[&website_name]) - .unwrap(); + let output = client.query("SELECT * FROM get_urls_and_paths($1);", &[&website_name])?; for row in output { let url: String = row.get(0); let folder_path: String = row.get(1); - writeln!(file, "{} # {}", url, folder_path).unwrap(); + writeln!(file, "{} # {}", url, folder_path)?; } - 0 + Ok(()) } -fn convert_folders(client: &mut postgres::Client, path: Option) -> i32 { - let mut root_folder: String = String::new(); +fn convert_folders( + client: &mut postgres::Client, + path: Option, +) -> Result<(), Box> { + let mut root_folder = String::new(); match path { Some(val) => root_folder = val, None => { - io::stdin() - .read_line(&mut root_folder) - .expect("Failed to read input"); + println!("Please input the desired path to where your files live"); + io::stdin().read_line(&mut root_folder)?; } }; - let filename: &str = "convert_folders.log"; - let mut log_file = match File::create_new(filename) { - Ok(file) => file, - Err(error) => { - let message = error.to_string(); - eprintln!("Problem creating the file: {message}"); - return 1; - } - }; + let root_folder = root_folder.trim(); - let output = client - .query("SELECT * FROM get_all_convertable_paths();", &[]) - .unwrap(); + let filename = "convert_folders.log"; + let mut log_file = File::create_new(filename)?; + + let output = client.query("SELECT * FROM get_all_convertable_paths();", &[])?; for row in output { let old_path: String = row.get(0); let new_path: String = row.get(1); + let old_path = format!("{}/{}", root_folder, old_path); let new_path = format!("{}/{}", root_folder, new_path); - let old_path = urlencoding::decode(&old_path).unwrap().to_string(); + let old_path = urlencoding::decode(&old_path)?.to_string(); dbg!(&old_path); dbg!(&new_path); - if !fs::exists(&old_path).unwrap() { - continue; + if fs::exists(&old_path)? { + fs::rename(&old_path, &new_path)?; + writeln!(log_file, "Converted {} => {}", old_path, new_path)?; } - - fs::rename(&old_path, &new_path).unwrap(); - writeln!(log_file, "Converted {} => {}", old_path, new_path).unwrap(); } - 0 + Ok(()) } -fn import_file(client: &mut postgres::Client, file: Option) -> i32 { - let mut filename: String = String::new(); +fn import_file(client: &mut postgres::Client, file: Option) -> Result<(), Box> { + let mut filename = String::new(); match file { Some(val) => filename = val, None => { - io::stdin() - .read_line(&mut filename) - .expect("Failed to read input"); + println!("Please input the name of the desired file"); + io::stdin().read_line(&mut filename)?; } }; - let input_file = File::open(filename).unwrap(); + let filename = filename.trim(); + + let input_file = File::open(filename)?; let reader_iter = BufReader::new(input_file).lines(); if File::open("Fucked.txt").is_ok() { if File::open("Fucked.txt.bak").is_ok() { - fs::remove_file("Fucked.txt.bak").unwrap(); + fs::remove_file("Fucked.txt.bak")?; } - fs::rename("Fucked.txt", "Fucked.txt.bak").unwrap(); + fs::rename("Fucked.txt", "Fucked.txt.bak")?; } - let mut fucked_file = match File::create_new("Fucked.txt") { - Ok(file) => file, - Err(err) => { - eprintln!("Problem creating the file: {}", err); - return 1; - } - }; - writeln!(fucked_file, "Them are hella fucked").unwrap(); + let mut fucked_file = File::create_new("Fucked.txt")?; + writeln!(fucked_file, "Them are hella fucked")?; for line in reader_iter { - let line = line.unwrap(); + let line = line?; + if line.contains("#") { - writeln!(fucked_file, "{}", line).unwrap(); + writeln!(fucked_file, "{}", line)?; continue; } - match client.query_one("SELECT insert_url($1);", &[&line]) { - Ok(_) => (), - Err(_) => { - writeln!(fucked_file, "{}", line).unwrap(); - } - }; + client.query_one("SELECT insert_url($1);", &[&line])?; } - 0 + Ok(()) } -fn add_url(client: &mut postgres::Client, uri: Option) -> i32 { - let mut url: String = String::new(); +fn add_url(client: &mut postgres::Client, uri: Option) -> Result<(), Box> { + let mut url = String::new(); match uri { Some(val) => url = val, None => { - io::stdin() - .read_line(&mut url) - .expect("Failed to read input"); + println!("Please input the desired url"); + io::stdin().read_line(&mut url)?; } }; - let url: &str = url.trim(); + let url = url.trim(); - let output = match client.query_one("SELECT insert_url($1);", &[&url]) { - Ok(val) => val, - Err(val) => { - eprintln!("{}", val); - return 1; - } - }; + let output = client.query_one("SELECT insert_url($1);", &[&url])?; let something: String = output.get(0); - println!("{}", something); + println!("{something}"); - 0 + Ok(()) } fn add_artist_with_custom_name( client: &mut postgres::Client, uri_and_artist: Option>, -) -> i32 { +) -> Result<(), Box> { let mut url: String = String::new(); let mut artist_name: String = String::new(); @@ -268,47 +237,33 @@ fn add_artist_with_custom_name( artist_name = vec[1].clone(); } None => { - io::stdin() - .read_line(&mut url) - .expect("Failed to read input"); - - io::stdin() - .read_line(&mut artist_name) - .expect("Failed to read input"); + io::stdin().read_line(&mut url)?; + io::stdin().read_line(&mut artist_name)?; } }; let url: &str = url.trim(); let artist_name: &str = artist_name.trim(); - let output = match client.query_one( + let output = client.query_one( "SELECT insert_url_with_custom_artist($1, $2);", &[&url, &artist_name], - ) { - Ok(val) => val, - Err(val) => { - eprintln!("{}", val); - return 1; - } - }; + )?; let something: String = output.get(0); - println!("{}", something); + println!("{something}"); - 0 + Ok(()) } -fn main() -> Result<(), i32> { +fn main() -> Result<(), Box> { let args: Vec = args().collect(); - let result = if args.len() == 1 { - run_as_cli() - } else { - run_with_input(args) - }; + let mut client = Client::connect("host=192.168.0.10 port=28945 user=hentai password=h99nqaNPhpfbuuhCDwQXLpZAnoVTjSQP7taoqmQhpzc2rPLVC4JUAKxAHfuuhuU9", NoTls)?; - if result != 0 { - return Err(result); + match args.len() { + 1 => run_as_cli(&mut client)?, + _ => run_with_input(&mut client, args)?, } Ok(())