tartarus-api 0.1.1

Structured API for sandboxing system (currently utilizing `bubblewrap`)
Documentation
use std::fmt::{self, Display, Formatter};
use thiserror::Error;

pub(crate) fn with_io_context(
    additional: impl Display,
    context: impl Display,
    error: impl Into<std::io::Error>,
) -> Error {
    Error {
        kind: ErrorKind::Io {
            error: error.into(),
            additional: additional.to_string(),
        },
        context: context.to_string(),
    }
}

pub type Result<T, E = Error> = std::result::Result<T, E>;

/// Represents an error that occurred when trying to configure or construct a sandbox or execute a
/// command inside it.
#[derive(Debug)]
pub struct Error {
    /// The kind of error that occurred.
    pub kind: ErrorKind,

    /// The context when the error occurred.
    pub context: String,
}

impl std::error::Error for Error {
    fn cause(&self) -> Option<&dyn std::error::Error> {
        self.kind.source()
    }
}

impl Display for Error {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        write!(f, "encountered {} during {}", self.kind, self.context)
    }
}

/// Represents the kind of error that occurred when trying to configure or construct a sandbox or
/// execute a command inside it.
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum ErrorKind {
    /// An I/O error occurred.
    #[error("{error} ({additional})")]
    Io {
        error: std::io::Error,
        additional: String,
    },

    /// An absolute path was provided when a relative one is needed.
    #[error("non-relative path {0}")]
    NonRelativePath(std::path::PathBuf),

    /// The opencode config file has invalid syntax.
    #[cfg(feature = "opencode")]
    #[error("{0}")]
    InvalidOpencodeConfigSyntax(jsonc_parser::errors::ParseError),

    /// The opencode config file has contents that don't fit the expected schema
    #[cfg(feature = "opencode")]
    #[error("{0}")]
    InvalidOpencodeConfigSchema(String),

    /// The zed config file has invalid syntax.
    #[cfg(feature = "zed")]
    #[error("{0}")]
    InvalidZedConfigSyntax(jsonc_parser::errors::ParseError),

    /// The zed config file has contents that don't fit the expected schema
    #[cfg(feature = "zed")]
    #[error("{0}")]
    InvalidZedConfigSchema(String),
}