1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
mod vectors;
use inner::*;

/// "Usage: cargo run --all-features --bin export_test_vectors -- [hex|b10] [legacy|kimchi] <OUTPUT_FILE>",
fn main() {
    inner::main();
}

mod inner {
    use super::vectors;
    use std::{
        env,
        fs::File,
        io::{self, Write},
        str::FromStr,
    };

    #[derive(Debug)]
    pub enum Mode {
        Hex,
        B10,
    }

    impl FromStr for Mode {
        type Err = ();

        fn from_str(input: &str) -> Result<Self, Self::Err> {
            match input.to_lowercase().as_str() {
                "b10" => Ok(Mode::B10),
                "hex" => Ok(Mode::Hex),
                _ => Err(()),
            }
        }
    }

    #[derive(Debug)]
    pub enum ParamType {
        Legacy,
        Kimchi,
    }

    impl FromStr for ParamType {
        type Err = ();

        fn from_str(input: &str) -> Result<Self, Self::Err> {
            match input.to_lowercase().as_str() {
                "legacy" => Ok(ParamType::Legacy),
                "kimchi" => Ok(ParamType::Kimchi),
                _ => Err(()),
            }
        }
    }

    pub(crate) fn main() {
        let args: Vec<String> = env::args().collect();
        match args.len() {
            4 => {
                // parse command-line args
                let mode: Mode = args
                    .get(1)
                    .expect("missing mode")
                    .parse()
                    .expect("invalid mode");
                let param_type: ParamType = args
                    .get(2)
                    .expect("missing param type")
                    .parse()
                    .expect("invalid param type");
                let output_file = args.get(3).expect("missing file");

                // generate vectors
                let vectors = vectors::generate(mode, param_type);

                // save to output file
                let writer: Box<dyn Write> = match output_file.as_str() {
                    "-" => Box::new(io::stdout()),
                    _ => Box::new(File::create(output_file).expect("could not create file")),
                };
                serde_json::to_writer_pretty(writer, &vectors).expect("could not write to file");
            }
            _ => {
                println!(
                "usage: cargo run -p export_test_vectors -- [{:?}|{:?}] [legacy|kimchi] <OUTPUT_FILE>",
                Mode::Hex,
                Mode::B10,
            );
            }
        }
    }
}