Skip to main content

moonpool_sim/storage/
error.rs

1//! Storage error types for simulation operations.
2
3use crate::sim::state::FileId;
4use std::io;
5use thiserror::Error;
6
7/// Errors that can occur during simulated storage operations.
8#[derive(Debug, Clone, PartialEq, Eq, Error)]
9pub enum StorageError {
10    /// File not found at the given path.
11    #[error("file not found: {path}")]
12    NotFound {
13        /// The path that was looked up.
14        path: String,
15    },
16
17    /// File already exists at the given path.
18    #[error("file already exists: {path}")]
19    AlreadyExists {
20        /// The path that already has a file.
21        path: String,
22    },
23
24    /// File handle is no longer valid (file was deleted or never opened).
25    #[error("invalid file handle: {file_id:?}")]
26    InvalidFileHandle {
27        /// The invalid file handle.
28        file_id: FileId,
29    },
30
31    /// File has been closed.
32    #[error("file is closed: {file_id:?}")]
33    FileClosed {
34        /// The closed file handle.
35        file_id: FileId,
36    },
37
38    /// Underlying I/O error from in-memory storage.
39    #[error("I/O error on {file_id:?} ({kind:?}): {message}")]
40    Io {
41        /// The file that encountered the error.
42        file_id: FileId,
43        /// The I/O error kind.
44        kind: io::ErrorKind,
45        /// The error message.
46        message: String,
47    },
48}
49
50impl From<StorageError> for io::Error {
51    fn from(e: StorageError) -> Self {
52        let kind = match &e {
53            StorageError::NotFound { .. } => io::ErrorKind::NotFound,
54            StorageError::AlreadyExists { .. } => io::ErrorKind::AlreadyExists,
55            StorageError::InvalidFileHandle { .. } => io::ErrorKind::NotFound,
56            StorageError::FileClosed { .. } => io::ErrorKind::BrokenPipe,
57            StorageError::Io { kind, .. } => *kind,
58        };
59        io::Error::new(kind, e.to_string())
60    }
61}