1 use std::{env, error::Error, fmt::Display};
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(), args.next()) {
7 (Some(data_type), Some(input1), Some(input2)) => {
8 match data_type.as_ref() {
9 "i8" | "int8" => compare_values::<i8>(&Vec::load_from(input1)?, &Vec::load_from(input2)?),
10 "u8" | "uint8" => compare_values::<u8>(&Vec::load_from(input1)?, &Vec::load_from(input2)?),
11 "i16" | "int16" => compare_values::<i16>(&Vec::load_from(input1)?, &Vec::load_from(input2)?),
12 "u16" | "uint16" => compare_values::<u16>(&Vec::load_from(input1)?, &Vec::load_from(input2)?),
13 "i32" | "int32" => compare_values::<i32>(&Vec::load_from(input1)?, &Vec::load_from(input2)?),
14 "u32" | "uint32" => compare_values::<u32>(&Vec::load_from(input1)?, &Vec::load_from(input2)?),
15 "i64" | "int64" => compare_values::<i64>(&Vec::load_from(input1)?, &Vec::load_from(input2)?),
16 "u64" | "uint64" => compare_values::<u64>(&Vec::load_from(input1)?, &Vec::load_from(input2)?),
17 "f32" | "float32" => compare_values::<f32>(&Vec::load_from(input1)?, &Vec::load_from(input2)?),
18 "f64" | "float64" => compare_values::<f64>(&Vec::load_from(input1)?, &Vec::load_from(input2)?),
21 return Err(Box::new(CliErr("Invalid data type")));
28 Err(Box::new(CliErr("Invalid arguments")))
35 "Usage: decode_vector data_type vector1_file vector2_file
37 Compares two vectors of elements in binary format. data_type can be one of
53 fn compare_values<T>(values1: &[T], values2: &[T])
58 if values1.len() != values2.len() {
61 "Can only compare vectors of equal size. The first vector has {} elements. The second vector has {} elements.",
68 let mut v1_smaller_count = 0;
69 let mut v2_smaller_count = 0;
70 let mut first_diff = None;
72 for (i, (v1, v2)) in values1.iter().zip(values2.iter()).enumerate() {
74 v1_smaller_count += 1;
77 v2_smaller_count += 1;
80 if first_diff.is_none() && v1 != v2 {
87 eprintln!("The vectors differ.");
88 eprintln!("{} elements are smaller in the first vector.", v1_smaller_count);
89 eprintln!("{} elements are smaller in the second vector.", v2_smaller_count);
90 eprintln!("{} elements are the same.", values1.len() - v1_smaller_count - v2_smaller_count);
91 println!("{}", values1.len() - v1_smaller_count - v2_smaller_count);
92 eprintln!("{} elements are different.", v1_smaller_count + v2_smaller_count);
93 eprintln!("The vectors have {} elements.", values1.len());
94 eprintln!("The first element that differs is at index {}.", index);
97 println!("{}", values1.len());
98 eprintln!("The vectors are the same and have {} elements.", values1.len());