typed_store/rocks/
errors.rs1use bincode::ErrorKind as BincodeErrorKind;
5
6use rocksdb::Error as RocksError;
7use serde::{Deserialize, Serialize};
8use std::{fmt, fmt::Display};
9use thiserror::Error;
10use typed_store_error::TypedStoreError;
11
12#[derive(Serialize, Deserialize, Clone, Eq, PartialEq, Hash, Debug, Error)]
13#[allow(dead_code)]
14pub(crate) struct RocksErrorDef {
15 message: String,
16}
17
18impl Display for RocksErrorDef {
19 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
20 self.message.fmt(formatter)
21 }
22}
23
24#[derive(Serialize, Deserialize, Clone, Hash, Eq, PartialEq, Debug, Error)]
25#[allow(dead_code)]
26pub(crate) enum BincodeErrorDef {
27 Io(String),
28 InvalidUtf8Encoding(String),
29 InvalidBoolEncoding(u8),
30 InvalidCharEncoding,
31 InvalidTagEncoding(usize),
32 DeserializeAnyNotSupported,
33 SizeLimit,
34 SequenceMustHaveLength,
35 Custom(String),
36}
37
38impl fmt::Display for BincodeErrorDef {
39 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
40 match *self {
41 BincodeErrorDef::Io(ref ioerr) => write!(fmt, "io error: {ioerr}"),
42 BincodeErrorDef::InvalidUtf8Encoding(ref e) => {
43 write!(fmt, "{e}")
44 }
45 BincodeErrorDef::InvalidBoolEncoding(b) => {
46 write!(fmt, "expected 0 or 1, found {b}")
47 }
48 BincodeErrorDef::InvalidCharEncoding => write!(fmt, "{self:?}"),
49 BincodeErrorDef::InvalidTagEncoding(tag) => {
50 write!(fmt, "found {tag}")
51 }
52 BincodeErrorDef::SequenceMustHaveLength => write!(fmt, "{self:?}"),
53 BincodeErrorDef::SizeLimit => write!(fmt, "{self:?}"),
54 BincodeErrorDef::DeserializeAnyNotSupported => write!(
55 fmt,
56 "Bincode does not support the serde::Deserializer::deserialize_any method"
57 ),
58 BincodeErrorDef::Custom(ref s) => s.fmt(fmt),
59 }
60 }
61}
62
63impl From<bincode::Error> for BincodeErrorDef {
64 fn from(err: bincode::Error) -> Self {
65 match err.as_ref() {
66 BincodeErrorKind::Io(ioerr) => BincodeErrorDef::Io(ioerr.to_string()),
67 BincodeErrorKind::InvalidUtf8Encoding(utf8err) => {
68 BincodeErrorDef::InvalidUtf8Encoding(utf8err.to_string())
69 }
70 BincodeErrorKind::InvalidBoolEncoding(byte) => {
71 BincodeErrorDef::InvalidBoolEncoding(*byte)
72 }
73 BincodeErrorKind::InvalidCharEncoding => BincodeErrorDef::InvalidCharEncoding,
74 BincodeErrorKind::InvalidTagEncoding(tag) => BincodeErrorDef::InvalidTagEncoding(*tag),
75 BincodeErrorKind::DeserializeAnyNotSupported => {
76 BincodeErrorDef::DeserializeAnyNotSupported
77 }
78 BincodeErrorKind::SizeLimit => BincodeErrorDef::SizeLimit,
79 BincodeErrorKind::SequenceMustHaveLength => BincodeErrorDef::SequenceMustHaveLength,
80 BincodeErrorKind::Custom(str) => BincodeErrorDef::Custom(str.to_owned()),
81 }
82 }
83}
84
85pub fn typed_store_err_from_bincode_err(err: bincode::Error) -> TypedStoreError {
86 TypedStoreError::SerializationError(format!("{err}"))
87}
88
89pub fn typed_store_err_from_bcs_err(err: bcs::Error) -> TypedStoreError {
90 TypedStoreError::SerializationError(format!("{err}"))
91}
92
93pub fn typed_store_err_from_rocks_err(err: RocksError) -> TypedStoreError {
94 TypedStoreError::RocksDBError(format!("{err}"))
95}