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
use std::collections::HashMap;
use std::path::PathBuf;
use std::fs::OpenOptions;
use std::io::Write;
use libra::prelude::*;
use clap::Clap;
use enum_iterator::IntoEnumIterator;
use anyhow::Result;
use dvm_cli::version;

/// Status table generator.
#[derive(Clap)]
#[clap(name = "status-table", version = version!())]
struct Opts {
    /// Optional path to the output file.
    /// If not passed, result will be printed to stdout.
    #[clap(short, long, parse(from_os_str))]
    output: Option<PathBuf>,
}

fn main() {
    let opts = Opts::parse();
    let status_table = status_table_json().unwrap();
    if let Some(output) = opts.output {
        let mut f = OpenOptions::new()
            .create(true)
            .write(true)
            .open(&output)
            .unwrap();
        f.set_len(0).unwrap();
        f.write_all(status_table.as_bytes()).unwrap();
    } else {
        println!("{}", status_table);
    }
}

fn status_table_json() -> Result<String> {
    Ok(serde_json::to_string_pretty(
        &StatusCode::into_enum_iter()
            .map(|code| (code as u64, format!("{:?}", code)))
            .collect::<HashMap<_, _>>(),
    )?)
}