use crate::ServiceError;
#[must_use]
pub enum TransactionOutput<T> {
Committed(T),
Aborted(ServiceError),
}
impl<T> TransactionOutput<T> {
pub fn is_committed(&self) -> bool {
if let TransactionOutput::Committed(_) = self {
true
} else {
false
}
}
pub fn is_aborted(&self) -> bool {
!self.is_committed()
}
pub fn unwrap(self) -> T {
match self {
TransactionOutput::Committed(v) => v,
TransactionOutput::Aborted(err) => panic!("Transaction was aborted: {}", err),
}
}
pub fn ok(self) -> Option<T> {
match self {
TransactionOutput::Committed(v) => Some(v),
TransactionOutput::Aborted(_) => None,
}
}
pub fn err(self) -> Option<ServiceError> {
match self {
TransactionOutput::Committed(_) => None,
TransactionOutput::Aborted(err) => Some(err),
}
}
}
impl<T> Into<Result<T, ServiceError>> for TransactionOutput<T> {
fn into(self) -> Result<T, ServiceError> {
match self {
TransactionOutput::Committed(v) => Ok(v),
TransactionOutput::Aborted(err) => Err(err),
}
}
}