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
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
use thiserror::Error;

use crate::append::AppendError;
use crate::error::AcceptError;
use crate::error::AffirmSnapshotError;
use crate::error::CommitError;
use crate::error::InstallSnapshotError;
use crate::error::PrepareError;
use crate::error::PrepareSnapshotError;
use crate::error::ReadStaleError;
use crate::invocation::Invocation;
use crate::invocation::RoundNumOf;
use crate::RoundNum;

pub use crate::error::ShutDown;

impl ShutDown {
    pub(super) fn rule_out<T>(result: Result<T, ShutDown>) -> T {
        result.unwrap()
    }
}

impl<I: Invocation> From<ShutDown> for AppendError<I> {
    fn from(_: ShutDown) -> Self {
        AppendError::ShutDown
    }
}

impl From<ShutDown> for ReadStaleError {
    fn from(_: ShutDown) -> Self {
        Self::ShutDown
    }
}

impl<I: Invocation> From<ShutDown> for PrepareError<I> {
    fn from(_: ShutDown) -> Self {
        Self::ShutDown
    }
}

impl<I: Invocation> From<ShutDown> for AcceptError<I> {
    fn from(_: ShutDown) -> Self {
        Self::ShutDown
    }
}

impl<I: Invocation> From<ShutDown> for CommitError<I> {
    fn from(_: ShutDown) -> Self {
        Self::ShutDown
    }
}

impl From<ShutDown> for AcquireRoundNumError {
    fn from(_: ShutDown) -> Self {
        Self::ShutDown
    }
}

impl<R: RoundNum> From<ShutDown> for ClusterError<R> {
    fn from(_: ShutDown) -> Self {
        Self::ShutDown
    }
}

impl From<ShutDown> for PrepareSnapshotError {
    fn from(_: ShutDown) -> Self {
        Self::ShutDown
    }
}

impl From<ShutDown> for AffirmSnapshotError {
    fn from(_: ShutDown) -> Self {
        Self::ShutDown
    }
}

impl From<ShutDown> for InstallSnapshotError {
    fn from(_: ShutDown) -> Self {
        Self::ShutDown
    }
}

#[derive(Debug, Error)]
pub enum AcquireRoundNumError {
    #[error("the round has already converged")]
    Converged,

    #[error("node is disoriented")]
    Disoriented,

    #[error("node is shut down")]
    ShutDown,
}

impl<I: Invocation> From<AcquireRoundNumError> for AppendError<I> {
    fn from(e: AcquireRoundNumError) -> Self {
        match e {
            AcquireRoundNumError::Converged => AppendError::Converged { caught_up: true },
            AcquireRoundNumError::Disoriented => AppendError::Disoriented,
            AcquireRoundNumError::ShutDown => AppendError::ShutDown,
        }
    }
}

#[derive(Debug, Error)]
pub enum ClusterError<R: RoundNum> {
    #[error("the round has already converged")]
    Converged(R),

    #[error("node is disoriented")]
    Disoriented,

    #[error("node is shut down")]
    ShutDown,
}

impl<I: Invocation> From<ClusterError<RoundNumOf<I>>> for AppendError<I> {
    fn from(e: ClusterError<RoundNumOf<I>>) -> Self {
        match e {
            ClusterError::Disoriented => AppendError::Disoriented,
            ClusterError::Converged(_) => AppendError::Converged { caught_up: true },
            ClusterError::ShutDown => AppendError::ShutDown,
        }
    }
}