180804b15f88e00e88cdf468cc752f9285e211c1
[Mitarbeiter/Tim-Zeitz/stud-rust-base.git] / src / bin / decode_vector.rs
1 extern crate stud_rust_base;
2 use stud_rust_base::io::*;
3 use std::env;
4
5 fn main() {
6     let mut args = env::args();
7     args.next();
8
9     match &args.collect::<Vec<String>>()[..] {
10         [data_type, input] => {
11             match data_type.as_ref() {
12                 "i8" => print_values(Vec::<i8>::load_from(input).expect("Failed to read from input")),
13                 "u8" => print_values(Vec::<u8>::load_from(input).expect("Failed to read from input")),
14                 "i16" => print_values(Vec::<i16>::load_from(input).expect("Failed to read from input")),
15                 "u16" => print_values(Vec::<u16>::load_from(input).expect("Failed to read from input")),
16                 "i32" => print_values(Vec::<i32>::load_from(input).expect("Failed to read from input")),
17                 "u32" => print_values(Vec::<u32>::load_from(input).expect("Failed to read from input")),
18                 "i64" => print_values(Vec::<i64>::load_from(input).expect("Failed to read from input")),
19                 "u64" => print_values(Vec::<u64>::load_from(input).expect("Failed to read from input")),
20                 "f32" => print_values(Vec::<f32>::load_from(input).expect("Failed to read from input")),
21                 "f64" => print_values(Vec::<f64>::load_from(input).expect("Failed to read from input")),
22                 "int8" => print_values(Vec::<i8>::load_from(input).expect("Failed to read from input")),
23                 "uint8" => print_values(Vec::<u8>::load_from(input).expect("Failed to read from input")),
24                 "int16" => print_values(Vec::<i16>::load_from(input).expect("Failed to read from input")),
25                 "uint16" => print_values(Vec::<u16>::load_from(input).expect("Failed to read from input")),
26                 "int32" => print_values(Vec::<i32>::load_from(input).expect("Failed to read from input")),
27                 "uint32" => print_values(Vec::<u32>::load_from(input).expect("Failed to read from input")),
28                 "int64" => print_values(Vec::<i64>::load_from(input).expect("Failed to read from input")),
29                 "uint64" => print_values(Vec::<u64>::load_from(input).expect("Failed to read from input")),
30                 "float32" => print_values(Vec::<f32>::load_from(input).expect("Failed to read from input")),
31                 "float64" => print_values(Vec::<f64>::load_from(input).expect("Failed to read from input")),
32                 _ => {
33                     print_usage();
34                     panic!("Unknown data_type {}", data_type);
35                 }
36             };
37         },
38         _ => {
39             print_usage();
40             panic!("Invalid input")
41         },
42     }
43 }
44
45 fn print_usage() {
46     eprintln!("Usage: decode_vector data_type input_vector_file
47
48 Reads binary data from input_vector_file and writes the data to the standard output. data_type can be one of
49 * i8
50 * u8
51 * i16
52 * u16
53 * i32
54 * u32
55 * i64
56 * u64
57 * f32
58 * f64
59
60 ");
61 }
62
63 use std::fmt::Display;
64
65 fn print_values<T>(values: Vec<T>) where
66     T: Display
67 {
68     for v in values {
69         println!("{}", v);
70     }
71 }