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
/// Error type for the transaction.
#[derive(thiserror::Error)]
pub enum TransactionError<C: std::error::Error, P: std::error::Error> {
  /// Returned if an update function is called on a read-only transaction.
  #[error("transaction is read-only")]
  ReadOnly,

  /// Returned when a transaction conflicts with another transaction. This can
  /// happen if the read rows had been updated concurrently by another transaction.
  #[error("transaction conflict, please retry")]
  Conflict,

  /// Returned if a previously discarded transaction is re-used.
  #[error("transaction has been discarded, please create a new one")]
  Discard,

  /// Returned if too many writes are fit into a single transaction.
  #[error("transaction is too large")]
  LargeTxn,

  /// Returned if the transaction manager error occurs.
  #[error("transaction manager error: {0}")]
  Pwm(P),

  /// Returned if the conflict manager error occurs.
  #[error("conflict manager error: {0}")]
  Cm(C),
}

impl<C: std::error::Error, P: std::error::Error> core::fmt::Debug for TransactionError<C, P> {
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    match self {
      Self::ReadOnly => write!(f, "ReadOnly"),
      Self::Conflict => write!(f, "Conflict"),
      Self::Discard => write!(f, "Discard"),
      Self::LargeTxn => write!(f, "LargeTxn"),
      Self::Pwm(e) => write!(f, "Pwm({:?})", e),
      Self::Cm(e) => write!(f, "Cm({:?})", e),
    }
  }
}

impl<C: std::error::Error, P: std::error::Error> TransactionError<C, P> {
  /// Create a new error from the database error.
  #[inline]
  pub const fn conflict(err: C) -> Self {
    Self::Cm(err)
  }

  /// Create a new error from the transaction error.
  #[inline]
  pub const fn pending(err: P) -> Self {
    Self::Pwm(err)
  }
}

/// Error type for write transaction.
pub enum WtmError<C: std::error::Error, P: std::error::Error, E: std::error::Error> {
  /// Returned if the transaction error occurs.
  Transaction(TransactionError<C, P>),
  /// Returned if the write error occurs.
  Commit(E),
}

impl<C: std::error::Error, P: std::error::Error, E: std::error::Error> core::fmt::Debug
  for WtmError<C, P, E>
{
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    match self {
      Self::Transaction(e) => write!(f, "Transaction({:?})", e),
      Self::Commit(e) => write!(f, "Commit({:?})", e),
    }
  }
}

impl<C: std::error::Error, P: std::error::Error, E: std::error::Error> core::fmt::Display
  for WtmError<C, P, E>
{
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    match self {
      Self::Transaction(e) => write!(f, "transaction error: {e}"),
      Self::Commit(e) => write!(f, "commit error: {e}"),
    }
  }
}

impl<C: std::error::Error, P: std::error::Error, E: std::error::Error> std::error::Error
  for WtmError<C, P, E>
{
}

impl<C: std::error::Error, P: std::error::Error, E: std::error::Error> From<TransactionError<C, P>>
  for WtmError<C, P, E>
{
  #[inline]
  fn from(err: TransactionError<C, P>) -> Self {
    Self::Transaction(err)
  }
}

impl<C: std::error::Error, P: std::error::Error, E: std::error::Error> WtmError<C, P, E> {
  /// Create a new error from the transaction error.
  #[inline]
  pub const fn transaction(err: TransactionError<C, P>) -> Self {
    Self::Transaction(err)
  }

  /// Create a new error from the commit error.
  #[inline]
  pub const fn commit(err: E) -> Self {
    Self::Commit(err)
  }
}