Rework 'blueprint apply' command and wrap up wiring up validation.

The apply command now works only from a serialized CBOR data (instead of a UPLC syntax). So it is no longer possible to specify arbitrary cbor terms through the CLI. I believe it to be an acceptable limitation for now; especially given that Aiken will never generate blueprints with non-data terms at the interface boundary.
This commit is contained in:
KtorZ
2023-04-07 17:40:40 +02:00
parent bf222a3ea2
commit 4799af3242
4 changed files with 95 additions and 26 deletions

View File

@@ -1,18 +1,22 @@
use crate::with_project;
use aiken_project::error::Error;
use miette::IntoDiagnostic;
use std::{fs, path::PathBuf};
use uplc::{
ast::{DeBruijn, Term},
parser,
};
use aiken_project::{blueprint, error::Error};
use owo_colors::{OwoColorize, Stream::Stderr};
use std::{fs, path::PathBuf, process, rc::Rc};
use uplc::ast::{Constant, DeBruijn, Term};
/// Apply a parameter to a parameterized validator.
#[derive(clap::Args)]
pub struct Args {
/// The parameter, as a Plutus Data (CBOR, hex-encoded)
parameter: String,
/// Path to project
directory: Option<PathBuf>,
/// Output file. Optional, print on stdout when omitted.
#[clap(short, long)]
out: Option<PathBuf>,
/// Name of the validator's module within the project. Optional if there's only one validator.
#[clap(short, long)]
module: Option<String>,
@@ -20,23 +24,58 @@ pub struct Args {
/// Name of the validator within the module. Optional if there's only one validator.
#[clap(short, long)]
validator: Option<String>,
/// The parameter, using high-level UPLC-syntax
parameter: String,
}
pub fn exec(
Args {
parameter,
directory,
out,
module,
validator,
parameter,
}: Args,
) -> miette::Result<()> {
let term: Term<DeBruijn> = parser::term(&parameter)
.into_diagnostic()?
.try_into()
.into_diagnostic()?;
eprintln!(
"{} inputs",
" Parsing"
.if_supports_color(Stderr, |s| s.purple())
.if_supports_color(Stderr, |s| s.bold()),
);
let bytes = hex::decode(parameter)
.map_err::<Error, _>(|e| {
blueprint::error::Error::MalformedParameter {
hint: format!("Invalid hex-encoded string: {e}"),
}
.into()
})
.unwrap_or_else(|e| {
println!();
e.report();
process::exit(1)
});
let data = uplc::plutus_data(&bytes)
.map_err::<Error, _>(|e| {
blueprint::error::Error::MalformedParameter {
hint: format!("Invalid Plutus data; malformed CBOR encoding: {e}"),
}
.into()
})
.unwrap_or_else(|e| {
println!();
e.report();
process::exit(1)
});
let term: Term<DeBruijn> = Term::Constant(Rc::new(Constant::Data(data)));
eprintln!(
"{} blueprint",
" Analyzing"
.if_supports_color(Stderr, |s| s.purple())
.if_supports_color(Stderr, |s| s.bold()),
);
with_project(directory, |p| {
let title = module.as_ref().map(|m| {
@@ -51,16 +90,35 @@ pub fn exec(
let title = title.as_ref().or(validator.as_ref());
eprintln!(
"{} parameter",
" Applying"
.if_supports_color(Stderr, |s| s.purple())
.if_supports_color(Stderr, |s| s.bold()),
);
let blueprint = p.apply_parameter(title, &term)?;
let json = serde_json::to_string_pretty(&blueprint).unwrap();
fs::write(p.blueprint_path(), json).map_err(|error| {
Error::FileIo {
match out {
None => {
println!("\n{}\n", json);
Ok(())
}
Some(ref path) => fs::write(path, json).map_err(|error| Error::FileIo {
error,
path: p.blueprint_path(),
}
.into()
})
}),
}?;
eprintln!(
"{}",
" Done"
.if_supports_color(Stderr, |s| s.purple())
.if_supports_color(Stderr, |s| s.bold()),
);
Ok(())
})
}