add basic return values
This commit is contained in:
92
src/main.rs
92
src/main.rs
@ -16,7 +16,8 @@ fn show_usage() {
|
|||||||
println!("add_custom_name");
|
println!("add_custom_name");
|
||||||
}
|
}
|
||||||
|
|
||||||
fn run_as_cli() {
|
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 client = Client::connect("host=192.168.0.10 port=28945 user=hentai password=h99nqaNPhpfbuuhCDwQXLpZAnoVTjSQP7taoqmQhpzc2rPLVC4JUAKxAHfuuhuU9", NoTls).unwrap();
|
||||||
let mut option: String = String::new();
|
let mut option: String = String::new();
|
||||||
|
|
||||||
@ -24,6 +25,8 @@ fn run_as_cli() {
|
|||||||
println!("Please input any of these options or type \'exit\' to leave");
|
println!("Please input any of these options or type \'exit\' to leave");
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
|
result = 0;
|
||||||
|
|
||||||
io::stdin()
|
io::stdin()
|
||||||
.read_line(&mut option)
|
.read_line(&mut option)
|
||||||
.expect("Failed to read input");
|
.expect("Failed to read input");
|
||||||
@ -32,42 +35,55 @@ fn run_as_cli() {
|
|||||||
|
|
||||||
match option {
|
match option {
|
||||||
"usage" => show_usage(),
|
"usage" => show_usage(),
|
||||||
"generate" => generate_file(&mut client, None),
|
"generate" => result = generate_file(&mut client, None),
|
||||||
"convert" => convert_folders(&mut client, None),
|
"convert" => result = convert_folders(&mut client, None),
|
||||||
"import" => import_file(&mut client, None),
|
"import" => result = import_file(&mut client, None),
|
||||||
"add_url" => add_url(&mut client, None),
|
"add_url" => result = add_url(&mut client, None),
|
||||||
"add_custom_name" => add_artist_with_custom_name(&mut client, None),
|
"add_custom_name" => result = add_artist_with_custom_name(&mut client, None),
|
||||||
"exit" => break,
|
"exit" => break,
|
||||||
_ => {
|
_ => {
|
||||||
eprintln!("Invalid option!");
|
eprintln!("Invalid option!");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if result != 0 {
|
||||||
|
return result;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn run_with_input(arguments: Vec<String>) {
|
fn run_with_input(arguments: Vec<String>) -> i32 {
|
||||||
|
let mut result: i32 = 0;
|
||||||
|
|
||||||
if arguments[1] == "usage" {
|
if arguments[1] == "usage" {
|
||||||
show_usage();
|
show_usage();
|
||||||
return;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut client = Client::connect("host=192.168.0.10 port=28945 user=hentai password=h99nqaNPhpfbuuhCDwQXLpZAnoVTjSQP7taoqmQhpzc2rPLVC4JUAKxAHfuuhuU9", NoTls).unwrap();
|
let mut client = Client::connect("host=192.168.0.10 port=28945 user=hentai password=h99nqaNPhpfbuuhCDwQXLpZAnoVTjSQP7taoqmQhpzc2rPLVC4JUAKxAHfuuhuU9", NoTls).unwrap();
|
||||||
let value: String = arguments[2].clone();
|
let value: String = arguments[2].clone();
|
||||||
|
|
||||||
match arguments[1].as_str() {
|
match arguments[1].as_str() {
|
||||||
"generate" => generate_file(&mut client, Option::Some(value)),
|
"generate" => result = generate_file(&mut client, Option::Some(value)),
|
||||||
"convert" => convert_folders(&mut client, Option::Some(value)),
|
"convert" => result = convert_folders(&mut client, Option::Some(value)),
|
||||||
"import" => import_file(&mut client, Option::Some(value)),
|
"import" => result = import_file(&mut client, Option::Some(value)),
|
||||||
"add_url" => add_url(&mut client, Option::Some(value)),
|
"add_url" => result = add_url(&mut client, Option::Some(value)),
|
||||||
"add_custom_name" => {
|
"add_custom_name" => {
|
||||||
let values: Vec<String> = vec![arguments[2].clone(), arguments[3].clone()];
|
let values: Vec<String> = vec![arguments[2].clone(), arguments[3].clone()];
|
||||||
add_artist_with_custom_name(&mut client, Option::Some(values))
|
result = add_artist_with_custom_name(&mut client, Option::Some(values));
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
eprintln!("Invalid argument");
|
||||||
|
result = 1;
|
||||||
}
|
}
|
||||||
_ => eprintln!("Invalid argument"),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn generate_file(client: &mut postgres::Client, website: Option<String>) {
|
fn generate_file(client: &mut postgres::Client, website: Option<String>) -> i32 {
|
||||||
let mut website_name: String = String::new();
|
let mut website_name: String = String::new();
|
||||||
|
|
||||||
match website {
|
match website {
|
||||||
@ -86,7 +102,7 @@ fn generate_file(client: &mut postgres::Client, website: Option<String>) {
|
|||||||
let result: bool = output.get(0);
|
let result: bool = output.get(0);
|
||||||
if result == false {
|
if result == false {
|
||||||
eprintln!("Invalid website_name");
|
eprintln!("Invalid website_name");
|
||||||
return;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
let filename = format!("{website_name}_generated.txt");
|
let filename = format!("{website_name}_generated.txt");
|
||||||
@ -95,7 +111,7 @@ fn generate_file(client: &mut postgres::Client, website: Option<String>) {
|
|||||||
Err(error) => {
|
Err(error) => {
|
||||||
let message = error.to_string();
|
let message = error.to_string();
|
||||||
eprintln!("Problem creating the file: {message}");
|
eprintln!("Problem creating the file: {message}");
|
||||||
return;
|
return 1;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -109,9 +125,11 @@ fn generate_file(client: &mut postgres::Client, website: Option<String>) {
|
|||||||
|
|
||||||
writeln!(file, "{} # {}", url, folder_path).unwrap();
|
writeln!(file, "{} # {}", url, folder_path).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn convert_folders(client: &mut postgres::Client, path: Option<String>) {
|
fn convert_folders(client: &mut postgres::Client, path: Option<String>) -> i32 {
|
||||||
let mut root_folder: String = String::new();
|
let mut root_folder: String = String::new();
|
||||||
|
|
||||||
match path {
|
match path {
|
||||||
@ -129,7 +147,7 @@ fn convert_folders(client: &mut postgres::Client, path: Option<String>) {
|
|||||||
Err(error) => {
|
Err(error) => {
|
||||||
let message = error.to_string();
|
let message = error.to_string();
|
||||||
eprintln!("Problem creating the file: {message}");
|
eprintln!("Problem creating the file: {message}");
|
||||||
return;
|
return 1;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -155,9 +173,11 @@ fn convert_folders(client: &mut postgres::Client, path: Option<String>) {
|
|||||||
fs::rename(&old_path, &new_path).unwrap();
|
fs::rename(&old_path, &new_path).unwrap();
|
||||||
writeln!(log_file, "Converted {} => {}", old_path, new_path).unwrap();
|
writeln!(log_file, "Converted {} => {}", old_path, new_path).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn import_file(client: &mut postgres::Client, file: Option<String>) {
|
fn import_file(client: &mut postgres::Client, file: Option<String>) -> i32 {
|
||||||
let mut filename: String = String::new();
|
let mut filename: String = String::new();
|
||||||
|
|
||||||
match file {
|
match file {
|
||||||
@ -184,7 +204,7 @@ fn import_file(client: &mut postgres::Client, file: Option<String>) {
|
|||||||
Ok(file) => file,
|
Ok(file) => file,
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
eprintln!("Problem creating the file: {}", err);
|
eprintln!("Problem creating the file: {}", err);
|
||||||
return;
|
return 1;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
writeln!(fucked_file, "Them are hella fucked").unwrap();
|
writeln!(fucked_file, "Them are hella fucked").unwrap();
|
||||||
@ -203,9 +223,11 @@ fn import_file(client: &mut postgres::Client, file: Option<String>) {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn add_url(client: &mut postgres::Client, uri: Option<String>) {
|
fn add_url(client: &mut postgres::Client, uri: Option<String>) -> i32 {
|
||||||
let mut url: String = String::new();
|
let mut url: String = String::new();
|
||||||
|
|
||||||
match uri {
|
match uri {
|
||||||
@ -223,16 +245,21 @@ fn add_url(client: &mut postgres::Client, uri: Option<String>) {
|
|||||||
Ok(val) => val,
|
Ok(val) => val,
|
||||||
Err(val) => {
|
Err(val) => {
|
||||||
eprintln!("{}", val);
|
eprintln!("{}", val);
|
||||||
return;
|
return 1;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let something: String = output.get(0);
|
let something: String = output.get(0);
|
||||||
|
|
||||||
println!("{}", something);
|
println!("{}", something);
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn add_artist_with_custom_name(client: &mut postgres::Client, uri_and_artist: Option<Vec<String>>) {
|
fn add_artist_with_custom_name(
|
||||||
|
client: &mut postgres::Client,
|
||||||
|
uri_and_artist: Option<Vec<String>>,
|
||||||
|
) -> i32 {
|
||||||
let mut url: String = String::new();
|
let mut url: String = String::new();
|
||||||
let mut artist_name: String = String::new();
|
let mut artist_name: String = String::new();
|
||||||
|
|
||||||
@ -262,21 +289,30 @@ fn add_artist_with_custom_name(client: &mut postgres::Client, uri_and_artist: Op
|
|||||||
Ok(val) => val,
|
Ok(val) => val,
|
||||||
Err(val) => {
|
Err(val) => {
|
||||||
eprintln!("{}", val);
|
eprintln!("{}", val);
|
||||||
return;
|
return 1;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let something: String = output.get(0);
|
let something: String = output.get(0);
|
||||||
|
|
||||||
println!("{}", something);
|
println!("{}", something);
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() -> Result<(), i32> {
|
||||||
let args: Vec<String> = args().collect();
|
let args: Vec<String> = args().collect();
|
||||||
|
let result: i32;
|
||||||
|
|
||||||
if args.len() == 1 {
|
if args.len() == 1 {
|
||||||
run_as_cli();
|
result = run_as_cli();
|
||||||
} else {
|
} else {
|
||||||
run_with_input(args);
|
result = run_with_input(args);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if result != 0 {
|
||||||
|
return Err(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user