Define 'is_library' for 'CheckedModule'

So that we can separate libraries from executable modules if necessary.
This commit is contained in:
KtorZ 2022-12-16 15:33:27 +01:00
parent 1edbf57943
commit 71e71fffe8
No known key found for this signature in database
GPG Key ID: 33173CB6F77F4277
2 changed files with 18 additions and 8 deletions

View File

@ -7,6 +7,7 @@ pub mod pretty;
pub mod script; pub mod script;
pub mod telemetry; pub mod telemetry;
use crate::module::{CERT, MINT, SPEND, VALIDATOR_NAMES, WITHDRAW};
use aiken_lang::{ use aiken_lang::{
ast::{Definition, Function, ModuleKind, TypedDataType, TypedDefinition, TypedFunction}, ast::{Definition, Function, ModuleKind, TypedDataType, TypedDefinition, TypedFunction},
builder::{DataTypeKey, FunctionAccessKey}, builder::{DataTypeKey, FunctionAccessKey},
@ -50,12 +51,6 @@ pub struct Source {
pub kind: ModuleKind, pub kind: ModuleKind,
} }
pub const SPEND: &str = "spend";
pub const CERT: &str = "cert";
pub const MINT: &str = "mint";
pub const WITHDRAWL: &str = "withdrawl";
pub const VALIDATOR_NAMES: [&str; 4] = [SPEND, CERT, MINT, WITHDRAWL];
pub struct Project<T> pub struct Project<T>
where where
T: EventListener, T: EventListener,
@ -351,7 +346,7 @@ where
// depending on name, validate the minimum number of arguments // depending on name, validate the minimum number of arguments
// if too low, push a new error on to errors // if too low, push a new error on to errors
if [MINT, CERT, WITHDRAWL].contains(&func_def.name.as_str()) if [MINT, CERT, WITHDRAW].contains(&func_def.name.as_str())
&& func_def.arguments.len() < 2 && func_def.arguments.len() < 2
{ {
errors.push(Error::WrongValidatorArity { errors.push(Error::WrongValidatorArity {

View File

@ -4,7 +4,7 @@ use std::{
path::PathBuf, path::PathBuf,
}; };
use aiken_lang::ast::{ModuleKind, TypedModule, UntypedModule}; use aiken_lang::ast::{Definition, ModuleKind, TypedModule, UntypedModule};
use petgraph::{algo, graph::NodeIndex, Direction, Graph}; use petgraph::{algo, graph::NodeIndex, Direction, Graph};
use crate::error::Error; use crate::error::Error;
@ -155,6 +155,12 @@ fn find_cycle(
false false
} }
pub const SPEND: &str = "spend";
pub const CERT: &str = "cert";
pub const MINT: &str = "mint";
pub const WITHDRAW: &str = "withdraw";
pub const VALIDATOR_NAMES: [&str; 4] = [SPEND, CERT, MINT, WITHDRAW];
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct CheckedModule { pub struct CheckedModule {
pub name: String, pub name: String,
@ -165,6 +171,15 @@ pub struct CheckedModule {
// pub extra: ModuleExtra, // pub extra: ModuleExtra,
} }
impl CheckedModule {
pub fn is_library(&self) -> bool {
self.ast.definitions().any(|def| match def {
Definition::Fn(func_def) => VALIDATOR_NAMES.contains(&func_def.name.as_str()),
_ => false,
})
}
}
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct CheckedModules(HashMap<String, CheckedModule>); pub struct CheckedModules(HashMap<String, CheckedModule>);