add a lot of docs and a reference to them in the README
[Mitarbeiter/Tim-Zeitz/stud-rust-base.git] / src / bin / encode_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, output] => {
11             match data_type.as_ref() {
12                 "i8" => parse_input::<i8>().write_to(output).expect("Failed to write to output"),
13                 "u8" => parse_input::<u8>().write_to(output).expect("Failed to write to output"),
14                 "i16" => parse_input::<i16>().write_to(output).expect("Failed to write to output"),
15                 "u16" => parse_input::<u16>().write_to(output).expect("Failed to write to output"),
16                 "i32" => parse_input::<i32>().write_to(output).expect("Failed to write to output"),
17                 "u32" => parse_input::<u32>().write_to(output).expect("Failed to write to output"),
18                 "i64" => parse_input::<i64>().write_to(output).expect("Failed to write to output"),
19                 "u64" => parse_input::<u64>().write_to(output).expect("Failed to write to output"),
20                 "f32" => parse_input::<f32>().write_to(output).expect("Failed to write to output"),
21                 "f64" => parse_input::<f64>().write_to(output).expect("Failed to write to output"),
22                 "int8" => parse_input::<i8>().write_to(output).expect("Failed to write to output"),
23                 "uint8" => parse_input::<u8>().write_to(output).expect("Failed to write to output"),
24                 "int16" => parse_input::<i16>().write_to(output).expect("Failed to write to output"),
25                 "uint16" => parse_input::<u16>().write_to(output).expect("Failed to write to output"),
26                 "int32" => parse_input::<i32>().write_to(output).expect("Failed to write to output"),
27                 "uint32" => parse_input::<u32>().write_to(output).expect("Failed to write to output"),
28                 "int64" => parse_input::<i64>().write_to(output).expect("Failed to write to output"),
29                 "uint64" => parse_input::<u64>().write_to(output).expect("Failed to write to output"),
30                 "float32" => parse_input::<f32>().write_to(output).expect("Failed to write to output"),
31                 "float64" => parse_input::<f64>().write_to(output).expect("Failed to write to output"),
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: encode_vector data_type output_vector_file
47
48 Reads textual data from the standard input and writes it in a binary format to output_vector_file. The input data should be one data element per line. The data is only written once an end of file is encountered on the input. 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::{
64     str::FromStr,
65     fmt::Debug,
66 };
67
68 fn parse_input<T>() -> Vec<T> where
69     T: FromStr,
70     <T as FromStr>::Err: Debug
71 {
72     use std::io::{BufRead, stdin};
73
74     let mut values = Vec::new();
75
76     let stdin = stdin();
77     for line in stdin.lock().lines() {
78         values.push(line.unwrap().parse::<T>().unwrap())
79     }
80
81     values
82 }