diff --git a/crates/aiken-project/src/blueprint/mod.rs b/crates/aiken-project/src/blueprint/mod.rs index 91c8fff9..22ad0590 100644 --- a/crates/aiken-project/src/blueprint/mod.rs +++ b/crates/aiken-project/src/blueprint/mod.rs @@ -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 { @@ -57,16 +57,13 @@ impl Blueprint where T: Clone + Default, { - pub fn lookup( - &self, - title: Option<&String>, - purpose: Option<&Purpose>, - ) -> Option>> { + pub fn lookup(&self, title: Option<&String>) -> Option>> { 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( &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) -> E, + when_too_many: fn(Vec) -> E, action: F, ) -> Result where F: Fn(Validator) -> Result, { - 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(), )), } } diff --git a/crates/aiken-project/src/error.rs b/crates/aiken-project/src/error.rs index c2f82ac5..9e6ddcee 100644 --- a/crates/aiken-project/src/error.rs +++ b/crates/aiken-project/src/error.rs @@ -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 }, #[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 }, } 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::>().join("\n") + known_validators.iter().map(|title| format!("→ {title}", title = title.purple().bold())).collect::>().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::>().join("\n") + known_validators.iter().map(|title| format!("→ {title}", title = title.purple().bold())).collect::>().join("\n") ))) }, } diff --git a/crates/aiken-project/src/lib.rs b/crates/aiken-project/src/lib.rs index 8e53eeb4..bcc76816 100644 --- a/crates/aiken-project/src/lib.rs +++ b/crates/aiken-project/src/lib.rs @@ -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 { // 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, ) -> Result, 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()) })?; diff --git a/crates/aiken/src/cmd/blueprint/address.rs b/crates/aiken/src/cmd/blueprint/address.rs index 467e9c7b..623f18ba 100644 --- a/crates/aiken/src/cmd/blueprint/address.rs +++ b/crates/aiken/src/cmd/blueprint/address.rs @@ -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, + module: Option, - /// Purpose of the validator within the module. Optional if there's only one validator. - #[clap(short, long, possible_values=&VALIDATOR_NAMES)] - purpose: Option, + /// Name of the validator within the module. Optional if there's only one validator. + #[clap(short, long)] + validator: Option, /// 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 - .as_ref() - .map(|p| p.clone().try_into().unwrap()) - .as_ref(), - delegated_to.as_ref(), - )?; + + let title = module.as_ref().map(|m| { + format!( + "{m}{}", + validator + .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(()) }) } diff --git a/crates/aiken/src/cmd/blueprint/apply.rs b/crates/aiken/src/cmd/blueprint/apply.rs index 9fb70586..56147ef8 100644 --- a/crates/aiken/src/cmd/blueprint/apply.rs +++ b/crates/aiken/src/cmd/blueprint/apply.rs @@ -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, + module: Option, - /// Purpose of the validator within the module. Optional if there's only one validator. - #[clap(short, long, possible_values=&VALIDATOR_NAMES)] - purpose: Option, + /// Name of the validator within the module. Optional if there's only one validator. + #[clap(short, long)] + validator: Option, /// 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 - .as_ref() - .map(|p| p.clone().try_into().unwrap()) - .as_ref(), - &term, - )?; + let title = module.as_ref().map(|m| { + format!( + "{m}{}", + validator + .as_ref() + .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(), diff --git a/examples/hello_world/plutus.json b/examples/hello_world/plutus.json index 75c45432..e0179760 100644 --- a/examples/hello_world/plutus.json +++ b/examples/hello_world/plutus.json @@ -6,7 +6,7 @@ }, "validators": [ { - "title": "hello_world-spend", + "title": "hello_world.spend", "datum": { "title": "Datum", "anyOf": [