libblobd_lite/op/
mod.rs

1use std::error::Error;
2use std::fmt::Display;
3use std::fmt::Write;
4
5pub mod commit_object;
6pub mod create_object;
7pub mod delete_object;
8pub mod inspect_object;
9pub mod read_object;
10pub mod write_object;
11
12pub type OpResult<T> = Result<T, OpError>;
13
14#[derive(Debug)]
15pub enum OpError {
16  DataStreamError(Box<dyn Error + Send + Sync>),
17  DataStreamLengthMismatch,
18  InexactWriteLength,
19  ObjectMetadataTooLarge,
20  ObjectNotFound,
21  RangeOutOfBounds,
22  UnalignedWrite,
23}
24
25impl Display for OpError {
26  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
27    match self {
28      OpError::DataStreamError(e) => {
29        write!(f, "an error occurred while reading the input data: {e}")
30      }
31      OpError::DataStreamLengthMismatch => write!(
32        f,
33        "the input data stream contains more or less bytes than specified"
34      ),
35      OpError::InexactWriteLength => write!(f, "data to write is not an exact chunk"),
36      OpError::ObjectMetadataTooLarge => write!(f, "object metadata is too large"),
37      OpError::ObjectNotFound => write!(f, "object does not exist"),
38      OpError::RangeOutOfBounds => write!(f, "requested range to read or write is invalid"),
39      OpError::UnalignedWrite => {
40        write!(f, "data to write does not start at a multiple of TILE_SIZE")
41      }
42    }
43  }
44}
45
46impl Error for OpError {}
47
48pub(crate) fn key_debug_str(key: &[u8]) -> String {
49  std::str::from_utf8(key)
50    .map(|k| format!("lit:{k}"))
51    .unwrap_or_else(|_| {
52      let mut nice = "hex:".to_string();
53      for (i, b) in key.iter().enumerate() {
54        write!(nice, "{:02x}", b).unwrap();
55        if i == 12 {
56          nice.push('…');
57          break;
58        };
59      }
60      write!(nice, " ({})", key.len()).unwrap();
61      nice
62    })
63}