Define 'apply_parameter' method on 'Project'

This commit is contained in:
KtorZ 2023-02-04 10:44:33 +01:00
parent 12f4768008
commit 592d3d7a1c
No known key found for this signature in database
GPG Key ID: 33173CB6F77F4277
3 changed files with 42 additions and 8 deletions

View File

@ -53,7 +53,10 @@ impl Blueprint<Schema> {
} }
} }
impl<T> Blueprint<T> { impl<T> Blueprint<T>
where
T: Clone,
{
pub fn lookup( pub fn lookup(
&self, &self,
title: Option<&String>, title: Option<&String>,
@ -83,10 +86,10 @@ impl<T> Blueprint<T> {
action: F, action: F,
) -> Result<A, E> ) -> Result<A, E>
where where
F: Fn(&Validator<T>) -> Result<A, E>, F: Fn(Validator<T>) -> Result<A, E>,
{ {
match self.lookup(title, purpose) { match self.lookup(title, purpose) {
Some(LookupResult::One(validator)) => action(validator), Some(LookupResult::One(validator)) => action(validator.to_owned()),
Some(LookupResult::Many) => Err(when_too_many( Some(LookupResult::Many) => Err(when_too_many(
self.validators self.validators
.iter() .iter()

View File

@ -122,7 +122,12 @@ impl Validator<Schema> {
.unwrap(), .unwrap(),
}) })
} }
}
impl<T> Validator<T>
where
T: Clone,
{
pub fn apply(&mut self, arg: &Term<DeBruijn>) -> Result<(), Error> { pub fn apply(&mut self, arg: &Term<DeBruijn>) -> Result<(), Error> {
match self.parameters.split_first() { match self.parameters.split_first() {
None => Err(Error::NoParametersToApply), None => Err(Error::NoParametersToApply),

View File

@ -12,7 +12,7 @@ pub mod pretty;
pub mod script; pub mod script;
pub mod telemetry; pub mod telemetry;
use crate::blueprint::{schema::Schema, validator, Blueprint, LookupResult}; use crate::blueprint::{schema::Schema, validator, Blueprint};
use aiken_lang::{ use aiken_lang::{
ast::{Definition, Function, ModuleKind, TypedDataType, TypedFunction}, ast::{Definition, Function, ModuleKind, TypedDataType, TypedFunction},
builder::{DataTypeKey, FunctionAccessKey}, builder::{DataTypeKey, FunctionAccessKey},
@ -37,7 +37,7 @@ use std::{
}; };
use telemetry::EventListener; use telemetry::EventListener;
use uplc::{ use uplc::{
ast::{Constant, Term}, ast::{Constant, DeBruijn, Term},
machine::cost_model::ExBudget, machine::cost_model::ExBudget,
}; };
@ -306,9 +306,8 @@ where
}; };
// Read blueprint // Read blueprint
let filepath = self.blueprint_path(); let blueprint = File::open(self.blueprint_path())
let blueprint = .map_err(|_| blueprint::error::Error::InvalidOrMissingFile)?;
File::open(filepath).map_err(|_| blueprint::error::Error::InvalidOrMissingFile)?;
let blueprint: Blueprint<serde_json::Value> = let blueprint: Blueprint<serde_json::Value> =
serde_json::from_reader(BufReader::new(blueprint))?; serde_json::from_reader(BufReader::new(blueprint))?;
@ -323,6 +322,33 @@ where
}) })
} }
pub fn apply_parameter(
&self,
title: Option<&String>,
purpose: Option<&validator::Purpose>,
param: &Term<DeBruijn>,
) -> Result<Blueprint<serde_json::Value>, Error> {
// Read blueprint
let blueprint = File::open(self.blueprint_path())
.map_err(|_| blueprint::error::Error::InvalidOrMissingFile)?;
let blueprint: Blueprint<serde_json::Value> =
serde_json::from_reader(BufReader::new(blueprint))?;
// Apply parameters
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,
|mut validator| validator.apply(param).map_err(|e| e.into()),
)?;
Ok(blueprint)
}
fn compile_deps(&mut self) -> Result<(), Error> { fn compile_deps(&mut self) -> Result<(), Error> {
let manifest = deps::download( let manifest = deps::download(
&self.event_listener, &self.event_listener,