30 lines
1.1 KiB
Rust
30 lines
1.1 KiB
Rust
//! Errors as values. The core never panics on bad input; it returns a `Fault`.
|
|
|
|
use std::fmt;
|
|
|
|
#[derive(Clone, Debug, PartialEq)]
|
|
pub enum Fault {
|
|
/// The line is not valid JSON.
|
|
Syntax(String),
|
|
/// A required field is absent.
|
|
Missing(String),
|
|
/// A field is present but has the wrong type or an out-of-domain value.
|
|
Invalid { field: String, detail: String },
|
|
/// Fields are individually valid but contradict each other.
|
|
Inconsistent(String),
|
|
/// Not enough cards or combinations left to compute anything.
|
|
Exhausted(String),
|
|
}
|
|
|
|
impl fmt::Display for Fault {
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
match self {
|
|
Fault::Syntax(detail) => write!(f, "syntax: {detail}"),
|
|
Fault::Missing(field) => write!(f, "missing field: {field}"),
|
|
Fault::Invalid { field, detail } => write!(f, "invalid {field}: {detail}"),
|
|
Fault::Inconsistent(detail) => write!(f, "inconsistent: {detail}"),
|
|
Fault::Exhausted(detail) => write!(f, "exhausted: {detail}"),
|
|
}
|
|
}
|
|
}
|