tickv/
error_codes.rs

1// Licensed under the Apache License, Version 2.0 or the MIT License.
2// SPDX-License-Identifier: Apache-2.0 OR MIT
3// Copyright Tock Contributors 2022.
4
5//! The standard error codes used by TicKV.
6
7/// Standard error codes.
8#[derive(Clone, Copy, Debug, PartialEq)]
9pub enum ErrorCode {
10    /// We found a header in the flash that we don't support
11    UnsupportedVersion,
12    /// Some of the data in flash appears to be corrupt
13    CorruptData,
14    /// The check sum doesn't match
15    /// Note that the value buffer is still filled
16    InvalidCheckSum,
17    /// The requested key couldn't be found
18    KeyNotFound,
19    /// Indicates that we can't add this key as one with
20    /// the same key hash already exists.
21    KeyAlreadyExists,
22    /// Indicates that the region where this object should be added
23    /// is full. In future this error should be handled internally
24    /// by allocating the object in a different region.
25    RegionFull,
26    /// Unable to add a key, the flash is full. Note that the flash
27    /// might not be full after running a garbage collection.
28    FlashFull,
29    /// Unable to read the flash region
30    ReadFail,
31    /// Unable to write the buffer to the flash address
32    WriteFail,
33    /// Unable to erase the flash region
34    EraseFail,
35    /// The object is larger then 0x7FFF
36    ObjectTooLarge,
37    /// The supplied buffer is too small.
38    /// The error code includes the total length of the value.
39    BufferTooSmall(usize),
40    /// Indicates that the flash read operation is not yet ready.
41    /// The process can be retried by calling `continue_operation()`.
42    ReadNotReady(usize),
43    /// Indicates that the flash write operation is not yet ready.
44    WriteNotReady(usize),
45    /// Indicates that the flash erase operation is not yet ready.
46    EraseNotReady(usize),
47}
48
49impl From<ErrorCode> for isize {
50    fn from(original: ErrorCode) -> isize {
51        match original {
52            ErrorCode::UnsupportedVersion => -1,
53            ErrorCode::CorruptData => -2,
54            ErrorCode::InvalidCheckSum => -3,
55            ErrorCode::KeyNotFound => -4,
56            ErrorCode::KeyAlreadyExists => -5,
57            ErrorCode::RegionFull => -6,
58            ErrorCode::FlashFull => -7,
59            ErrorCode::ReadFail => -8,
60            ErrorCode::WriteFail => -9,
61            ErrorCode::EraseFail => -10,
62            ErrorCode::ObjectTooLarge => -11,
63            ErrorCode::BufferTooSmall(_) => -12,
64            ErrorCode::ReadNotReady(_) => -13,
65            ErrorCode::WriteNotReady(_) => -14,
66            ErrorCode::EraseNotReady(_) => -15,
67        }
68    }
69}
70
71impl From<ErrorCode> for usize {
72    fn from(original: ErrorCode) -> usize {
73        isize::from(original) as usize
74    }
75}