extern crate clap;
extern crate csv;
extern crate deunicode;
extern crate rand;
use clap::{Arg, App};
use deunicode::deunicode;
use rand::prelude::*;
use regex::Regex;
use std::error::Error;
use std::process;
fn run(source: String) -> Result<String, Box<dyn Error>> {
let mut rdr = csv::Reader::from_reader(source.as_bytes());
let mut names: Vec<String> = Vec::new();
for result in rdr.records() {
let record = result?;
names.push(record[0].to_string())
}
let count = names.len();
let mut rng = rand::thread_rng();
let e: usize = rng.gen_range(0, count);
let replace_hyphens = Regex::new(r"[-]+").unwrap();
let name = &names[e]
.to_lowercase()
.replace(" ", "-")
.replace("'", "-")
.replace("/", "")
.replace("\"", "")
.replace("(", "")
.replace(")", "")
.replace("[", "")
.replace("]", "")
.replace(".", "");
let cleaned_name = replace_hyphens.replace_all(&name, "-");
let useable_name = deunicode(&cleaned_name)
.replace("[?]", "");
Ok(useable_name.to_string())
}
fn main() {
let ancient_greek = include_bytes!("ancient-greek.txt");
let ancient_greek_str = String::from_utf8_lossy(ancient_greek).to_string();
let ancient_roman = include_bytes!("ancient-roman.txt");
let ancient_roman_str = String::from_utf8_lossy(ancient_roman).to_string();
let darkwing = include_bytes!("darkwing-duck.txt");
let darkwing_str = String::from_utf8_lossy(darkwing).to_string();
let islands_japan = include_bytes!("islands-japan.txt");
let islands_japan_str = String::from_utf8_lossy(islands_japan).to_string();
let lotr = include_bytes!("lotr.txt");
let lotr_str = String::from_utf8_lossy(lotr).to_string();
let shakespeare = include_bytes!("shakespeare.txt");
let shakespeare_str = String::from_utf8_lossy(shakespeare).to_string();
let stars = include_bytes!("stars.txt");
let stars_str = String::from_utf8_lossy(stars).to_string();
let starwars = include_bytes!("star-wars.txt");
let starwars_str = String::from_utf8_lossy(starwars).to_string();
let wot = include_bytes!("wheel-of-time.txt");
let wot_str = String::from_utf8_lossy(wot).to_string();
let mut all_str = ancient_greek_str.clone();
all_str.push_str(ancient_roman_str.as_str());
all_str.push_str(darkwing_str.as_str());
all_str.push_str(islands_japan_str.as_str());
all_str.push_str(lotr_str.as_str());
all_str.push_str(shakespeare_str.as_str());
all_str.push_str(stars_str.as_str());
all_str.push_str(starwars_str.as_str());
all_str.push_str(wot_str.as_str());
let params = App::new("Generate Server Name")
.version("1.0.5")
.about("Generate a server name suggestion based on source pool and given domain.")
.arg(Arg::with_name("domain")
.help("The domain to append to the server name.")
)
.arg(Arg::with_name("source")
.help("The source for the name suggestions. Either all, agreek, aroman, darkwing, islands, lotr, shakespeare, stars, sw or wot. Alternative specification is 'ancient greek', 'ancient roman', 'darkwing duck', 'islands of japan', 'lord of the rings', 'william shakespeare', 'star names', 'starwars', 'star wars' or 'wheel of time'.")
)
.get_matches();
let domain = params.value_of("domain").unwrap_or("example.com");
let source = match params.value_of("source").unwrap_or("all").to_lowercase().as_str() {
"agreek" | "ancient greek" => ancient_greek_str,
"aroman" | "ancient roman" => ancient_roman_str,
"darkwing" | "darkwing duck" => darkwing_str,
"islands" | "islands of japan" => islands_japan_str,
"lotr" | "lord of the rings" => lotr_str,
"stars" | "star names" => stars_str,
"sw" | "starwars" | "star wars" => starwars_str,
"shakespeare" | "william shakespeare" => shakespeare_str,
"wot" | "wheel of time" => wot_str,
_ => all_str,
};
match run(source) {
Err(e) => {
println!("{}", e);
process::exit(1);
},
Ok(name) => {
let mut server = name;
server.push_str(&".");
server.push_str(&domain);
println!("{}", server);
},
}
}