feat: improve validator arity check

* add count to dynamically adjust message
* check if args is greater than 3
* delete unused project level errors
This commit is contained in:
rvcas 2023-02-20 15:03:49 -05:00 committed by Lucas
parent 700e47d482
commit bd93ced647
3 changed files with 30 additions and 40 deletions

View File

@ -763,7 +763,7 @@ The best thing to do from here is to remove it."#))]
{signature} {signature}
...but I expected this to be a {type_Bool}. If I am inferring the wrong type, you may want to add a type annotation to the function."# ...but I expected this to be a {type_Bool}. If I am inferring the wrong type, try annotating the validator's return type with Bool"#
, type_Bool = "Bool".bright_blue().bold() , type_Bool = "Bool".bright_blue().bold()
, signature = return_type.to_pretty(0).red() , signature = return_type.to_pretty(0).red()
))] ))]
@ -773,11 +773,35 @@ The best thing to do from here is to remove it."#))]
return_type: Arc<Type>, return_type: Arc<Type>,
}, },
#[error("Validators requires at least {} arguments.", at_least.to_string().purple().bold())] #[error("Validators require at least 2 arguments and at most 3 arguments.")]
#[diagnostic(code("illegal::validator_arity"))] #[diagnostic(code("illegal::validator_arity"))]
#[diagnostic(help(
"Validators require either 2 or 3 arguments {}.\nIf you don't need one of the required arguments use an underscore `_datum`.",
if *count < 2 {
let missing = 2 - count;
let mut arguments = "argument".to_string();
if missing > 1 {
arguments.push('s');
}
format!("please add the {} missing {arguments}", missing.to_string().yellow())
} else {
let extra = count - 3;
let mut arguments = "argument".to_string();
if extra > 1 {
arguments.push('s');
}
format!("please remove the {} extra {arguments}", extra.to_string().yellow())
}
))]
IncorrectValidatorArity { IncorrectValidatorArity {
at_least: u8, count: u32,
#[label("not enough arguments")] #[label("{} arguments", if *count < 2 { "not enough" } else { "too many" })]
location: Span, location: Span,
}, },
} }

View File

@ -281,9 +281,9 @@ fn infer_definition(
let typed_params = typed_fun.arguments.drain(0..params_length).collect(); let typed_params = typed_fun.arguments.drain(0..params_length).collect();
if typed_fun.arguments.len() < 2 { if typed_fun.arguments.len() < 2 || typed_fun.arguments.len() > 3 {
return Err(Error::IncorrectValidatorArity { return Err(Error::IncorrectValidatorArity {
at_least: 2, count: typed_fun.arguments.len() as u32,
location: typed_fun.location, location: typed_fun.location,
}); });
} }

View File

@ -87,24 +87,6 @@ pub enum Error {
error: tipo::error::Error, error: tipo::error::Error,
}, },
#[error("Validator functions must return Bool")]
ValidatorMustReturnBool {
path: PathBuf,
src: String,
named: NamedSource,
location: Span,
},
#[error("Validator\n\n{name}\n\nrequires at least {at_least} arguments")]
WrongValidatorArity {
name: String,
at_least: u8,
location: Span,
path: PathBuf,
src: String,
named: Box<NamedSource>,
},
#[error("{name} failed{}", if *verbose { format!("\n{src}") } else { String::new() } )] #[error("{name} failed{}", if *verbose { format!("\n{src}") } else { String::new() } )]
TestFailure { TestFailure {
name: String, name: String,
@ -259,8 +241,6 @@ impl Diagnostic for Error {
Error::MissingManifest { .. } => None, Error::MissingManifest { .. } => None,
Error::TomlLoading { .. } => Some(Box::new("aiken::loading::toml")), Error::TomlLoading { .. } => Some(Box::new("aiken::loading::toml")),
Error::Format { .. } => None, Error::Format { .. } => None,
Error::ValidatorMustReturnBool { .. } => Some(Box::new("aiken::scripts")),
Error::WrongValidatorArity { .. } => Some(Box::new("aiken::validators")),
Error::TestFailure { path, .. } => Some(Box::new(path.to_str().unwrap_or(""))), Error::TestFailure { path, .. } => Some(Box::new(path.to_str().unwrap_or(""))),
Error::Http(_) => Some(Box::new("aiken::packages::download")), Error::Http(_) => Some(Box::new("aiken::packages::download")),
Error::ZipExtract(_) => None, Error::ZipExtract(_) => None,
@ -292,8 +272,6 @@ impl Diagnostic for Error {
Error::MissingManifest { .. } => Some(Box::new("Try running `aiken new <REPOSITORY/PROJECT>` to initialise a project with an example manifest.")), Error::MissingManifest { .. } => Some(Box::new("Try running `aiken new <REPOSITORY/PROJECT>` to initialise a project with an example manifest.")),
Error::TomlLoading { .. } => None, Error::TomlLoading { .. } => None,
Error::Format { .. } => None, Error::Format { .. } => None,
Error::ValidatorMustReturnBool { .. } => Some(Box::new("Try annotating the validator's return type with Bool")),
Error::WrongValidatorArity { .. } => Some(Box::new("Validators require a minimum number of arguments please add the missing arguments.\nIf you don't need one of the required arguments use an underscore `_datum`.")),
Error::TestFailure { evaluation_hint, .. } =>{ Error::TestFailure { evaluation_hint, .. } =>{
match evaluation_hint { match evaluation_hint {
None => None, None => None,
@ -366,12 +344,6 @@ impl Diagnostic for Error {
} }
} }
Error::Format { .. } => None, Error::Format { .. } => None,
Error::ValidatorMustReturnBool { location, .. } => Some(Box::new(
vec![LabeledSpan::new_with_span(None, *location)].into_iter(),
)),
Error::WrongValidatorArity { location, .. } => Some(Box::new(
vec![LabeledSpan::new_with_span(None, *location)].into_iter(),
)),
Error::TestFailure { .. } => None, Error::TestFailure { .. } => None,
Error::Http(_) => None, Error::Http(_) => None,
Error::ZipExtract(_) => None, Error::ZipExtract(_) => None,
@ -396,8 +368,6 @@ impl Diagnostic for Error {
Error::MissingManifest { .. } => None, Error::MissingManifest { .. } => None,
Error::TomlLoading { named, .. } => Some(named.deref()), Error::TomlLoading { named, .. } => Some(named.deref()),
Error::Format { .. } => None, Error::Format { .. } => None,
Error::ValidatorMustReturnBool { named, .. } => Some(named),
Error::WrongValidatorArity { named, .. } => Some(named.deref()),
Error::TestFailure { .. } => None, Error::TestFailure { .. } => None,
Error::Http(_) => None, Error::Http(_) => None,
Error::ZipExtract(_) => None, Error::ZipExtract(_) => None,
@ -422,8 +392,6 @@ impl Diagnostic for Error {
Error::MissingManifest { .. } => None, Error::MissingManifest { .. } => None,
Error::TomlLoading { .. } => None, Error::TomlLoading { .. } => None,
Error::Format { .. } => None, Error::Format { .. } => None,
Error::ValidatorMustReturnBool { .. } => None,
Error::WrongValidatorArity { .. } => None,
Error::TestFailure { .. } => None, Error::TestFailure { .. } => None,
Error::Http { .. } => None, Error::Http { .. } => None,
Error::ZipExtract { .. } => None, Error::ZipExtract { .. } => None,
@ -448,8 +416,6 @@ impl Diagnostic for Error {
Error::MissingManifest { .. } => None, Error::MissingManifest { .. } => None,
Error::TomlLoading { .. } => None, Error::TomlLoading { .. } => None,
Error::Format { .. } => None, Error::Format { .. } => None,
Error::ValidatorMustReturnBool { .. } => None,
Error::WrongValidatorArity { .. } => None,
Error::TestFailure { .. } => None, Error::TestFailure { .. } => None,
Error::Http { .. } => None, Error::Http { .. } => None,
Error::ZipExtract { .. } => None, Error::ZipExtract { .. } => None,