metacall/types/
metacall_error.rs

1use super::MetaCallValue;
2use std::{
3    ffi::{c_int, NulError},
4    fmt,
5    path::PathBuf,
6};
7
8#[derive(Debug, Clone)]
9/// This error happens when it's not possible to initialize the MetaCall core. You can check
10/// your logs for more information.
11pub struct MetaCallInitError(c_int);
12impl MetaCallInitError {
13    #[doc(hidden)]
14    pub fn new(code: c_int) -> Self {
15        Self(code)
16    }
17}
18impl fmt::Display for MetaCallInitError {
19    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
20        write!(f, "Failed to initialize MetaCall with code: {}", self.0)
21    }
22}
23
24#[derive(Debug, Clone)]
25/// This error may happen when passing contains a null character. You can access the
26/// original string and the NulError throughout this struct.
27pub struct MetaCallStringConversionError {
28    pub original_string: String,
29    pub nul_error: NulError,
30}
31impl MetaCallStringConversionError {
32    #[doc(hidden)]
33    pub fn new(original_string: impl ToString, nul_error: NulError) -> Self {
34        Self {
35            original_string: original_string.to_string(),
36            nul_error,
37        }
38    }
39}
40impl fmt::Display for MetaCallStringConversionError {
41    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
42        write!(
43            f,
44            "Failed to convert string: {}",
45            self.original_string.clone()
46        )
47    }
48}
49
50#[derive(Debug, Clone)]
51/// This error may happen when trying to call a function.
52pub enum MetaCallError {
53    /// Function not found.
54    FunctionNotFound,
55    /// Failed to cast the return type as the type requested.
56    FailedCasting(Box<dyn MetaCallValue>),
57    /// Null character detected.
58    UnexpectedCStringConversionErr(MetaCallStringConversionError),
59}
60
61#[derive(Debug, Clone)]
62/// This error may happen when trying to set a class/object attribute. Check your logs
63/// if you get `SetAttributeFailure` error variant.
64pub enum MetaCallSetAttributeError {
65    /// Failed to set the attribute.
66    SetAttributeFailure,
67    /// Null character detected.
68    UnexpectedCStringConversionErr(MetaCallStringConversionError),
69}
70
71#[derive(Debug, Clone)]
72/// This error may happen when trying to get a class/object attribute.
73pub enum MetaCallGetAttributeError {
74    /// Failed to cast the attribute as the type requested.
75    FailedCasting(Box<dyn MetaCallValue>),
76    /// Null character detected.
77    UnexpectedCStringConversionErr(MetaCallStringConversionError),
78}
79
80#[derive(Debug, Clone)]
81/// This error may happen when loading a code. Check your logs for more information if you
82/// get `FromFileFailure` or `FromMemoryFailure` error variant.
83pub enum MetaCallLoaderError {
84    /// File not found.
85    FileNotFound(PathBuf),
86    /// Failed to load from file.
87    FromFileFailure,
88    /// Failed to load from memory.
89    FromMemoryFailure,
90    /// Not a file or permission denied.
91    NotAFileOrPermissionDenied(PathBuf),
92    /// Null character detected.
93    UnexpectedCStringConversionErr(MetaCallStringConversionError),
94}
95
96#[derive(Debug, Clone)]
97/// This error may happen when trying to get a class by its name.
98pub enum MetaCallClassFromNameError {
99    /// Class not found.
100    ClassNotFound,
101    /// Null character detected.
102    UnexpectedCStringConversionErr(MetaCallStringConversionError),
103}