Skip to content

Commit

Permalink
feat: Add detect support for parity_scale_decoder
Browse files Browse the repository at this point in the history
Signed-off-by: aplio <[email protected]>
  • Loading branch information
aplio committed Jun 3, 2024
1 parent 9c58203 commit 87f50ac
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 18 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 2 additions & 6 deletions tools/parity_scale_cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@ license.workspace = true
[lints]
workspace = true

[features]
# Disable colour for all program output.
# Useful for Docker-based deployment and terminals without colour support.
no_color = ["colored/no-color"]

[dependencies]
iroha_data_model = { workspace = true, features = ["http"] }
iroha_primitives = { workspace = true }
Expand All @@ -24,12 +19,13 @@ iroha_crypto = { workspace = true }
iroha_version = { workspace = true }
iroha_genesis = { workspace = true }

clap = { workspace = true, features = ["derive", "cargo"] }
clap = { workspace = true, features = ["derive", "cargo", "env", "string"] }
eyre = { workspace = true }
parity-scale-codec = { workspace = true }
colored = "2.1.0"
serde_json = { workspace = true, features = ["std"]}
serde = { workspace = true }
supports-color = { workspace = true }

[build-dependencies]
iroha_data_model = { workspace = true }
Expand Down
6 changes: 0 additions & 6 deletions tools/parity_scale_cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,6 @@ To build the tool, run:
cargo build --bin parity_scale_cli
```

If your terminal does not support colours, run:

```bash
cargo build --features no_color --bin parity_scale_cli
```

## Usage

Run Parity Scale Decoder Tool:
Expand Down
61 changes: 55 additions & 6 deletions tools/parity_scale_cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,29 @@ where
/// Parity Scale decoder tool for Iroha data types
#[derive(Debug, Parser)]
#[clap(version, about, author)]
enum Args {
struct Args {
#[clap(subcommand)]
command: Command,

/// Whether to enable ANSI colored output or not
///
/// By default, Iroha determines whether the terminal supports colors or not.
///
/// In order to disable this flag explicitly, pass `--terminal-colors=false`.
#[arg(
long,
env,
default_missing_value("true"),
default_value(default_terminal_colors_str()),
action(clap::ArgAction::Set),
require_equals(true),
num_args(0..=1),
)]
pub terminal_colors: bool,
}

#[derive(Debug, Parser)]
enum Command {
/// Show all available types
ListTypes,
/// Decode SCALE to Rust debug format from binary file
Expand Down Expand Up @@ -113,26 +135,34 @@ struct ScaleJsonArgs {
type_name: String,
}

fn is_colouring_supported() -> bool {
supports_color::on(supports_color::Stream::Stdout).is_some()
}

fn default_terminal_colors_str() -> clap::builder::OsStr {
is_colouring_supported().to_string().into()
}

fn main() -> Result<()> {
let args = Args::parse();

let map = generate_map();

match args {
Args::ScaleToRust(decode_args) => {
match args.command {
Command::ScaleToRust(decode_args) => {
let mut writer = BufWriter::new(io::stdout().lock());
let decoder = ScaleToRustDecoder::new(decode_args, &map);
decoder.decode(&mut writer)
}
Args::ScaleToJson(args) => {
Command::ScaleToJson(args) => {
let decoder = ScaleJsonDecoder::new(args, &map)?;
decoder.scale_to_json()
}
Args::JsonToScale(args) => {
Command::JsonToScale(args) => {
let decoder = ScaleJsonDecoder::new(args, &map)?;
decoder.json_to_scale()
}
Args::ListTypes => {
Command::ListTypes => {
let mut writer = BufWriter::new(io::stdout().lock());
list_types(&map, &mut writer)
}
Expand Down Expand Up @@ -383,4 +413,23 @@ mod tests {
.expect("Couldn't convert to SCALE");
assert_eq!(scale_actual, scale_expected);
}

#[test]
fn terminal_colors_works_as_expected() -> eyre::Result<()> {
fn try_with(arg: &str) -> eyre::Result<bool> {
// Since arg contains enum Command and we must provide something for it, we use "list-types"
Ok(Args::try_parse_from(["test", arg, "list-types"])?.terminal_colors)
}

assert_eq!(
Args::try_parse_from(["test", "list-types"])?.terminal_colors,
is_colouring_supported()
);
assert_eq!(try_with("--terminal-colors")?, true);
assert_eq!(try_with("--terminal-colors=false")?, false);
assert_eq!(try_with("--terminal-colors=true")?, true);
assert!(try_with("--terminal-colors=random").is_err());

Ok(())
}
}

0 comments on commit 87f50ac

Please sign in to comment.