injectum/
error.rs

1//! Centralized error handling types for the library.
2//!
3//! This module leverages the `thiserror` crate to provide a unified [`Error`] enum
4//! that aggregates low-level OS failures (IO, Win32), logical inconsistencies
5//! (Validation, Mismatch), and runtime execution errors.
6
7/// A convenience alias for `Result<T, Error>`.
8pub type Result<T> = std::result::Result<T, Error>;
9
10/// The exhaustive list of failure modes for the injection lifecycle.
11#[derive(Debug, thiserror::Error)]
12pub enum Error {
13    /// Wraps standard Input/Output failures (e.g., file not found, permission denied).
14    #[error("{0}")]
15    Io(#[from] std::io::Error),
16
17    /// A raw Operating System API failure.
18    ///
19    /// Contains the name of the failed function and the raw error code (decimal).
20    #[error("Win32 API '{0}' failed with error code: {1}")]
21    Win32(&'static str, u32),
22
23    /// The configuration or builder arguments are invalid (e.g., missing required fields).
24    #[error("Validation error: {0}")]
25    Validation(String),
26
27    /// The payload binary is malformed or does not match the expected format.
28    ///
29    /// (e.g., Missing PE magic bytes, invalid sections).
30    #[error("Invalid image format: {0}")]
31    InvalidImage(String),
32
33    /// A logical error where the Strategy and Payload are incompatible.
34    ///
35    /// (e.g., Attempting 'Process Hollowing' using a raw 'Shellcode' payload)
36    #[error("Strategy '{strategy}' incompatible with payload type '{variant}'")]
37    Mismatch {
38        strategy: &'static str,
39        variant: &'static str,
40    },
41
42    /// The requested functionality is disabled via Cargo features or not supported on this OS.
43    #[error("Capability unavailable: {0}")]
44    Unsupported(String),
45
46    /// A generic runtime failure not covered by specific variants.
47    #[error("Execution failed: {0}")]
48    Execution(String),
49}
50
51// Helper: Enables usage of `?` on String to convert automatically to Error::Execution.
52impl From<String> for Error {
53    fn from(s: String) -> Self {
54        Error::Execution(s)
55    }
56}
57
58// Helper: Enables usage of `?` on &str to convert automatically to Error::Execution.
59impl From<&str> for Error {
60    fn from(s: &str) -> Self {
61        Error::Execution(s.to_string())
62    }
63}