Make errors nice

This commit is contained in:
Jeff 2024-07-12 16:16:28 -04:00
parent c47d09fd1d
commit 2dbbc7b128
3 changed files with 19 additions and 26 deletions

View File

@ -122,34 +122,14 @@ impl AbstractNode for ValueNode {
return Err(ValidationError::WrongTypeArgumentsCount {
expected: parameters.len(),
actual: arguments.len(),
position: arguments.last().unwrap().position(),
});
}
let mut arguments = arguments.iter();
let mut found = false;
for (identifier, content) in variants {
if identifier == variant.node {
found = true;
}
if let Some(content) = content {
for expected_type in &content {
if let Type::Generic {
concrete_type: None,
..
} = expected_type
{
arguments.next().ok_or_else(|| {
ValidationError::WrongTypeArgumentsCount {
expected: content.len(),
actual: arguments.len(),
}
})?;
}
}
}
}
let found = variants
.into_iter()
.find(|(identifier, _)| identifier == &variant.node)
.is_some();
if !found {
return Err(ValidationError::EnumVariantNotFound {

View File

@ -184,6 +184,7 @@ pub enum ValidationError {
WrongTypeArgumentsCount {
expected: usize,
actual: usize,
position: SourcePosition,
},
}

View File

@ -445,7 +445,19 @@ impl InterpreterError {
.add_label(Label::new((self.source_id.clone(), 0..0)).with_message(reason)),
ValidationError::CannotUsePath(_) => todo!(),
ValidationError::Uninitialized => todo!(),
ValidationError::WrongTypeArgumentsCount { expected, actual } => todo!(),
ValidationError::WrongTypeArgumentsCount {
expected,
actual,
position,
} => builder.add_label(
Label::new((self.source_id.clone(), position.0..position.1)).with_message(
format!(
"Expected {} type arguments but got {}.",
expected.fg(type_color),
actual.fg(type_color)
),
),
),
}
}