feat: some cbor hex flags for flat and unflat

This commit is contained in:
rvcas 2022-09-04 17:32:38 -04:00
parent da73995124
commit bc983d694a
No known key found for this signature in database
GPG Key ID: C09B64E263F7D68C
2 changed files with 60 additions and 21 deletions

View File

@ -66,6 +66,8 @@ pub enum UplcCommand {
print: bool, print: bool,
#[clap(short, long)] #[clap(short, long)]
out: Option<String>, out: Option<String>,
#[clap(short, long)]
cbor_hex: bool,
}, },
/// Decode flat bytes to textual Untyped Plutus Core /// Decode flat bytes to textual Untyped Plutus Core
Unflat { Unflat {
@ -74,6 +76,8 @@ pub enum UplcCommand {
print: bool, print: bool,
#[clap(short, long)] #[clap(short, long)]
out: Option<String>, out: Option<String>,
#[clap(short, long)]
cbor_hex: bool,
}, },
/// Format an Untyped Plutus Core program /// Format an Untyped Plutus Core program
Fmt { Fmt {

View File

@ -78,13 +78,19 @@ fn main() -> anyhow::Result<()> {
} }
}, },
Args::Uplc(uplc_cmd) => match uplc_cmd { Args::Uplc(uplc_cmd) => match uplc_cmd {
UplcCommand::Flat { input, print, out } => { UplcCommand::Flat {
input,
print,
out,
cbor_hex,
} => {
let code = std::fs::read_to_string(&input)?; let code = std::fs::read_to_string(&input)?;
let program = parser::program(&code)?; let program = parser::program(&code)?;
let program = Program::<DeBruijn>::try_from(program)?; let program = Program::<DeBruijn>::try_from(program)?;
if cbor_hex {
let bytes = program.to_flat()?; let bytes = program.to_flat()?;
if print { if print {
@ -110,6 +116,21 @@ fn main() -> anyhow::Result<()> {
fs::write(&out_name, &bytes)?; fs::write(&out_name, &bytes)?;
} }
} else {
let cbor = program.to_hex()?;
if print {
println!("{}", &cbor);
} else {
let out_name = if let Some(out) = out {
out
} else {
format!("{}.cbor", input.file_stem().unwrap().to_str().unwrap())
};
fs::write(&out_name, &cbor)?;
}
}
} }
UplcCommand::Fmt { input, print } => { UplcCommand::Fmt { input, print } => {
let code = std::fs::read_to_string(&input)?; let code = std::fs::read_to_string(&input)?;
@ -124,10 +145,24 @@ fn main() -> anyhow::Result<()> {
fs::write(&input, pretty)?; fs::write(&input, pretty)?;
} }
} }
UplcCommand::Unflat { input, print, out } => { UplcCommand::Unflat {
input,
print,
out,
cbor_hex,
} => {
let program = if cbor_hex {
let cbor = std::fs::read_to_string(&input)?;
let mut cbor_buffer = Vec::new();
let mut flat_buffer = Vec::new();
Program::<DeBruijn>::from_hex(cbor.trim(), &mut cbor_buffer, &mut flat_buffer)?
} else {
let bytes = std::fs::read(&input)?; let bytes = std::fs::read(&input)?;
let program = Program::<DeBruijn>::from_flat(&bytes)?; Program::<DeBruijn>::from_flat(&bytes)?
};
let program: Program<Name> = program.try_into()?; let program: Program<Name> = program.try_into()?;