foundationdb/recipes/ranked_register/
errors.rs

1// Copyright 2024 foundationdb-rs developers
2//
3// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
4// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
5// http://opensource.org/licenses/MIT>, at your option. This file may not be
6// copied, modified, or distributed except according to those terms.
7
8//! Error types for the ranked register
9
10use crate::tuple::PackError;
11use crate::FdbError;
12use std::fmt;
13
14/// Ranked register specific errors
15#[derive(Debug)]
16pub enum RankedRegisterError {
17    /// Database error
18    Fdb(FdbError),
19    /// Serialization error
20    PackError(PackError),
21    /// Invalid state encountered in the register
22    InvalidState(String),
23}
24
25impl fmt::Display for RankedRegisterError {
26    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
27        match self {
28            Self::Fdb(e) => write!(f, "Database error: {e}"),
29            Self::PackError(e) => write!(f, "Pack error: {e:?}"),
30            Self::InvalidState(msg) => write!(f, "Invalid state: {msg}"),
31        }
32    }
33}
34
35impl std::error::Error for RankedRegisterError {}
36
37impl From<FdbError> for RankedRegisterError {
38    fn from(error: FdbError) -> Self {
39        Self::Fdb(error)
40    }
41}
42
43impl From<PackError> for RankedRegisterError {
44    fn from(error: PackError) -> Self {
45        Self::PackError(error)
46    }
47}
48
49/// Result type for ranked register operations
50pub type Result<T> = std::result::Result<T, RankedRegisterError>;