Skip to main content

rustduck_core/
error.rs

1use thiserror::Error;
2
3/// The native exception category captured at the `rustduck-sys` boundary.
4#[derive(Debug, Clone, Copy, PartialEq, Eq)]
5#[non_exhaustive]
6pub enum NativeExceptionKind {
7    /// A native `std::exception` was caught.
8    Standard,
9    /// A native exception outside the `std::exception` hierarchy was caught.
10    Unknown,
11}
12
13/// Errors reported by the safe Rust facade.
14#[derive(Debug, Error)]
15#[non_exhaustive]
16pub enum Error {
17    /// A caller supplied a value outside this facade's valid contract.
18    #[error("invalid value for {field}: {message}")]
19    InvalidArgument {
20        /// The validated argument name.
21        field: &'static str,
22        /// A controlled description of the violated constraint.
23        message: String,
24    },
25
26    /// An operation is invalid for the object's current lifecycle state.
27    #[error("{object} cannot {operation} while it is {state}")]
28    InvalidState {
29        /// The affected object type.
30        object: &'static str,
31        /// The requested operation.
32        operation: &'static str,
33        /// The object's current state.
34        state: &'static str,
35    },
36
37    /// A native operation reported its documented normal failure result.
38    #[error("native operation {operation} failed")]
39    NativeFailure {
40        /// Stable identifier for the failed operation.
41        operation: &'static str,
42    },
43
44    /// A native operation threw an exception that was contained at the boundary.
45    #[error("native operation {operation} raised {kind:?}")]
46    NativeException {
47        /// Stable identifier for the failed operation.
48        operation: &'static str,
49        /// The caught exception category.
50        kind: NativeExceptionKind,
51    },
52
53    /// A memory endpoint was closed before accepting or producing data.
54    #[error("{endpoint} is closed")]
55    QueueClosed {
56        /// Stable endpoint identifier.
57        endpoint: &'static str,
58    },
59
60    /// A memory endpoint was aborted.
61    #[error("{endpoint} was aborted")]
62    QueueAborted {
63        /// Stable endpoint identifier.
64        endpoint: &'static str,
65    },
66
67    /// The private memory callback protocol was violated.
68    #[error("memory callback protocol failed: {operation}")]
69    CallbackProtocol {
70        /// Stable protocol operation identifier.
71        operation: &'static str,
72    },
73}
74
75pub(crate) fn native_exception(operation: &'static str, error: cxx::Exception) -> Error {
76    let kind = match error.what() {
77        "rustduck:unknown-exception" => NativeExceptionKind::Unknown,
78        _ => NativeExceptionKind::Standard,
79    };
80    Error::NativeException { operation, kind }
81}