libblobd_direct/op/
mod.rs

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