1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
use abscissa_core::error::{BoxError, Context};
use std::fmt::{self, Display};
use thiserror::Error;
#[derive(Debug)]
pub struct StateError(pub(crate) Box<Context<StateErrorKind>>);
#[derive(Copy, Clone, Debug, Error, Eq, PartialEq)]
pub enum StateErrorKind {
#[error("height regression")]
HeightRegression,
#[error("step regression")]
StepRegression,
#[error("round regression")]
RoundRegression,
#[error("double sign detected")]
DoubleSign,
#[error("error syncing state to disk")]
SyncError,
}
impl StateErrorKind {
pub fn context(self, source: impl Into<BoxError>) -> Context<StateErrorKind> {
Context::new(self, Some(source.into()))
}
}
impl StateError {
pub fn kind(&self) -> StateErrorKind {
*self.0.kind()
}
}
impl From<StateErrorKind> for StateError {
fn from(kind: StateErrorKind) -> Self {
Context::new(kind, None).into()
}
}
impl From<Context<StateErrorKind>> for StateError {
fn from(context: Context<StateErrorKind>) -> Self {
StateError(Box::new(context))
}
}
impl Display for StateError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
impl std::error::Error for StateError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
self.0.source()
}
}