use serde::Serialize;
use std::fmt;
use std::panic::Location;
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize)]
pub struct Place {
pub file: String,
pub line: u32,
pub column: u32,
}
impl Place {
pub fn new(file: impl Into<String>, line: u32, column: u32) -> Place {
Place {
file: file.into(),
line,
column,
}
}
pub(crate) fn from_location(location: &Location<'_>) -> Place {
Place::new(location.file(), location.line(), location.column())
}
#[track_caller]
pub(crate) fn caller() -> Place {
Place::from_location(Location::caller())
}
}
impl fmt::Display for Place {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}:{}:{}", self.file, self.line, self.column)
}
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
#[non_exhaustive]
pub struct Error {
pub code: &'static str,
pub message: String,
pub fix: &'static str,
pub place: Option<Place>,
}
impl Error {
pub fn new(code: &'static str, message: impl Into<String>) -> Error {
Error {
code,
message: message.into(),
fix: explain(code)
.map(|c| c.fix)
.expect("every error code is listed in CODES"),
place: None,
}
}
pub fn with_fix(code: &'static str, message: impl Into<String>, fix: &'static str) -> Error {
Error {
code,
message: message.into(),
fix,
place: None,
}
}
#[must_use]
pub fn at(mut self, place: Place) -> Error {
self.place = Some(place);
self
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if let Some(place) = &self.place {
write!(f, "{place}: ")?;
}
write!(f, "error[{}]: {}", self.code, self.message)
}
}
impl std::error::Error for Error {}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize)]
#[non_exhaustive]
pub struct Code {
pub code: &'static str,
pub when: &'static str,
pub fix: &'static str,
}
impl Code {
pub const fn new(code: &'static str, when: &'static str, fix: &'static str) -> Code {
Code { code, when, fix }
}
}
const fn code(code: &'static str, when: &'static str, fix: &'static str) -> Code {
Code::new(code, when, fix)
}
pub const CODES: &[Code] = &[
code(
"universe.empty",
"a universe with no atoms",
"give the universe at least one atom",
),
code(
"universe.duplicate_atom",
"two atoms with one name",
"give every atom of a universe its own name",
),
code(
"universe.bitwidth",
"a bitwidth outside 1 to 32 (LOGIC.md 16.1)",
"give a bitwidth from 1 to 32",
),
code(
"universe.int",
"an integer atom whose integer is outside the bitwidth's range, or two integers for one atom, or two atoms for one integer (LOGIC.md 16.1)",
"give each integer atom one integer, within the bitwidth's range, and each integer at most one atom",
),
code(
"universe.unknown_atom",
"a tuple names an atom the universe lacks",
"name only atoms of the universe, or add the atom to it",
),
code(
"universe.capacity",
"the number of tuples of an arity, n^k, is more than 2^64 - 1",
"use fewer atoms, or relations and expressions of smaller arity",
),
code(
"tuples.arity",
"a tuple set is given a tuple of another arity",
"give a tuple set only tuples of its own arity",
),
code(
"tuples.other_universe",
"a tuple set is given a tuple, or combined with a tuple set, over a different universe",
"build every tuple and tuple set from the same universe",
),
code(
"tuples.index",
"a tuple set is given an index of no tuple of its arity",
"give an index below n^k for n atoms and arity k",
),
code(
"bounds.arity",
"a bound whose arity is not the relation's",
"give the relation bounds of its own arity",
),
code(
"bounds.lower_outside_upper",
"a lower bound with a tuple the upper bound lacks",
"add the tuple to the upper bound, or remove it from the lower bound",
),
code(
"bounds.other_universe",
"a tuple set over a different universe",
"build the bounds from tuple sets over the bounds' own universe",
),
code(
"bounds.duplicate_relation",
"bounds are given for a relation that has them",
"bound each relation once",
),
code(
"bounds.duplicate_name",
"two relations of one bounds have one name",
"give each relation its own name",
),
code(
"instance.arity",
"a tuple set whose arity is not the relation's",
"give the relation a tuple set of its own arity",
),
code(
"instance.other_universe",
"a tuple set over a different universe",
"build the instance from tuple sets over its own universe",
),
code(
"instance.duplicate_relation",
"a tuple set is given for a relation that has one",
"give each relation one tuple set",
),
code(
"instance.duplicate_name",
"two relations of one instance have one name",
"give each relation its own name",
),
code(
"arity.zero",
"a relation or a tuple of arity 0",
"give it an arity of 1 or more",
),
code(
"arity.mismatch",
"an operator that needs equal arities is given unequal ones",
"make both sides the same arity, for example by joining one side with a relation",
),
code(
"arity.join",
"a join of two expressions of arity 1",
"to ask whether two sets meet, write `some (a & b)`",
),
code(
"arity.not_binary",
"transpose or closure on an expression of arity other than 2",
"apply it to a binary relation",
),
code(
"arity.not_a_set",
"the set side of a restriction, or a declaration's expression, of arity other than 1",
"use an expression of arity 1 there",
),
code(
"logic.free_variable",
"a formula with a free variable is evaluated, run, or checked",
"use the variable only inside the quantifier or comprehension that declares it",
),
code(
"logic.no_declarations",
"a quantifier or a comprehension with no declarations",
"declare at least one variable",
),
code(
"logic.rebound_variable",
"a variable is declared where it is already bound",
"declare a new variable instead of reusing one that is in scope",
),
code(
"logic.higher_order",
"a declaration over sets or relations where only atoms are declared: in a comprehension, or in a quantifier skolemization cannot remove",
"declare atoms there, or move the quantifier over sets to the outside of the formula, where it becomes a relation",
),
code(
"logic.unbound_relation",
"a formula is run or checked and uses a relation the bounds lack",
"give the relation bounds",
),
code(
"instance.missing_relation",
"a formula is evaluated and uses a relation the instance lacks",
"give the relation a tuple set in the instance",
),
code(
"trace.no_states",
"a trace with no states",
"give the trace at least one state",
),
code(
"trace.loop",
"a loop target that is not a state",
"loop back to one of the trace's states, from 0",
),
code(
"trace.relations",
"the states disagree about which changing relations they give, or give a fixed one",
"give every state the same changing relations, and fixed relations in the fixed instance",
),
code(
"enumerate.too_large",
"brute-force enumeration over more free tuples than its cap",
"shrink the upper bounds, raise the cap, or use the solver",
),
];
pub fn explain(code: &str) -> Option<&'static Code> {
CODES.iter().find(|c| c.code == code)
}