soroban_cli/commands/tx/
decode.rs

1use clap::ValueEnum;
2use std::ffi::OsString;
3use stellar_xdr::{cli::Channel, curr::TypeVariant};
4
5#[derive(thiserror::Error, Debug)]
6pub enum Error {
7    #[error(transparent)]
8    Cli(#[from] stellar_xdr::cli::decode::Error),
9}
10
11/// Decode a transaction envelope from XDR to JSON
12#[derive(Debug, clap::Parser, Clone, Default)]
13pub struct Cmd {
14    /// XDR or files containing XDR to decode, or stdin if empty
15    #[arg()]
16    pub input: Vec<OsString>,
17    // Input format
18    #[arg(long = "input", value_enum, default_value_t)]
19    pub input_format: InputFormat,
20    // Output format
21    #[arg(long = "output", value_enum, default_value_t)]
22    pub output_format: OutputFormat,
23}
24
25#[derive(Default, Clone, Copy, Debug, Eq, Hash, PartialEq, ValueEnum)]
26pub enum InputFormat {
27    #[default]
28    SingleBase64,
29    Single,
30}
31
32impl From<InputFormat> for stellar_xdr::cli::decode::InputFormat {
33    fn from(v: InputFormat) -> Self {
34        match v {
35            InputFormat::SingleBase64 => Self::SingleBase64,
36            InputFormat::Single => Self::Single,
37        }
38    }
39}
40
41#[derive(Default, Clone, Copy, Debug, Eq, Hash, PartialEq, ValueEnum)]
42pub enum OutputFormat {
43    #[default]
44    Json,
45    JsonFormatted,
46}
47
48impl From<OutputFormat> for stellar_xdr::cli::decode::OutputFormat {
49    fn from(v: OutputFormat) -> Self {
50        match v {
51            OutputFormat::Json => Self::Json,
52            OutputFormat::JsonFormatted => Self::JsonFormatted,
53        }
54    }
55}
56
57impl Cmd {
58    pub fn run(&self) -> Result<(), Error> {
59        let cmd = stellar_xdr::cli::decode::Cmd {
60            input: self.input.clone(),
61            r#type: TypeVariant::TransactionEnvelope.name().to_string(),
62            input_format: self.input_format.into(),
63            output_format: self.output_format.into(),
64        };
65        cmd.run(&Channel::Curr)?;
66        Ok(())
67    }
68}