updates, clippy, formatting
[Mitarbeiter/Tim-Zeitz/stud-rust-base.git] / src / bin / decode_vector.rs
1 use std::{env, error::Error};
2 use stud_rust_base::{cli::CliErr, io::*};
3
4 fn main() -> Result<(), Box<dyn Error>> {
5     let mut args = env::args().skip(1);
6     match (args.next(), args.next()) {
7         (Some(data_type), Some(ref input)) => match data_type.as_ref() {
8             "i8" | "int8" => print_values(Vec::<i8>::load_from(input)?),
9             "u8" | "uint8" => print_values(Vec::<u8>::load_from(input)?),
10             "i16" | "int16" => print_values(Vec::<i16>::load_from(input)?),
11             "u16" | "uint16" => print_values(Vec::<u16>::load_from(input)?),
12             "i32" | "int32" => print_values(Vec::<i32>::load_from(input)?),
13             "u32" | "uint32" => print_values(Vec::<u32>::load_from(input)?),
14             "i64" | "int64" => print_values(Vec::<i64>::load_from(input)?),
15             "u64" | "uint64" => print_values(Vec::<u64>::load_from(input)?),
16             "f32" | "float32" => print_values(Vec::<f32>::load_from(input)?),
17             "f64" | "float64" => print_values(Vec::<f64>::load_from(input)?),
18             _ => {
19                 print_usage();
20                 return Err(Box::new(CliErr("Invalid data type")));
21             }
22         },
23         _ => {
24             print_usage();
25             return Err(Box::new(CliErr("Invalid arguments")));
26         }
27     };
28     Ok(())
29 }
30
31 fn print_usage() {
32     eprintln!(
33         "Usage: decode_vector data_type input_vector_file
34
35 Reads binary data from input_vector_file and writes the data to the standard output. data_type can be one of
36 * i8
37 * u8
38 * i16
39 * u16
40 * i32
41 * u32
42 * i64
43 * u64
44 * f32
45 * f64
46
47 "
48     );
49 }
50
51 use std::fmt::Display;
52
53 fn print_values<T>(values: Vec<T>)
54 where
55     T: Display,
56 {
57     for v in values {
58         println!("{}", v);
59     }
60 }