use ariadne::Fmt;
use cas_attrs::ErrorKind;
use cas_error::EXPR;
#[derive(Debug, Clone, ErrorKind, PartialEq)]
#[error(
message = format!("cannot override builtin constant: `{}`", self.name),
labels = ["this variable"],
help = "choose a different name for this variable",
note = "builtin constants include: `i`, `e`, `phi`, `pi`, or `tau`",
)]
pub struct OverrideBuiltinConstant {
pub name: String,
}
#[derive(Debug, Clone, ErrorKind, PartialEq)]
#[error(
message = format!("cannot override builtin function: `{}`", self.name),
labels = ["this function"],
help = "choose a different name for this function",
)]
pub struct OverrideBuiltinFunction {
pub name: String,
}
#[derive(Debug, Clone, ErrorKind, PartialEq)]
#[error(
message = format!("unknown variable: `{}`", self.name),
labels = ["this variable"],
help = format!("to define it, type: {} = {}", (&self.name).fg(EXPR), "<expression>".fg(EXPR)),
)]
pub struct UnknownVariable {
pub name: String,
}
#[derive(Debug, Clone, ErrorKind, PartialEq)]
#[error(
message = format!("unknown function: `{}`", self.name),
labels = ["this function"],
help = if self.suggestions.is_empty() {
"see the documentation for a list of available functions".to_string()
} else if self.suggestions.len() == 1 {
format!("did you mean the `{}` function?", (&*self.suggestions[0]).fg(EXPR))
} else {
format!(
"did you mean one of these functions? {}",
self.suggestions
.iter()
.map(|s| format!("`{}`", s.fg(EXPR)))
.collect::<Vec<_>>()
.join(", ")
)
}
)]
pub struct UnknownFunction {
pub name: String,
pub suggestions: Vec<String>,
}
#[derive(Debug, Clone, ErrorKind, PartialEq)]
#[error(
message = format!("too many arguments given to the `{}` function", self.name),
labels = ["this function call", "", "these argument(s) are extraneous"],
help = format!(
"the `{}` function takes {} argument(s); there are {} argument(s) provided here",
(&self.name).fg(EXPR),
self.expected,
self.given
),
note = format!("function signature: `{}({})`", self.name, self.signature),
)]
pub struct TooManyArguments {
pub name: String,
pub expected: usize,
pub given: usize,
pub signature: String,
}
#[derive(Debug, Clone, ErrorKind, PartialEq)]
#[error(
message = match &*self.indices {
&[index] => format!("missing required argument #{} for the `{}` function", index + 1, self.name),
_ => format!(
"missing required arguments {} for the `{}` function",
self.indices
.iter()
.map(|i| format!("#{}", i + 1))
.collect::<Vec<_>>()
.join(", "),
self.name
),
},
labels = ["this function call", ""],
help = format!(
"the `{}` function takes {} argument(s); there are {} argument(s) provided here",
(&self.name).fg(EXPR),
self.expected,
self.given
),
note = format!("function signature: `{}({})`", self.name, self.signature),
)]
pub struct MissingArgument {
pub name: String,
pub indices: Vec<usize>,
pub expected: usize,
pub given: usize,
pub signature: String,
}