lighthouse/lcli/src/parse_hex.rs
blacktemplar d8cda2d86e Fix new clippy lints (#2036)
## Issue Addressed

NA

## Proposed Changes

Fixes new clippy lints in the whole project (mainly [manual_strip](https://rust-lang.github.io/rust-clippy/master/index.html#manual_strip) and [unnecessary_lazy_evaluations](https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_lazy_evaluations)). Furthermore, removes `to_string()` calls on literals when used with the `?`-operator.
2020-12-03 01:10:26 +00:00

42 lines
1.2 KiB
Rust

use clap::ArgMatches;
use serde::Serialize;
use ssz::Decode;
use types::{BeaconBlock, BeaconState, EthSpec};
pub fn run_parse_hex<T: EthSpec>(matches: &ArgMatches) -> Result<(), String> {
let type_str = matches.value_of("type").ok_or("No type supplied")?;
let mut hex: String = matches
.value_of("hex_ssz")
.ok_or("No hex ssz supplied")?
.to_string();
if hex.starts_with("0x") {
hex = hex[2..].to_string();
}
let hex = hex::decode(&hex).map_err(|e| format!("Failed to parse hex: {:?}", e))?;
info!("Using {} spec", T::spec_name());
info!("Type: {:?}", type_str);
match type_str {
"block" => decode_and_print::<BeaconBlock<T>>(&hex)?,
"state" => decode_and_print::<BeaconState<T>>(&hex)?,
other => return Err(format!("Unknown type: {}", other)),
};
Ok(())
}
fn decode_and_print<T: Decode + Serialize>(bytes: &[u8]) -> Result<(), String> {
let item = T::from_ssz_bytes(&bytes).map_err(|e| format!("Ssz decode failed: {:?}", e))?;
println!(
"{}",
serde_yaml::to_string(&item)
.map_err(|e| format!("Unable to write object to YAML: {:?}", e))?
);
Ok(())
}