extern crate serde;
extern crate terminal_size;
#[macro_use]
extern crate serde_derive;
pub mod disp {
use std::fmt;
pub fn print<T>(text: T) where T: fmt::Display {
println!("{}", text);
return;
}
pub fn newline() {
println!("");
return;
}
pub fn print_debug<T>(data: T) where T: fmt::Debug {
println!("{:?}", data);
return;
}
}
pub mod range {
pub fn range(x: u32, y: u32) -> Vec<u32> {
let mut iterator = x;
let mut list = Vec::new();
list.push(iterator);
while iterator != y {
iterator += 1;
list.push(iterator);
}
return list;
}
}
pub mod arg {
use std::env;
pub fn get_args() -> Vec<String> {
let args = env::args().collect();
return args;
}
}
pub mod stringutil {
use std::string::String;
pub fn get_char_pos(string: &String, char_loc: usize) -> char {
let char_vec: &Vec<char> = &string.chars().collect();
return char_vec[char_loc];
}
pub fn newline() -> String {
let newline = String::from("\n");
return newline;
}
}
pub mod appinfo {
#[derive(Serialize, Deserialize, Debug, Clone, Copy)]
pub struct AppInfo {
pub name: &'static str,
pub author: &'static str,
pub repository: &'static str,
pub crate_link: &'static str,
}
impl AppInfo {
pub fn new() -> AppInfo {
let appinfo: AppInfo = AppInfo {name: "", author: "", repository: "", crate_link: ""};
return appinfo;
}
}
}
pub mod point {
#[derive(Serialize, Deserialize, Debug, Clone, Copy)]
pub struct Point {
pub x: i32,
pub y: i32,
}
impl Point {
pub fn new() -> Point {
let point: Point = Point {x: 0, y: 0};
return point;
}
}
}
pub mod coordinate {
#[derive(Serialize, Deserialize, Debug, Clone, Copy)]
pub struct Coordinate {
pub x: i32,
pub y: i32,
pub z: i32,
}
impl Coordinate {
pub fn new() -> Coordinate {
let coordinate: Coordinate = Coordinate {x: 0, y: 0, z: 0};
return coordinate;
}
}
}
pub mod shape {
pub mod quadrilateral {
use point::Point;
#[derive(Serialize, Deserialize, Debug, Clone, Copy)]
pub struct Quadrilateral {
pub corner: [Point; 4],
}
impl Quadrilateral {
pub fn new() -> Quadrilateral {
let quadrilateral: Quadrilateral = Quadrilateral {corner: [Point {x: 0, y: 0}; 4]};
return quadrilateral;
}
}
}
pub mod hexahedron {
use coordinate::Coordinate;
#[derive(Serialize, Deserialize, Debug, Clone, Copy)]
pub struct Hexahedron {
pub corner: [Coordinate; 8],
}
impl Hexahedron {
pub fn new() -> Hexahedron {
let hexahedron: Hexahedron = Hexahedron {corner: [Coordinate {x: 0, y: 0, z: 0}; 8]};
return hexahedron;
}
}
}
}
pub mod error {
use std::process;
use ::disp::print;
use ::stringutil;
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Error {
pub errormessage: String,
pub exitcode: u8,
pub exit: bool,
}
pub fn const_error(errormessage: &'static str, exitcode: u8) -> Error {
let error = Error {errormessage: String::from(errormessage), exitcode: exitcode, exit: true};
return error;
}
pub fn const_error_noexit(errormessage: &'static str) -> Error {
let error = Error {errormessage: String::from(errormessage), exitcode: 1, exit: false};
return error;
}
pub fn error(error: Error) {
print([String::from("Error: "), error.errormessage, stringutil::newline()].concat());
if error.exit {
process::exit(error.exitcode as i32);
}
return;
}
}
pub mod argumentparser {
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Argument {
pub identifier: char,
pub description: String,
}
pub mod help {
use std::string::String;
use ::argumentparser::Argument;
use ::aesthetic::border;
use ::disp::print;
use ::stringutil;
pub fn help(usage: &'static str, arg_array: Vec<Argument>) {
let mut help: Vec<String> = Vec::new();
help.push([border::horizontal(String::from("-")), stringutil::newline()].concat());
help.push([String::from("Usage:\n "), String::from(usage), stringutil::newline()].concat());
help.push(String::from("Flags:"));
for argument in arg_array {
help.push([String::from(" -"), argument.identifier.to_string(), String::from(" # "), argument.description].concat());
}
help.push(stringutil::newline());
for line in &help {
print(&line);
}
return;
}
}
pub mod parse {
use std::collections::HashMap;
use ::argumentparser::Argument;
pub fn parseargs(argstring: String, valid_arg_array: Vec<Argument>) -> HashMap<String, bool> {
let mut arg_bool_list = HashMap::new();
for validarg in &valid_arg_array {
arg_bool_list.insert(validarg.identifier.to_string(), false);
}
for arg in argstring.chars() {
for validarg in &valid_arg_array {
if arg == validarg.identifier {
arg_bool_list.remove(&validarg.identifier.to_string());
arg_bool_list.insert(validarg.identifier.to_string(), true);
}
}
}
return arg_bool_list;
}
}
}
pub mod aesthetic {
pub mod border {
use std::iter;
use terminal_size::terminal_size;
pub fn horizontal(border_char: String) -> String {
let term_size = terminal_size().unwrap();
let term_width: usize = (term_size.0).0 as usize;
let border = iter::repeat(border_char).take(term_width).collect::<String>();
return border;
}
}
}
pub mod pstring {
use std::vec::Vec;
use std::string::String;
use std::str;
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct PString {
codepoints: Vec<u8>,
}
impl PString {
pub fn new() -> PString {
let returnvalue: PString = PString { codepoints: vec![] };
return returnvalue;
}
pub fn set(codepoints: Vec<u8>) -> PString {
let returnvalue: PString = PString { codepoints: codepoints };
return returnvalue;
}
pub fn set_existing(mut self: &mut Self, codepoints: Vec<u8>) {
self.codepoints = codepoints;
}
pub fn from_str(string: &'static str) -> PString {
let mut returnvalue: PString = PString::new();
for character in string.chars() {
let codepoint: u8 = character as u8;
returnvalue.codepoints.push(codepoint);
}
return returnvalue;
}
pub fn to_string(self: &Self) -> String {
let returnvalue: String = str::from_utf8(&self.codepoints[..]).unwrap().to_string();
return returnvalue;
}
pub fn get(self: &Self) -> Vec<u8> {
let returnvalue = self.codepoints.clone();
return returnvalue;
}
}
}
#[cfg(test)]
mod tests {
use ::range;
use ::error;
use ::aesthetic::border;
use ::argumentparser;
use ::argumentparser::Argument;
use ::pstring::PString;
#[test]
fn range() {
let testvec = range::range(0, 4);
println!("{:?}", testvec);
}
#[test]
fn error() {
let error = error::const_error_noexit("This is a sample error, beep beep.");
error::error(error);
}
#[test]
fn bordertest() {
let border = border::horizontal(String::from("-"));
println!("{}", border);
}
#[test]
fn arghelptest() {
let usage = "[PROGRAM NAME] -[CATEGORIES AND SWITCHES]";
let arg1: Argument = Argument { identifier: 'v', description: String::from("Prints the program version") };
let arg2: Argument = Argument { identifier: 't', description: String::from("Prints \"Rust is the best language\"") };
let mut arg_array: Vec<Argument> = Vec::new();
arg_array.push(arg1);
arg_array.push(arg2);
argumentparser::help::help(usage, arg_array);
}
#[test]
fn argparser() {
let args = String::from("-vt");
let arg1: Argument = Argument { identifier: 'v', description: String::from("Prints the program version") };
let arg2: Argument = Argument { identifier: 't', description: String::from("Prints \"Rust is the best language\"") };
let mut arg_array: Vec<Argument> = Vec::new();
arg_array.push(arg1);
arg_array.push(arg2);
let ev = argumentparser::parse::parseargs(args, arg_array);
println!("{:?}", ev);
}
#[test]
fn pstring() {
let teststr = "hmm";
let mut pstr = PString::from_str(teststr);
for codepoint in pstr.get() {
println!("{}", codepoint.to_string());
}
println!("{}", pstr.to_string());
pstr.set_existing(vec![110]);
println!("{}", pstr.to_string());
}
}