fix(cli): aiken address

This commit is contained in:
rvcas 2023-02-15 22:04:45 -05:00 committed by Lucas
parent 8b4985498b
commit a311531508
6 changed files with 62 additions and 77 deletions

View File

@ -7,7 +7,7 @@ use aiken_lang::uplc::CodeGenerator;
use error::Error;
use schema::Schema;
use std::fmt::{self, Debug, Display};
use validator::{Purpose, Validator};
use validator::Validator;
#[derive(Debug, PartialEq, Clone, serde::Serialize, serde::Deserialize)]
pub struct Blueprint<T: Default> {
@ -57,16 +57,13 @@ impl<T> Blueprint<T>
where
T: Clone + Default,
{
pub fn lookup(
&self,
title: Option<&String>,
purpose: Option<&Purpose>,
) -> Option<LookupResult<Validator<T>>> {
pub fn lookup(&self, title: Option<&String>) -> Option<LookupResult<Validator<T>>> {
let mut validator = None;
for v in self.validators.iter() {
let match_title = Some(&v.title) == title.or(Some(&v.title));
let match_purpose = v.purpose.as_ref() == purpose.or(v.purpose.as_ref());
if match_title && match_purpose {
let match_title = title.map(|t| v.title.contains(t)).unwrap_or(false);
if match_title {
validator = Some(if validator.is_none() {
LookupResult::One(v)
} else {
@ -74,47 +71,27 @@ where
})
}
}
validator
}
pub fn with_validator<F, A, E>(
&self,
title: Option<&String>,
purpose: Option<&Purpose>,
when_missing: fn(Vec<(String, String)>) -> E,
when_too_many: fn(Vec<(String, String)>) -> E,
when_missing: fn(Vec<String>) -> E,
when_too_many: fn(Vec<String>) -> E,
action: F,
) -> Result<A, E>
where
F: Fn(Validator<T>) -> Result<A, E>,
{
match self.lookup(title, purpose) {
match self.lookup(title) {
Some(LookupResult::One(validator)) => action(validator.to_owned()),
Some(LookupResult::Many) => Err(when_too_many(
self.validators
.iter()
.map(|v| {
let mut title = v.title.split('.');
(
title.next().unwrap().to_string(),
title.next().unwrap().to_string(),
)
})
.collect(),
self.validators.iter().map(|v| v.title.clone()).collect(),
)),
None => Err(when_missing(
self.validators
.iter()
.map(|v| {
let mut title = v.title.split('-');
(
title.next().unwrap().to_string(),
title.next().unwrap().to_string(),
)
})
.collect(),
self.validators.iter().map(|v| v.title.clone()).collect(),
)),
}
}

View File

@ -132,14 +132,10 @@ pub enum Error {
},
#[error("I didn't find any validator matching your criteria.")]
NoValidatorNotFound {
known_validators: Vec<(String, String)>,
},
NoValidatorNotFound { known_validators: Vec<String> },
#[error("I found multiple suitable validators and I need you to tell me which one to pick.")]
MoreThanOneValidatorFound {
known_validators: Vec<(String, String)>,
},
MoreThanOneValidatorFound { known_validators: Vec<String> },
}
impl Error {
@ -374,13 +370,13 @@ impl Diagnostic for Error {
Error::NoValidatorNotFound { known_validators } => {
Some(Box::new(format!(
"Here's a list of all validators (and their purpose) I've found in your project. Please double-check this list against the options that you've provided:\n\n{}",
known_validators.iter().map(|(name, purpose)| format!("{name} (purpose = {purpose})", name = name.purple().bold(), purpose = purpose.bright_blue())).collect::<Vec<String>>().join("\n")
known_validators.iter().map(|title| format!("{title}", title = title.purple().bold())).collect::<Vec<String>>().join("\n")
)))
},
Error::MoreThanOneValidatorFound { known_validators } => {
Some(Box::new(format!(
"Here's a list of all validators (and their purpose) I've found in your project. Select one of them using the appropriate options:\n\n{}",
known_validators.iter().map(|(name, purpose)| format!("{name} (purpose = {purpose})", name = name.purple().bold(), purpose = purpose.bright_blue())).collect::<Vec<String>>().join("\n")
known_validators.iter().map(|title| format!("{title}", title = title.purple().bold())).collect::<Vec<String>>().join("\n")
)))
},
}

View File

@ -12,7 +12,7 @@ pub mod pretty;
pub mod script;
pub mod telemetry;
use crate::blueprint::{schema::Schema, validator, Blueprint};
use crate::blueprint::{schema::Schema, Blueprint};
use aiken_lang::{
ast::{Definition, Function, ModuleKind, TypedDataType, TypedFunction},
builder::{DataTypeKey, FunctionAccessKey},
@ -287,7 +287,6 @@ where
pub fn address(
&self,
title: Option<&String>,
purpose: Option<&validator::Purpose>,
stake_address: Option<&String>,
) -> Result<ShelleyAddress, Error> {
// Parse stake address
@ -318,7 +317,8 @@ where
let when_too_many =
|known_validators| Error::MoreThanOneValidatorFound { known_validators };
let when_missing = |known_validators| Error::NoValidatorNotFound { known_validators };
blueprint.with_validator(title, purpose, when_too_many, when_missing, |validator| {
blueprint.with_validator(title, when_too_many, when_missing, |validator| {
let n = validator.parameters.len();
if n > 0 {
Err(blueprint::error::Error::ParameterizedValidator { n }.into())
@ -333,7 +333,6 @@ where
pub fn apply_parameter(
&self,
title: Option<&String>,
purpose: Option<&validator::Purpose>,
param: &Term<DeBruijn>,
) -> Result<Blueprint<serde_json::Value>, Error> {
// Read blueprint
@ -346,8 +345,9 @@ where
let when_too_many =
|known_validators| Error::MoreThanOneValidatorFound { known_validators };
let when_missing = |known_validators| Error::NoValidatorNotFound { known_validators };
let applied_validator =
blueprint.with_validator(title, purpose, when_too_many, when_missing, |validator| {
blueprint.with_validator(title, when_too_many, when_missing, |validator| {
validator.apply(param).map_err(|e| e.into())
})?;

View File

@ -1,5 +1,4 @@
use crate::with_project;
use aiken_lang::VALIDATOR_NAMES;
use std::path::PathBuf;
#[derive(clap::Args)]
@ -11,11 +10,11 @@ pub struct Args {
/// Name of the validator's module within the project. Optional if there's only one validator.
#[clap(short, long)]
validator: Option<String>,
module: Option<String>,
/// Purpose of the validator within the module. Optional if there's only one validator.
#[clap(short, long, possible_values=&VALIDATOR_NAMES)]
purpose: Option<String>,
/// Name of the validator within the module. Optional if there's only one validator.
#[clap(short, long)]
validator: Option<String>,
/// Stake address to attach, if any.
#[clap(long)]
@ -29,8 +28,8 @@ pub struct Args {
pub fn exec(
Args {
directory,
module,
validator,
purpose,
delegated_to,
rebuild,
}: Args,
@ -39,15 +38,23 @@ pub fn exec(
if rebuild {
p.build(false)?;
}
let address = p.address(
validator.as_ref(),
purpose
let title = module.as_ref().map(|m| {
format!(
"{m}{}",
validator
.as_ref()
.map(|p| p.clone().try_into().unwrap())
.as_ref(),
delegated_to.as_ref(),
)?;
.map(|v| format!(".{v}"))
.unwrap_or("".to_string())
)
});
let title = title.as_ref().or(validator.as_ref());
let address = p.address(title, delegated_to.as_ref())?;
println!("{}", address.to_bech32().unwrap());
Ok(())
})
}

View File

@ -1,5 +1,4 @@
use crate::with_project;
use aiken_lang::VALIDATOR_NAMES;
use aiken_project::error::Error;
use miette::IntoDiagnostic;
use std::{fs, path::PathBuf};
@ -17,11 +16,11 @@ pub struct Args {
/// Name of the validator's module within the project. Optional if there's only one validator.
#[clap(short, long)]
validator: Option<String>,
module: Option<String>,
/// Purpose of the validator within the module. Optional if there's only one validator.
#[clap(short, long, possible_values=&VALIDATOR_NAMES)]
purpose: Option<String>,
/// 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,
@ -30,8 +29,8 @@ pub struct Args {
pub fn exec(
Args {
directory,
module,
validator,
purpose,
parameter,
}: Args,
) -> miette::Result<()> {
@ -41,16 +40,22 @@ pub fn exec(
.into_diagnostic()?;
with_project(directory, |p| {
let blueprint = p.apply_parameter(
validator.as_ref(),
purpose
let title = module.as_ref().map(|m| {
format!(
"{m}{}",
validator
.as_ref()
.map(|p| p.clone().try_into().unwrap())
.as_ref(),
&term,
)?;
.map(|v| format!(".{v}"))
.unwrap_or("".to_string())
)
});
let title = title.as_ref().or(validator.as_ref());
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 {
error,
path: p.blueprint_path(),

View File

@ -6,7 +6,7 @@
},
"validators": [
{
"title": "hello_world-spend",
"title": "hello_world.spend",
"datum": {
"title": "Datum",
"anyOf": [