Skip to content

Commit

Permalink
chore: use let-else 🤯
Browse files Browse the repository at this point in the history
  • Loading branch information
rvcas committed Mar 17, 2023
1 parent a4c7337 commit d753b57
Showing 1 changed file with 63 additions and 65 deletions.
128 changes: 63 additions & 65 deletions crates/aiken-lang/src/tipo/infer.rs
Expand Up @@ -262,81 +262,79 @@ fn infer_definition(
let temp_params = params.iter().cloned().chain(fun.arguments);
fun.arguments = temp_params.collect();

if let Definition::Fn(mut typed_fun) = infer_definition(
let Definition::Fn(mut typed_fun) = infer_definition(
Definition::Fn(fun),
module_name,
hydrators,
environment,
tracing,
kind,
)? {
if !typed_fun.return_type.is_bool() {
return Err(Error::ValidatorMustReturnBool {
return_type: typed_fun.return_type.clone(),
location: typed_fun.location,
});
}
)? else {
unreachable!("validator definition inferred as something other than a function?")
};

let typed_params = typed_fun.arguments.drain(0..params_length).collect();
if !typed_fun.return_type.is_bool() {
return Err(Error::ValidatorMustReturnBool {
return_type: typed_fun.return_type.clone(),
location: typed_fun.location,
});
}

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

let typed_other_fun = other_fun
.map(|mut other| -> Result<TypedFunction, Error> {
let params = params.into_iter().chain(other.arguments);
other.arguments = params.collect();

if let Definition::Fn(mut other_typed_fun) = infer_definition(
Definition::Fn(other),
module_name,
hydrators,
environment,
tracing,
kind,
)? {
if !other_typed_fun.return_type.is_bool() {
return Err(Error::ValidatorMustReturnBool {
return_type: other_typed_fun.return_type.clone(),
location: other_typed_fun.location,
});
}

other_typed_fun.arguments.drain(0..params_length);

if other_typed_fun.arguments.len() < 2
|| other_typed_fun.arguments.len() > 3
{
return Err(Error::IncorrectValidatorArity {
count: other_typed_fun.arguments.len() as u32,
location: other_typed_fun.location,
});
}

Ok(other_typed_fun)
} else {
unreachable!(
"validator definition inferred as something other than a function?"
)
}
})
.transpose();

Ok(Definition::Validator(Validator {
doc,
end_position,
fun: typed_fun,
other_fun: typed_other_fun?,
location,
params: typed_params,
}))
} else {
unreachable!("validator definition inferred as something other than a function?")
if typed_fun.arguments.len() < 2 || typed_fun.arguments.len() > 3 {
return Err(Error::IncorrectValidatorArity {
count: typed_fun.arguments.len() as u32,
location: typed_fun.location,
});
}

let typed_other_fun = other_fun
.map(|mut other| -> Result<TypedFunction, Error> {
let params = params.into_iter().chain(other.arguments);
other.arguments = params.collect();

let Definition::Fn(mut other_typed_fun) = infer_definition(
Definition::Fn(other),
module_name,
hydrators,
environment,
tracing,
kind,
)? else {
unreachable!(
"validator definition inferred as something other than a function?"
)
};

if !other_typed_fun.return_type.is_bool() {
return Err(Error::ValidatorMustReturnBool {
return_type: other_typed_fun.return_type.clone(),
location: other_typed_fun.location,
});
}

other_typed_fun.arguments.drain(0..params_length);

if other_typed_fun.arguments.len() < 2 || other_typed_fun.arguments.len() > 3 {
return Err(Error::IncorrectValidatorArity {
count: other_typed_fun.arguments.len() as u32,
location: other_typed_fun.location,
});
}

Ok(other_typed_fun)
})
.transpose();

Ok(Definition::Validator(Validator {
doc,
end_position,
fun: typed_fun,
other_fun: typed_other_fun?,
location,
params: typed_params,
}))
}

Definition::Test(f) => {
Expand Down

0 comments on commit d753b57

Please sign in to comment.