1 use std::{env, error::Error};
2 use stud_rust_base::{cli::CliErr, io::*};
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 output)) => {
8 match data_type.as_ref() {
9 "i8" | "int8" => parse_input::<i8>()?.write_to(output)?,
10 "u8" | "uint8" => parse_input::<u8>()?.write_to(output)?,
11 "i16" | "int16" => parse_input::<i16>()?.write_to(output)?,
12 "u16" | "uint16" => parse_input::<u16>()?.write_to(output)?,
13 "i32" | "int32" => parse_input::<i32>()?.write_to(output)?,
14 "u32" | "uint32" => parse_input::<u32>()?.write_to(output)?,
15 "i64" | "int64" => parse_input::<i64>()?.write_to(output)?,
16 "u64" | "uint64" => parse_input::<u64>()?.write_to(output)?,
17 "f32" | "float32" => parse_input::<f32>()?.write_to(output)?,
18 "f64" | "float64" => parse_input::<f64>()?.write_to(output)?,
21 return Err(Box::new(CliErr("Invalid data type")));
28 Err(Box::new(CliErr("Invalid arguments")))
34 eprintln!("Usage: encode_vector data_type output_vector_file
36 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
51 use std::str::FromStr;
53 fn parse_input<T>() -> Result<Vec<T>, Box<dyn Error>>
56 <T as FromStr>::Err: Error + 'static,
58 use std::io::{stdin, BufRead};
60 let mut values = Vec::new();
63 for line in stdin.lock().lines() {
64 values.push(line.unwrap().parse::<T>()?)