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
use crate::cardano_cli::mock::command::write_to_file_or_println;
use crate::cardano_cli::mock::fake;
use clap::Parser;
use std::io::Error;
use std::path::PathBuf;

#[derive(Parser, Debug)]
pub enum Query {
    Utxo(UTxOCommand),
    Tip(TipCommand),
    ProtocolParameters(ProtocolParametersCommand),
}

impl Query {
    pub fn exec(self) -> Result<(), Error> {
        match self {
            Self::Utxo(utxo) => utxo.exec(),
            Self::Tip(tip) => tip.exec(),
            Self::ProtocolParameters(protocol) => protocol.exec(),
        }
    }
}

#[derive(Parser, Debug)]
pub struct ProtocolParametersCommand {
    #[clap(long = "testnet-magic")]
    pub testnet_magic: Option<u32>,

    #[clap(long = "mainnet")]
    pub mainnet: bool,

    #[clap(long = "out-file")]
    pub out_file: Option<PathBuf>,
}

impl ProtocolParametersCommand {
    pub fn exec(self) -> Result<(), Error> {
        assert!(
            !(self.mainnet || self.testnet_magic.is_some()),
            "no network setting"
        );

        let protocol_parameters = fake::protocol_parameters();
        write_to_file_or_println(
            self.out_file,
            &serde_json::to_string(&protocol_parameters).unwrap(),
        )
    }
}

#[derive(Parser, Debug)]
pub struct TipCommand {
    #[clap(long = "testnet-magic")]
    pub testnet_magic: Option<u32>,

    #[clap(long = "mainnet")]
    pub mainnet: bool,

    #[clap(long = "out-file")]
    pub out_file: Option<PathBuf>,
}

impl TipCommand {
    pub fn exec(self) -> Result<(), Error> {
        let tip = fake::tip();
        write_to_file_or_println(self.out_file, &serde_json::to_string(&tip).unwrap())
    }
}
#[derive(Parser, Debug)]
pub struct UTxOCommand {
    #[clap(long = "address")]
    pub address: String,

    #[clap(long = "testnet-magic")]
    pub testnet_magic: Option<u32>,

    #[clap(long = "mainnet")]
    pub mainnet: bool,

    #[clap(long = "out-file")]
    pub out_file: Option<PathBuf>,
}

impl UTxOCommand {
    pub fn exec(self) -> Result<(), Error> {
        let utxos = fake::utxo();
        write_to_file_or_println(self.out_file, &utxos.to_string())
    }
}