byteor_transport_shm/
error.rs

1//! Error types for shared-memory transport.
2
3use std::path::PathBuf;
4
5/// Errors returned by this transport.
6#[derive(Debug)]
7pub enum Error {
8    /// Filesystem/mapping I/O error.
9    Io(std::io::Error),
10
11    /// Filesystem/mapping I/O error with path context.
12    IoAt {
13        /// Operation being performed.
14        op: &'static str,
15        /// Path involved in the IO.
16        path: PathBuf,
17        /// Underlying IO error.
18        source: std::io::Error,
19    },
20
21    /// Mapped bytes are not a compatible/initialized v1 layout.
22    Layout(indexbus_core::Error),
23
24    /// Backing file is smaller than the required bytes for the chosen layout/options.
25    RegionTooSmall {
26        /// Minimum required bytes.
27        needed: usize,
28        /// Mapped bytes found.
29        found: usize,
30    },
31
32    /// Mapping base address doesn't meet the required layout alignment.
33    Misaligned {
34        /// Required alignment.
35        align: usize,
36    },
37}
38
39impl core::fmt::Display for Error {
40    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
41        match self {
42            Error::Io(e) => write!(f, "io error: {e}"),
43            Error::IoAt { op, path, source } => {
44                write!(f, "io error ({op}) at '{}': {source}", path.display())
45            }
46            Error::Layout(e) => write!(f, "layout error: {e:?}"),
47            Error::RegionTooSmall { needed, found } => {
48                write!(f, "region too small: needed={needed} found={found}")
49            }
50            Error::Misaligned { align } => write!(f, "misaligned mapping (align={align})"),
51        }
52    }
53}
54
55impl std::error::Error for Error {
56    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
57        match self {
58            Error::Io(e) => Some(e),
59            Error::IoAt { source, .. } => Some(source),
60            _ => None,
61        }
62    }
63}
64
65impl From<std::io::Error> for Error {
66    fn from(value: std::io::Error) -> Self {
67        Self::Io(value)
68    }
69}
70
71impl From<indexbus_core::Error> for Error {
72    fn from(value: indexbus_core::Error) -> Self {
73        Self::Layout(value)
74    }
75}