Skip to main content

stellar_xdr/cli/
decode.rs

1use std::ffi::OsString;
2use std::io::{stdout, Write};
3use std::{fmt::Debug, str::FromStr};
4
5use clap::{Args, ValueEnum};
6use serde::Serialize;
7
8use crate::cli::util;
9
10#[derive(thiserror::Error, Debug)]
11pub enum Error {
12    #[error("unknown type {0}, choose one of {1:?}")]
13    UnknownType(String, &'static [&'static str]),
14    #[error("error decoding XDR: {0}")]
15    ReadXdr(#[from] crate::Error),
16    #[error("error reading file: {0}")]
17    ReadFile(std::io::Error),
18    #[error("error writing output: {0}")]
19    WriteOutput(std::io::Error),
20    #[error("error generating JSON: {0}")]
21    GenerateJson(#[from] serde_json::Error),
22    #[error("type doesn't have a text representation, use 'json' as output")]
23    TextUnsupported,
24}
25
26#[derive(Args, Debug, Clone)]
27#[command()]
28pub struct Cmd {
29    /// XDR or files containing XDR to decode, or stdin if empty
30    #[arg()]
31    pub input: Vec<OsString>,
32
33    /// XDR type to decode
34    #[arg(long)]
35    pub r#type: String,
36
37    // Input format of the XDR
38    #[arg(long = "input", value_enum, default_value_t)]
39    pub input_format: InputFormat,
40
41    // Output format
42    #[arg(long = "output", value_enum, default_value_t)]
43    pub output_format: OutputFormat,
44}
45
46#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, ValueEnum)]
47pub enum InputFormat {
48    Single,
49    SingleBase64,
50    Stream,
51    StreamBase64,
52    StreamFramed,
53}
54
55impl Default for InputFormat {
56    fn default() -> Self {
57        Self::StreamBase64
58    }
59}
60
61#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, ValueEnum)]
62pub enum OutputFormat {
63    Json,
64    JsonFormatted,
65    Text,
66    RustDebug,
67    RustDebugFormatted,
68}
69
70impl Default for OutputFormat {
71    fn default() -> Self {
72        Self::Json
73    }
74}
75
76// TODO: Remove run_x macro, it exists only to reduce the diff from when curr/next
77// channels existed and each had their own run_curr/run_next invocation.
78macro_rules! run_x {
79    ($f:ident) => {
80        fn $f(&self) -> Result<(), Error> {
81            let mut input = util::parse_input(&self.input).map_err(Error::ReadFile)?;
82            let r#type = crate::TypeVariant::from_str(&self.r#type).map_err(|_| {
83                Error::UnknownType(self.r#type.clone(), &crate::TypeVariant::VARIANTS_STR)
84            })?;
85            for f in &mut input {
86                match self.input_format {
87                    InputFormat::Single => {
88                        let mut l = crate::Limited::new(f, crate::Limits::none());
89                        let t = crate::Type::read_xdr_to_end(r#type, &mut l)?;
90                        self.out(&t)?;
91                    }
92                    InputFormat::SingleBase64 => {
93                        let mut l = crate::Limited::new(f, crate::Limits::none());
94                        let t = crate::Type::read_xdr_base64_to_end(r#type, &mut l)?;
95                        self.out(&t)?;
96                    }
97                    InputFormat::Stream => {
98                        let mut l = crate::Limited::new(f, crate::Limits::none());
99                        for t in crate::Type::read_xdr_iter(r#type, &mut l) {
100                            self.out(&t?)?;
101                        }
102                    }
103                    InputFormat::StreamBase64 => {
104                        let mut l = crate::Limited::new(f, crate::Limits::none());
105                        for t in crate::Type::read_xdr_base64_iter(r#type, &mut l) {
106                            self.out(&t?)?;
107                        }
108                    }
109                    InputFormat::StreamFramed => {
110                        let mut l = crate::Limited::new(f, crate::Limits::none());
111                        for t in crate::Type::read_xdr_framed_iter(r#type, &mut l) {
112                            self.out(&t?)?;
113                        }
114                    }
115                };
116            }
117            Ok(())
118        }
119    };
120}
121
122impl Cmd {
123    /// Run the CLIs decode command.
124    ///
125    /// ## Errors
126    ///
127    /// If the command is configured with state that is invalid.
128    pub fn run(&self) -> Result<(), Error> {
129        let result = self.run_inner();
130        match result {
131            Ok(()) => Ok(()),
132            Err(Error::WriteOutput(e)) if e.kind() == std::io::ErrorKind::BrokenPipe => Ok(()),
133            Err(e) => Err(e),
134        }
135    }
136
137    run_x!(run_inner);
138
139    fn out(&self, v: &(impl Serialize + Debug)) -> Result<(), Error> {
140        let text = match self.output_format {
141            OutputFormat::Json => serde_json::to_string(v)?,
142            OutputFormat::JsonFormatted => serde_json::to_string_pretty(v)?,
143            OutputFormat::Text => {
144                let v = serde_json::to_value(v)?;
145                util::serde_json_value_to_text(v).ok_or(Error::TextUnsupported)?
146            }
147            OutputFormat::RustDebug => format!("{v:?}"),
148            OutputFormat::RustDebugFormatted => format!("{v:#?}"),
149        };
150        writeln!(stdout(), "{text}").map_err(Error::WriteOutput)?;
151        Ok(())
152    }
153}