dont panic, rather return errors in main
authorTim "S.D.Eagle" Zeitz <dev.tim.zeitz@gmail.com>
Wed, 17 Oct 2018 11:40:42 +0000 (13:40 +0200)
committerTim "S.D.Eagle" Zeitz <dev.tim.zeitz@gmail.com>
Wed, 17 Oct 2018 11:40:42 +0000 (13:40 +0200)
src/bin/compare_vector.rs
src/bin/decode_vector.rs
src/bin/encode_vector.rs
src/cli.rs [new file with mode: 0644]
src/lib.rs

index 8e852ac..760496c 100644 (file)
@@ -1,43 +1,43 @@
 extern crate stud_rust_base;
-use stud_rust_base::io::*;
-use std::env;
+use stud_rust_base::{io::*, cli::CliErr};
+use std::{env, fmt::Display, error::Error};
 
-fn main() {
+fn main() -> Result<(), Box<dyn Error>> {
     let mut args = env::args();
     args.next();
 
     match &args.collect::<Vec<String>>()[..] {
         [data_type, input1, input2] => {
             match data_type.as_ref() {
-                "i8" => compare_values(&Vec::<i8>::load_from(input1).expect("Failed to read from input1"), &Vec::<i8>::load_from(input2).expect("Failed to read from input2")),
-                "u8" => compare_values(&Vec::<u8>::load_from(input1).expect("Failed to read from input1"), &Vec::<u8>::load_from(input2).expect("Failed to read from input2")),
-                "i16" => compare_values(&Vec::<i16>::load_from(input1).expect("Failed to read from input1"), &Vec::<i16>::load_from(input2).expect("Failed to read from input2")),
-                "u16" => compare_values(&Vec::<u16>::load_from(input1).expect("Failed to read from input1"), &Vec::<u16>::load_from(input2).expect("Failed to read from input2")),
-                "i32" => compare_values(&Vec::<i32>::load_from(input1).expect("Failed to read from input1"), &Vec::<i32>::load_from(input2).expect("Failed to read from input2")),
-                "u32" => compare_values(&Vec::<u32>::load_from(input1).expect("Failed to read from input1"), &Vec::<u32>::load_from(input2).expect("Failed to read from input2")),
-                "i64" => compare_values(&Vec::<i64>::load_from(input1).expect("Failed to read from input1"), &Vec::<i64>::load_from(input2).expect("Failed to read from input2")),
-                "u64" => compare_values(&Vec::<u64>::load_from(input1).expect("Failed to read from input1"), &Vec::<u64>::load_from(input2).expect("Failed to read from input2")),
-                "f32" => compare_values(&Vec::<f32>::load_from(input1).expect("Failed to read from input1"), &Vec::<f32>::load_from(input2).expect("Failed to read from input2")),
-                "f64" => compare_values(&Vec::<f64>::load_from(input1).expect("Failed to read from input1"), &Vec::<f64>::load_from(input2).expect("Failed to read from input2")),
-                "int8" => compare_values(&Vec::<i8>::load_from(input1).expect("Failed to read from input1"), &Vec::<i8>::load_from(input2).expect("Failed to read from input2")),
-                "uint8" => compare_values(&Vec::<u8>::load_from(input1).expect("Failed to read from input1"), &Vec::<u8>::load_from(input2).expect("Failed to read from input2")),
-                "int16" => compare_values(&Vec::<i16>::load_from(input1).expect("Failed to read from input1"), &Vec::<i16>::load_from(input2).expect("Failed to read from input2")),
-                "uint16" => compare_values(&Vec::<u16>::load_from(input1).expect("Failed to read from input1"), &Vec::<u16>::load_from(input2).expect("Failed to read from input2")),
-                "int32" => compare_values(&Vec::<i32>::load_from(input1).expect("Failed to read from input1"), &Vec::<i32>::load_from(input2).expect("Failed to read from input2")),
-                "uint32" => compare_values(&Vec::<u32>::load_from(input1).expect("Failed to read from input1"), &Vec::<u32>::load_from(input2).expect("Failed to read from input2")),
-                "int64" => compare_values(&Vec::<i64>::load_from(input1).expect("Failed to read from input1"), &Vec::<i64>::load_from(input2).expect("Failed to read from input2")),
-                "uint64" => compare_values(&Vec::<u64>::load_from(input1).expect("Failed to read from input1"), &Vec::<u64>::load_from(input2).expect("Failed to read from input2")),
-                "float32" => compare_values(&Vec::<f32>::load_from(input1).expect("Failed to read from input1"), &Vec::<f32>::load_from(input2).expect("Failed to read from input2")),
-                "float64" => compare_values(&Vec::<f64>::load_from(input1).expect("Failed to read from input1"), &Vec::<f64>::load_from(input2).expect("Failed to read from input2")),
+                "i8" => { compare_values(&Vec::<i8>::load_from(input1)?, &Vec::<i8>::load_from(input2)?); Ok(()) },
+                "u8" => { compare_values(&Vec::<u8>::load_from(input1)?, &Vec::<u8>::load_from(input2)?); Ok(()) },
+                "i16" => { compare_values(&Vec::<i16>::load_from(input1)?, &Vec::<i16>::load_from(input2)?); Ok(()) },
+                "u16" => { compare_values(&Vec::<u16>::load_from(input1)?, &Vec::<u16>::load_from(input2)?); Ok(()) },
+                "i32" => { compare_values(&Vec::<i32>::load_from(input1)?, &Vec::<i32>::load_from(input2)?); Ok(()) },
+                "u32" => { compare_values(&Vec::<u32>::load_from(input1)?, &Vec::<u32>::load_from(input2)?); Ok(()) },
+                "i64" => { compare_values(&Vec::<i64>::load_from(input1)?, &Vec::<i64>::load_from(input2)?); Ok(()) },
+                "u64" => { compare_values(&Vec::<u64>::load_from(input1)?, &Vec::<u64>::load_from(input2)?); Ok(()) },
+                "f32" => { compare_values(&Vec::<f32>::load_from(input1)?, &Vec::<f32>::load_from(input2)?); Ok(()) },
+                "f64" => { compare_values(&Vec::<f64>::load_from(input1)?, &Vec::<f64>::load_from(input2)?); Ok(()) },
+                "int8" => { compare_values(&Vec::<i8>::load_from(input1)?, &Vec::<i8>::load_from(input2)?); Ok(()) },
+                "uint8" => { compare_values(&Vec::<u8>::load_from(input1)?, &Vec::<u8>::load_from(input2)?); Ok(()) },
+                "int16" => { compare_values(&Vec::<i16>::load_from(input1)?, &Vec::<i16>::load_from(input2)?); Ok(()) },
+                "uint16" => { compare_values(&Vec::<u16>::load_from(input1)?, &Vec::<u16>::load_from(input2)?); Ok(()) },
+                "int32" => { compare_values(&Vec::<i32>::load_from(input1)?, &Vec::<i32>::load_from(input2)?); Ok(()) },
+                "uint32" => { compare_values(&Vec::<u32>::load_from(input1)?, &Vec::<u32>::load_from(input2)?); Ok(()) },
+                "int64" => { compare_values(&Vec::<i64>::load_from(input1)?, &Vec::<i64>::load_from(input2)?); Ok(()) },
+                "uint64" => { compare_values(&Vec::<u64>::load_from(input1)?, &Vec::<u64>::load_from(input2)?); Ok(()) },
+                "float32" => { compare_values(&Vec::<f32>::load_from(input1)?, &Vec::<f32>::load_from(input2)?); Ok(()) },
+                "float64" => { compare_values(&Vec::<f64>::load_from(input1)?, &Vec::<f64>::load_from(input2)?); Ok(()) },
                 _ => {
                     print_usage();
-                    panic!("Unknown data_type {}", data_type);
+                    Err(Box::new(CliErr("Invalid data type")))
                 }
-            };
+            }
         },
         _ => {
             print_usage();
-            panic!("Invalid input")
+            Err(Box::new(CliErr("Invalid arguments")))
         },
     }
 }
@@ -60,8 +60,6 @@ Compares two vectors of elements in binary format. data_type can be one of
 ");
 }
 
-use std::fmt::Display;
-
 fn compare_values<T>(values1: &[T], values2: &[T]) where
     T: Display,
     T: PartialOrd
index 180804b..691a7a0 100644 (file)
@@ -1,43 +1,43 @@
 extern crate stud_rust_base;
-use stud_rust_base::io::*;
-use std::env;
+use stud_rust_base::{io::*, cli::CliErr};
+use std::{env, error::Error};
 
-fn main() {
+fn main() -> Result<(), Box<dyn Error>> {
     let mut args = env::args();
     args.next();
 
     match &args.collect::<Vec<String>>()[..] {
         [data_type, input] => {
             match data_type.as_ref() {
-                "i8" => print_values(Vec::<i8>::load_from(input).expect("Failed to read from input")),
-                "u8" => print_values(Vec::<u8>::load_from(input).expect("Failed to read from input")),
-                "i16" => print_values(Vec::<i16>::load_from(input).expect("Failed to read from input")),
-                "u16" => print_values(Vec::<u16>::load_from(input).expect("Failed to read from input")),
-                "i32" => print_values(Vec::<i32>::load_from(input).expect("Failed to read from input")),
-                "u32" => print_values(Vec::<u32>::load_from(input).expect("Failed to read from input")),
-                "i64" => print_values(Vec::<i64>::load_from(input).expect("Failed to read from input")),
-                "u64" => print_values(Vec::<u64>::load_from(input).expect("Failed to read from input")),
-                "f32" => print_values(Vec::<f32>::load_from(input).expect("Failed to read from input")),
-                "f64" => print_values(Vec::<f64>::load_from(input).expect("Failed to read from input")),
-                "int8" => print_values(Vec::<i8>::load_from(input).expect("Failed to read from input")),
-                "uint8" => print_values(Vec::<u8>::load_from(input).expect("Failed to read from input")),
-                "int16" => print_values(Vec::<i16>::load_from(input).expect("Failed to read from input")),
-                "uint16" => print_values(Vec::<u16>::load_from(input).expect("Failed to read from input")),
-                "int32" => print_values(Vec::<i32>::load_from(input).expect("Failed to read from input")),
-                "uint32" => print_values(Vec::<u32>::load_from(input).expect("Failed to read from input")),
-                "int64" => print_values(Vec::<i64>::load_from(input).expect("Failed to read from input")),
-                "uint64" => print_values(Vec::<u64>::load_from(input).expect("Failed to read from input")),
-                "float32" => print_values(Vec::<f32>::load_from(input).expect("Failed to read from input")),
-                "float64" => print_values(Vec::<f64>::load_from(input).expect("Failed to read from input")),
+                "i8" => { print_values(Vec::<i8>::load_from(input)?); Ok(()) },
+                "u8" => { print_values(Vec::<u8>::load_from(input)?); Ok(()) },
+                "i16" => { print_values(Vec::<i16>::load_from(input)?); Ok(()) },
+                "u16" => { print_values(Vec::<u16>::load_from(input)?); Ok(()) },
+                "i32" => { print_values(Vec::<i32>::load_from(input)?); Ok(()) },
+                "u32" => { print_values(Vec::<u32>::load_from(input)?); Ok(()) },
+                "i64" => { print_values(Vec::<i64>::load_from(input)?); Ok(()) },
+                "u64" => { print_values(Vec::<u64>::load_from(input)?); Ok(()) },
+                "f32" => { print_values(Vec::<f32>::load_from(input)?); Ok(()) },
+                "f64" => { print_values(Vec::<f64>::load_from(input)?); Ok(()) },
+                "int8" => { print_values(Vec::<i8>::load_from(input)?); Ok(()) },
+                "uint8" => { print_values(Vec::<u8>::load_from(input)?); Ok(()) },
+                "int16" => { print_values(Vec::<i16>::load_from(input)?); Ok(()) },
+                "uint16" => { print_values(Vec::<u16>::load_from(input)?); Ok(()) },
+                "int32" => { print_values(Vec::<i32>::load_from(input)?); Ok(()) },
+                "uint32" => { print_values(Vec::<u32>::load_from(input)?); Ok(()) },
+                "int64" => { print_values(Vec::<i64>::load_from(input)?); Ok(()) },
+                "uint64" => { print_values(Vec::<u64>::load_from(input)?); Ok(()) },
+                "float32" => { print_values(Vec::<f32>::load_from(input)?); Ok(()) },
+                "float64" => { print_values(Vec::<f64>::load_from(input)?); Ok(()) },
                 _ => {
                     print_usage();
-                    panic!("Unknown data_type {}", data_type);
+                    Err(Box::new(CliErr("Invalid data type")))
                 }
-            };
+            }
         },
         _ => {
             print_usage();
-            panic!("Invalid input")
+            Err(Box::new(CliErr("Invalid arguments")))
         },
     }
 }
index ebd1c73..c399669 100644 (file)
@@ -1,43 +1,43 @@
 extern crate stud_rust_base;
-use stud_rust_base::io::*;
-use std::env;
+use stud_rust_base::{io::*, cli::CliErr};
+use std::{env, error::Error};
 
-fn main() {
+fn main() -> Result<(), Box<dyn Error>> {
     let mut args = env::args();
     args.next();
 
     match &args.collect::<Vec<String>>()[..] {
         [data_type, output] => {
             match data_type.as_ref() {
-                "i8" => parse_input::<i8>().write_to(output).expect("Failed to write to output"),
-                "u8" => parse_input::<u8>().write_to(output).expect("Failed to write to output"),
-                "i16" => parse_input::<i16>().write_to(output).expect("Failed to write to output"),
-                "u16" => parse_input::<u16>().write_to(output).expect("Failed to write to output"),
-                "i32" => parse_input::<i32>().write_to(output).expect("Failed to write to output"),
-                "u32" => parse_input::<u32>().write_to(output).expect("Failed to write to output"),
-                "i64" => parse_input::<i64>().write_to(output).expect("Failed to write to output"),
-                "u64" => parse_input::<u64>().write_to(output).expect("Failed to write to output"),
-                "f32" => parse_input::<f32>().write_to(output).expect("Failed to write to output"),
-                "f64" => parse_input::<f64>().write_to(output).expect("Failed to write to output"),
-                "int8" => parse_input::<i8>().write_to(output).expect("Failed to write to output"),
-                "uint8" => parse_input::<u8>().write_to(output).expect("Failed to write to output"),
-                "int16" => parse_input::<i16>().write_to(output).expect("Failed to write to output"),
-                "uint16" => parse_input::<u16>().write_to(output).expect("Failed to write to output"),
-                "int32" => parse_input::<i32>().write_to(output).expect("Failed to write to output"),
-                "uint32" => parse_input::<u32>().write_to(output).expect("Failed to write to output"),
-                "int64" => parse_input::<i64>().write_to(output).expect("Failed to write to output"),
-                "uint64" => parse_input::<u64>().write_to(output).expect("Failed to write to output"),
-                "float32" => parse_input::<f32>().write_to(output).expect("Failed to write to output"),
-                "float64" => parse_input::<f64>().write_to(output).expect("Failed to write to output"),
+                "i8" => { parse_input::<i8>()?.write_to(output)?; Ok(()) },
+                "u8" => { parse_input::<u8>()?.write_to(output)?; Ok(()) },
+                "i16" => { parse_input::<i16>()?.write_to(output)?; Ok(()) },
+                "u16" => { parse_input::<u16>()?.write_to(output)?; Ok(()) },
+                "i32" => { parse_input::<i32>()?.write_to(output)?; Ok(()) },
+                "u32" => { parse_input::<u32>()?.write_to(output)?; Ok(()) },
+                "i64" => { parse_input::<i64>()?.write_to(output)?; Ok(()) },
+                "u64" => { parse_input::<u64>()?.write_to(output)?; Ok(()) },
+                "f32" => { parse_input::<f32>()?.write_to(output)?; Ok(()) },
+                "f64" => { parse_input::<f64>()?.write_to(output)?; Ok(()) },
+                "int8" => { parse_input::<i8>()?.write_to(output)?; Ok(()) },
+                "uint8" => { parse_input::<u8>()?.write_to(output)?; Ok(()) },
+                "int16" => { parse_input::<i16>()?.write_to(output)?; Ok(()) },
+                "uint16" => { parse_input::<u16>()?.write_to(output)?; Ok(()) },
+                "int32" => { parse_input::<i32>()?.write_to(output)?; Ok(()) },
+                "uint32" => { parse_input::<u32>()?.write_to(output)?; Ok(()) },
+                "int64" => { parse_input::<i64>()?.write_to(output)?; Ok(()) },
+                "uint64" => { parse_input::<u64>()?.write_to(output)?; Ok(()) },
+                "float32" => { parse_input::<f32>()?.write_to(output)?; Ok(()) },
+                "float64" => { parse_input::<f64>()?.write_to(output)?; Ok(()) },
                 _ => {
                     print_usage();
-                    panic!("Unknown data_type {}", data_type);
+                    Err(Box::new(CliErr("Invalid data type")))
                 }
-            };
+            }
         },
         _ => {
             print_usage();
-            panic!("Invalid input")
+            Err(Box::new(CliErr("Invalid arguments")))
         },
     }
 }
@@ -60,14 +60,11 @@ Reads textual data from the standard input and writes it in a binary format to o
 ");
 }
 
-use std::{
-    str::FromStr,
-    fmt::Debug,
-};
+use std::str::FromStr;
 
-fn parse_input<T>() -> Vec<T> where
+fn parse_input<T>() -> Result<Vec<T>, Box<dyn Error>> where
     T: FromStr,
-    <T as FromStr>::Err: Debug
+    <T as FromStr>::Err: Error + 'static
 {
     use std::io::{BufRead, stdin};
 
@@ -75,8 +72,8 @@ fn parse_input<T>() -> Vec<T> where
 
     let stdin = stdin();
     for line in stdin.lock().lines() {
-        values.push(line.unwrap().parse::<T>().unwrap())
+        values.push(line.unwrap().parse::<T>()?)
     }
 
-    values
+    Ok(values)
 }
diff --git a/src/cli.rs b/src/cli.rs
new file mode 100644 (file)
index 0000000..e66421d
--- /dev/null
@@ -0,0 +1,15 @@
+//! Utility module for command line interfaces
+
+use std::{fmt, fmt::Display, error::Error};
+
+/// An error struct to wrap simple static error messages
+#[derive(Debug)]
+pub struct CliErr(pub &'static str);
+
+impl Display for CliErr {
+    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+        f.write_str(self.0)
+    }
+}
+
+impl Error for CliErr {}
index 28f3f9b..85f3191 100644 (file)
@@ -2,6 +2,7 @@
 
 extern crate time as time_crate;
 
+pub mod cli;
 pub mod index_heap;
 pub mod io;
 pub mod time;