use unitype_core::QualifiedName;
use unitype_core::value::ValueError;
#[derive(thiserror::Error, Debug)]
pub enum ExecError {
#[error("could not reach the `{environment}` environment: {detail}")]
Unreachable {
environment: String,
detail: String,
},
#[error("`{id}` did not answer within {millis}ms")]
Timeout {
id: String,
millis: u64,
},
#[error("`{id}` used up its budget of {budget} before answering")]
Exhausted {
id: String,
budget: u64,
},
#[error("`{id}` failed: {message}")]
Failed {
id: String,
message: String,
},
#[error("`{id}` broke the protocol: {detail}")]
Protocol {
id: String,
detail: String,
},
#[error("`{0}` has no body registered in this process")]
NotRegistered(String),
#[error("`{0}` has nothing bound to it")]
Unbound(String),
#[error("`{value}` has no field `{field}`")]
NoSuchField {
value: String,
field: String,
},
#[error("{0}")]
Untyped(String),
#[error("`{0}` is not a signature in this workspace")]
UnknownSignature(QualifiedName),
#[error("evaluation did not terminate")]
TooDeep,
#[error("`{op}` overflowed: the result is not a number this system can carry")]
Overflow {
op: String,
},
}
impl ExecError {
pub fn is_environmental(&self) -> bool {
matches!(
self,
ExecError::Unreachable { .. } | ExecError::Timeout { .. } | ExecError::Exhausted { .. }
)
}
}
#[derive(thiserror::Error, Debug)]
pub enum DispatchError {
#[error("`{0}` is not a signature in this workspace")]
UnknownSignature(QualifiedName),
#[error("nothing proves `{0}`")]
NoImplementation(QualifiedName),
#[error("nothing that proves `{sig}` can run here: {}", .skipped.join("; "))]
NoneAvailable {
sig: QualifiedName,
skipped: Vec<String>,
},
#[error("`{id}` cannot be used: {reason}")]
Unusable {
id: String,
reason: String,
},
#[error("argument {index} of `{sig}`: {}", .errors.iter().map(ToString::to_string).collect::<Vec<_>>().join("; "))]
BadArgument {
sig: QualifiedName,
index: usize,
errors: Vec<ValueError>,
},
#[error("`{sig}` takes {expected} type argument(s), but {got} were given")]
TypeArity {
sig: QualifiedName,
expected: usize,
got: usize,
},
#[error("`{sig}` takes `{param}` only where it is a `{bound}`, and `{arg}` is not")]
BoundNotMet {
sig: QualifiedName,
param: unitype_core::Ident,
arg: String,
bound: String,
},
#[error("`{sig}` takes {expected} argument(s), but {got} were given")]
Arity {
sig: QualifiedName,
expected: usize,
got: usize,
},
#[error("`{id}` claims to prove `{sig}` but returned something else: {}", .errors.iter().map(ToString::to_string).collect::<Vec<_>>().join("; "))]
BadResult {
id: String,
sig: QualifiedName,
errors: Vec<ValueError>,
},
#[error("`{id}`: {source}")]
Exec {
id: String,
#[source]
source: ExecError,
},
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_failure_of_the_environment_is_worth_trying_elsewhere() {
assert!(
ExecError::Unreachable {
environment: "python3".into(),
detail: "not installed".into(),
}
.is_environmental()
);
assert!(
ExecError::Timeout {
id: "slow".into(),
millis: 100
}
.is_environmental()
);
}
#[test]
fn a_failure_of_the_proof_is_not() {
assert!(
!ExecError::Failed {
id: "wrong".into(),
message: "divided by zero".into(),
}
.is_environmental(),
"asking someone else will not make this proof correct"
);
assert!(
!ExecError::Protocol {
id: "confused".into(),
detail: "sent nothing".into(),
}
.is_environmental()
);
}
}