1use std::ffi::NulError;
4use std::fmt::{Display, Formatter};
5
6pub type Result<T> = std::result::Result<T, Error>;
8
9#[derive(Debug)]
11pub enum Error {
12 NullHandle,
14 InvalidArgument(&'static str),
16 CString(NulError),
18 Native {
20 operation: &'static str,
22 code: i32,
24 message: String,
26 },
27 ResourceExists(String),
29 ResourceNotFound(String),
31 ResourceTypeMismatch {
33 id: String,
35 expected: &'static str,
37 actual: &'static str,
39 },
40}
41
42impl Display for Error {
43 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
44 match self {
45 Self::NullHandle => write!(f, "native runtime handle was null"),
46 Self::InvalidArgument(message) => write!(f, "invalid argument: {message}"),
47 Self::CString(err) => write!(f, "string contains interior nul byte: {err}"),
48 Self::Native {
49 operation,
50 code,
51 message,
52 } => {
53 write!(f, "{operation} failed with code {code}: {message}")
54 }
55 Self::ResourceExists(id) => write!(f, "resource already exists: {id}"),
56 Self::ResourceNotFound(id) => write!(f, "resource not found: {id}"),
57 Self::ResourceTypeMismatch {
58 id,
59 expected,
60 actual,
61 } => {
62 write!(f, "resource {id} has kind {actual}, expected {expected}")
63 }
64 }
65 }
66}
67
68impl std::error::Error for Error {}
69
70impl From<NulError> for Error {
71 fn from(value: NulError) -> Self {
72 Self::CString(value)
73 }
74}
75
76pub fn describe_return_code(code: i32) -> &'static str {
81 match code {
82 0 => "Ok",
83 1 => "Node type not recognized: the requested node kind was not registered in the runtime bridge",
84 2 => "Node not found",
85 3 => "Attempting to create a node that already exists",
86 4 => "Attempting to create a node type that already exists",
87 5 => "Invalid value type for the given node property",
88 6 => "Invalid value for the given node property",
89 7 => "Invariant violation",
90 8 => "Invalid instruction format",
91 _ => "Return code not recognized",
92 }
93}