1use rocksdb::{Direction, ErrorKind, IteratorMode};
2use tuwunel_core::Result;
3
4#[inline]
5pub(crate) fn _into_direction(mode: &IteratorMode<'_>) -> Direction {
6 use Direction::{Forward, Reverse};
7 use IteratorMode::{End, From, Start};
8
9 match mode {
10 | Start | From(_, Forward) => Forward,
11 | End | From(_, Reverse) => Reverse,
12 }
13}
14
15#[inline]
16pub(crate) fn result<T>(
17 r: std::result::Result<T, rocksdb::Error>,
18) -> Result<T, tuwunel_core::Error> {
19 r.map_or_else(or_else, and_then)
20}
21
22#[inline(always)]
23pub(crate) fn and_then<T>(t: T) -> Result<T, tuwunel_core::Error> { Ok(t) }
24
25pub(crate) fn or_else<T>(e: rocksdb::Error) -> Result<T, tuwunel_core::Error> { Err(map_err(e)) }
26
27#[inline]
28pub(crate) fn is_incomplete(e: &rocksdb::Error) -> bool { e.kind() == ErrorKind::Incomplete }
29
30pub(crate) fn map_err(e: rocksdb::Error) -> tuwunel_core::Error {
31 let kind = io_error_kind(&e.kind());
32 let string = e.into_string();
33
34 std::io::Error::new(kind, string).into()
35}
36
37fn io_error_kind(e: &ErrorKind) -> std::io::ErrorKind {
38 use std::io;
39
40 match e {
41 | ErrorKind::NotFound => io::ErrorKind::NotFound,
42 | ErrorKind::Corruption => io::ErrorKind::InvalidData,
43 | ErrorKind::InvalidArgument => io::ErrorKind::InvalidInput,
44 | ErrorKind::Aborted => io::ErrorKind::Interrupted,
45 | ErrorKind::NotSupported => io::ErrorKind::Unsupported,
46 | ErrorKind::CompactionTooLarge => io::ErrorKind::FileTooLarge,
47 | ErrorKind::MergeInProgress | ErrorKind::Busy => io::ErrorKind::ResourceBusy,
48 | ErrorKind::Expired | ErrorKind::TimedOut => io::ErrorKind::TimedOut,
49 | ErrorKind::Incomplete | ErrorKind::TryAgain => io::ErrorKind::WouldBlock,
50 | ErrorKind::ColumnFamilyDropped
51 | ErrorKind::ShutdownInProgress
52 | ErrorKind::IOError
53 | ErrorKind::Unknown => io::ErrorKind::Other,
54 }
55}