use super::types::Type;
#[must_use]
pub fn overflow_message(op: &str) -> &'static str {
match op {
"add" => "attempt to add with overflow",
"sub" => "attempt to subtract with overflow",
"mul" => "attempt to multiply with overflow",
"div" => "attempt to divide with overflow",
"rem" => "attempt to calculate the remainder with overflow",
"neg" => "attempt to negate with overflow",
other => panic!("{other} does not overflow"),
}
}
#[must_use]
pub fn zero_divisor_message(op: &str, ty: &Type) -> &'static str {
if !matches!(ty, Type::Fixed { .. }) {
return "Division by zero";
}
if op == "rem" {
"attempt to calculate the remainder with a divisor of zero"
} else {
"attempt to divide by zero"
}
}
#[must_use]
pub fn cast_message(message: Option<&str>) -> &str {
message.unwrap_or("conversion of a negative integer to a natural")
}
#[must_use]
pub fn rust_macro_message(name: &str, message: Option<&str>) -> String {
let prefix = match name {
"unreachable" => Some("internal error: entered unreachable code"),
"unimplemented" => Some("not implemented"),
"todo" => Some("not yet implemented"),
_ => None,
};
match (prefix, message) {
(prefix, None) => prefix.unwrap_or("explicit panic").to_owned(),
(None, Some(message)) => message.to_owned(),
(Some(prefix), Some(message)) => format!("{prefix}: {message}"),
}
}