#![feature(test)]
extern crate test;
extern crate terminal_size;
extern crate snap;
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 arg {
use std::env;
pub fn get_args() -> Vec<String> {
return env::args().collect();
}
}
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 {
return String::from("\n"); }
}
pub mod appinfo {
#[derive(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 {
return AppInfo {name: "", author: "", repository: "", crate_link: ""};
}
}
}
pub mod point {
#[derive(Debug, Clone, Copy)]
pub struct Point {
pub x: i32,
pub y: i32,
}
impl Point {
pub fn new() -> Point {
return Point {x: 0, y: 0};
}
}
}
pub mod coordinate {
#[derive(Debug, Clone, Copy)]
pub struct Coordinate {
pub x: i32,
pub y: i32,
pub z: i32,
}
impl Coordinate {
pub fn new() -> Coordinate {
return Coordinate {x: 0, y: 0, z: 0};
}
}
}
pub mod shape {
pub mod quadrilateral {
use point::Point;
#[derive(Debug, Clone, Copy)]
pub struct Quadrilateral {
pub corner: [Point; 4],
}
impl Quadrilateral {
pub fn new() -> Quadrilateral {
return Quadrilateral {corner: [Point {x: 0, y: 0}; 4]};
}
}
}
pub mod hexahedron {
use coordinate::Coordinate;
#[derive(Debug, Clone, Copy)]
pub struct Hexahedron {
pub corner: [Coordinate; 8],
}
impl Hexahedron {
pub fn new() -> Hexahedron {
return Hexahedron {corner: [Coordinate {x: 0, y: 0, z: 0}; 8]};
}
}
}
}
pub mod error {
use std::process;
use ::disp::print;
use ::stringutil;
#[derive(Debug, Clone)]
pub struct Error {
pub errormessage: String,
pub exitcode: i32,
pub exit: bool,
}
pub fn const_error(errormessage: String, exitcode: i32) -> Error {
return Error {errormessage: errormessage, exitcode: exitcode, exit: true};
}
pub fn const_error_noexit(errormessage: String) -> Error {
return Error {errormessage: errormessage, exitcode: 1, exit: false};
}
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(Debug, Clone)]
pub struct Argument {
pub identifier: char,
pub description: String,
}
pub mod help {
use std::string::String;
use std::{thread, time};
use ::argumentparser::Argument;
use ::aesthetic::border;
use ::disp::print;
use ::stringutil;
pub fn help(usage: &'static str, arg_array: Vec<Argument>) {
let duration = time::Duration::from_millis(25);
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);
thread::sleep(duration);
}
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;
use std::fmt;
use snap;
pub const ENCODING: &'static str = "utf-8";
const BYTEXOR: u8 = 53;
const COMPRESSIONDEFAULT: bool = false;
struct Compressor{}
impl Compressor {
#[inline(always)]
pub fn compress_bytes(bytes: Vec<u8>, compression: bool) -> Vec<u8> {
if compression {
let mut encoder = snap::Encoder::new();
return encoder.compress_vec(&bytes[..]).unwrap();
} else {
return bytes;
}
}
#[inline(always)]
pub fn decompress_bytes(bytes: Vec<u8>, compression: bool) -> Vec<u8> {
if compression {
let mut decoder = snap::Decoder::new();
return decoder.decompress_vec(&bytes[..]).unwrap();
} else {
return bytes;
}
}
}
#[derive(Debug, Clone)]
pub struct PString {
pub compression: bool,
codepoints: Vec<u8>,
}
impl fmt::Display for PString {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.clone().to_string_backend())
}
}
impl PString {
pub fn new() -> PString {
return PString { compression: COMPRESSIONDEFAULT, codepoints: Vec::new() };
}
pub fn from_bytes(codepoints: Vec<u8>) -> PString {
return PString { compression: COMPRESSIONDEFAULT, codepoints: Compressor::compress_bytes(codepoints, COMPRESSIONDEFAULT) };
}
pub fn set_bytes(self: &mut Self, codepoints: Vec<u8>) {
self.codepoints = Compressor::compress_bytes(codepoints, self.compression);
}
pub fn set_str(self: &mut Self, string: &'static str) {
let chararray: Vec<char> = string.chars().collect();
let mut newarray: Vec<u8> = Vec::new();
for character in chararray {
newarray.push(character as u8);
}
self.codepoints = Compressor::compress_bytes(newarray, self.compression);
}
pub fn set_string(self: &mut Self, string: String) {
let chararray: Vec<char> = string.chars().collect();
let mut newarray: Vec<u8> = Vec::new();
for character in chararray {
newarray.push(character as u8);
}
self.codepoints = Compressor::compress_bytes(newarray, self.compression);
}
pub fn from_str(string: &'static str) -> PString {
let mut returnvalue: PString = PString::new();
for character in string.chars() {
returnvalue.codepoints.push(character as u8);
}
returnvalue.autocompress();
return returnvalue;
}
pub fn from_string(string: String) -> PString {
let mut returnvalue: PString = PString::new();
for character in string.chars() {
returnvalue.codepoints.push(character as u8);
}
returnvalue.autocompress();
return returnvalue;
}
fn to_string_backend(self: &mut Self) -> String {
let bytes = Compressor::decompress_bytes(self.codepoints.clone(), self.compression);
return str::from_utf8(&bytes[..]).unwrap().to_string();
}
pub fn as_bytes(self: &mut Self) -> Vec<u8> {
return self.decompress_to_bytes();
}
pub fn as_bytes_raw(self: &Self) -> Vec<u8> {
return self.codepoints.clone();
}
pub fn chars(self: &Self) -> Vec<char> {
let mut chararray: Vec<char> = Vec::new();
for byte in self.decompress_to_bytes() {
chararray.push(byte as char);
}
return chararray;
}
pub fn compress(self: &mut Self) {
if self.compression == false {
let mut encoder = snap::Encoder::new();
self.codepoints = encoder.compress_vec(&self.codepoints[..]).unwrap();
self.compression = true;
}
}
pub fn decompress(self: &mut Self) {
if self.compression == true {
let mut decoder = snap::Decoder::new();
self.codepoints = decoder.decompress_vec(&self.codepoints[..]).unwrap();
self.compression = false;
}
}
#[inline(always)]
fn compress_to_bytes(self: &Self) -> Vec<u8> {
if self.compression == true {
let mut encoder = snap::Encoder::new();
return encoder.compress_vec(&self.codepoints[..]).unwrap();
}
return self.codepoints.clone();
}
#[inline(always)]
fn decompress_to_bytes(self: &Self) -> Vec<u8> {
if self.compression == true {
let mut decoder = snap::Decoder::new();
return decoder.decompress_vec(&self.codepoints[..]).unwrap();
}
return self.codepoints.clone();
}
#[inline(always)]
fn autocompress(self: &mut Self) {
if self.compression {
let mut encoder = snap::Encoder::new();
self.codepoints = encoder.compress_vec(&self.codepoints[..]).unwrap();
}
}
#[inline(always)]
fn autodecompress(self: &mut Self) {
if self.compression {
let mut decoder = snap::Decoder::new();
self.codepoints = decoder.decompress_vec(&self.codepoints[..]).unwrap();
}
}
}
}
pub mod num {
pub fn average(values: Vec<f64>) -> f64 {
let mut total: f64 = 0_f64;
for value in &values {
total += value;
}
return total / (values.len() as f64);
}
}
#[cfg(test)]
mod tests {
use ::error;
use ::aesthetic::border;
use ::argumentparser;
use ::argumentparser::Argument;
use ::pstring::PString;
use ::num;
use test::Bencher;
#[test]
fn error() {
let error = error::const_error_noexit("This is a sample error, beep beep.".to_string());
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 mut pstr = PString::from_bytes(vec![104, 104, 104, 104, 104, 104, 104, 104, 104, 104, 104, 104, 104, 104, 104, 104, 104, 104, 104, 104, 104, 104, 104, 104, 104, 104, 104, 104, 104, 104, 104, 104, 104, 104, 104, 104, 104, 104, 104, 104, 104, 104, 104, 104]);
println!("{}", "----");
println!("{:?}", pstr.compression);
pstr.compress();
println!("{:?}", pstr.as_bytes());
println!("{:?}", pstr.as_bytes_raw());
println!("{}", pstr);
}
#[test]
fn average() {
let average = num::average(vec![4_f64, 2_f64]);
println!("{:?}", average);
}
#[bench]
fn bench_pstring(b: &mut Bencher) {
let teststr = "aOYtemeapoDKOJNEzQrdfSozFSqsetcYHpM6kurYRzbnZ99AWpQoCDwuH6pvsjJZsgsMXSWYAB4XEiF6HmiRhiNuawKduBo9VSFt02Ub92tfZjKsTWON7wZQhFxapBWsf3hmD6M7BSc3Xw63zVO0tfRti0fWjluMNWgbNsmjm9ql5R9Eq1jpNUIOO9k1I2OwTrngKLA8Ieqn0U9Jith3kERmJMkjdKoGSGNueRtlqOM1e4lnYBdbnwEpy9Qbq46S";
b.iter(|| {
for _ in 1..256 {
let mut pstr = PString::from_str(teststr);
pstr.compress();
let mut mv = pstr.as_bytes();
mv.push(104);
}
});
}
}