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]
20pub(crate) fn result<T>(
21 r: std::result::Result<T, rocksdb::Error>,
22) -> Result<T, tuwunel_core::Error> {
23 r.map_or_else(or_else, and_then)
24}
25
26#[inline(always)]
27pub(crate) fn and_then<T>(t: T) -> Result<T, tuwunel_core::Error> { Ok(t) }
28
29pub(crate) fn or_else<T>(e: rocksdb::Error) -> Result<T, tuwunel_core::Error> { Err(map_err(e)) }
30
31#[inline]
36pub(crate) fn is_incomplete(e: &rocksdb::Error) -> bool { e.kind() == ErrorKind::Incomplete }
37
38pub(crate) fn map_err(e: rocksdb::Error) -> tuwunel_core::Error {
43 let kind = io_error_kind(&e.kind());
44 let string = e.into_string();
45
46 std::io::Error::new(kind, string).into()
47}
48
49fn io_error_kind(e: &ErrorKind) -> std::io::ErrorKind {
50 use std::io;
51
52 match e {
53 | ErrorKind::NotFound => io::ErrorKind::NotFound,
54 | ErrorKind::Corruption => io::ErrorKind::InvalidData,
55 | ErrorKind::InvalidArgument => io::ErrorKind::InvalidInput,
56 | ErrorKind::Aborted => io::ErrorKind::Interrupted,
57 | ErrorKind::NotSupported => io::ErrorKind::Unsupported,
58 | ErrorKind::CompactionTooLarge => io::ErrorKind::FileTooLarge,
59 | ErrorKind::MergeInProgress | ErrorKind::Busy => io::ErrorKind::ResourceBusy,
60 | ErrorKind::Expired | ErrorKind::TimedOut => io::ErrorKind::TimedOut,
61 | ErrorKind::Incomplete | ErrorKind::TryAgain => io::ErrorKind::WouldBlock,
62 | ErrorKind::ColumnFamilyDropped
63 | ErrorKind::ShutdownInProgress
64 | ErrorKind::IOError
65 | ErrorKind::Unknown => io::ErrorKind::Other,
66 }
67}