plugmem-host 0.1.4

Native host layer for plugmem: file storage with locking, Embedder trait and HTTP embedder implementations.
Documentation
//! Host-layer errors.

use std::path::PathBuf;

/// Every way the host layer can fail. Engine failures pass through as
/// [`HostError::Engine`]; everything filesystem- or network-shaped is
/// typed here.
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum HostError {
    /// The database file is exclusively locked by another process (or
    /// another handle in this process). One file has one owner — open a
    /// different file, or drop the other handle.
    #[error("database at {} is locked by another process", path.display())]
    Locked {
        /// The database base path.
        path: PathBuf,
    },

    /// A filesystem operation failed.
    #[error("i/o on {}: {source}", path.display())]
    Io {
        /// The file the operation touched.
        path: PathBuf,
        /// The underlying error.
        #[source]
        source: std::io::Error,
    },

    /// A read-only open ([`crate::Database::open_readonly`]) found a
    /// non-empty journal. Replaying it would mutate the engine — copying
    /// whole arenas up from the mapped bytes (copy-on-write) — which
    /// defeats the zero-copy intent. Open the database read-write once to
    /// checkpoint it (fold the journal into the snapshot), then retry
    #[error("database at {} needs a checkpoint before a read-only open (non-empty journal)", path.display())]
    NeedsCheckpoint {
        /// The database base path.
        path: PathBuf,
    },

    /// The engine returned a typed error.
    #[error(transparent)]
    Engine(#[from] plugmem_core::Error),

    /// The embedder transport or response was unusable (the message
    /// names what exactly: status, dimension mismatch, malformed JSON).
    #[error("embedder: {0}")]
    Embed(String),
}

impl HostError {
    /// Shorthand for wrapping an I/O error with its path.
    pub(crate) fn io(path: &std::path::Path, source: std::io::Error) -> Self {
        Self::Io {
            path: path.to_path_buf(),
            source,
        }
    }
}