1use super::*;
2
3
4#[derive(Debug, Clone, Copy, PartialEq, Eq)]
5pub enum ExternalError<'a> {
6 C(FfiError),
7 ActionScript(ActionScriptError<'a>),
8}
9impl<'a> ExternalError<'a> {
10 pub fn try_from (result: FREResult, thrown: Option<as3::Object<'a>>) -> Result<Self, ()> {
21 let r = result.try_into();
22 if let (Ok(Self::ActionScript(_)), Some(obj)) = (r, thrown) {
23 return Ok(ActionScriptError(obj).into());
24 }
25 r
26 }
27}
28impl From<FfiError> for ExternalError<'static> {
29 fn from(value: FfiError) -> Self {Self::C(value)}
30}
31impl<'a> From<ActionScriptError<'a>> for ExternalError<'a> {
32 fn from(value: ActionScriptError<'a>) -> Self {Self::ActionScript(value)}
33}
34impl TryFrom<FREResult> for ExternalError<'static> {
35 type Error = ();
36 fn try_from(value: FREResult) -> Result<Self, ()> {
37 FfiError::try_from(value).map(|e|{
38 if let FfiError::UnexpectedResult(v)=e && v==FREResult::FRE_ACTIONSCRIPT_ERROR {
39 ActionScriptError::IGNORED.into()
40 }else{e.into()}
41 })
42 }
43}
44impl Display for ExternalError<'_> {
45 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
46 match self {
47 Self::C(e) => Display::fmt(e, f),
48 Self::ActionScript(e) => Display::fmt(e, f),
49 }
50 }
51}
52
53
54#[derive(Debug, Clone, Copy, PartialEq, Eq)]
55pub enum FfiError {
56 NoSuchName,
57 InvalidObject,
58 TypeMismatch,
59 InvalidArgument,
60 ReadOnly,
61 WrongThread,
62 IllegalState,
63 InsufficientMemory,
64 UnexpectedResult(FREResult),
65}
66impl Display for FfiError {
67 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
68 const PREFIX: &str = "[FfiError]";
69 match *self {
70 FfiError::NoSuchName => write!(f, "{PREFIX} The name of a class, property, or method passed as a parameter does not match an ActionScript class name, property, or method."),
71 FfiError::InvalidObject => write!(f, "{PREFIX} An FREObject parameter is invalid. For examples of invalid FREObject variables, see 'FREObject validity'. https://help.adobe.com/en_US/air/extensions/WS460ee381960520ad-866f9c112aa6e1ad46-7ff9.html"),
72 FfiError::TypeMismatch => write!(f, "{PREFIX} An FREObject parameter does not represent an object of the ActionScript class expected by the called function."),
73 FfiError::InvalidArgument => write!(f, "{PREFIX} A pointer parameter is `NULL`."),
74 FfiError::ReadOnly => write!(f, "{PREFIX} The function attempted to modify a read-only property of an ActionScript object."),
75 FfiError::WrongThread => write!(f, "{PREFIX} The method was called from a thread other than the one on which the runtime has an outstanding call to a native extension function."),
76 FfiError::IllegalState => write!(f, "{PREFIX} A call was made to a native extensions C API function when the extension context was in an illegal state for that call. This return value occurs in the following situation. The context has acquired access to an ActionScript BitmapData or ByteArray class object. With one exception, the context can call no other C API functions until it releases the BitmapData or ByteArray object. The one exception is that the context can call `FREInvalidateBitmapDataRect()` after calling `FREAcquireBitmapData()` or `FREAcquireBitmapData2()`."),
77 FfiError::InsufficientMemory => write!(f, "{PREFIX} The runtime could not allocate enough memory to change the size of an Array or Vector object."),
78 FfiError::UnexpectedResult(code) => write!(f, "{PREFIX} Unexpected FREResult. ({code:#08X})"),
79 }
80 }
81}
82impl Error for FfiError {}
83impl TryFrom<FREResult> for FfiError {
84 type Error = ();
85
86 fn try_from(value: FREResult) -> Result<Self, ()> {
92 Ok(match value {
93 FREResult::FRE_OK => return Err(()),
94 FREResult::FRE_INVALID_OBJECT => Self::InvalidObject,
95 FREResult::FRE_TYPE_MISMATCH => Self::TypeMismatch,
96 FREResult::FRE_INVALID_ARGUMENT => Self::InvalidArgument,
97 FREResult::FRE_READ_ONLY => Self::ReadOnly,
98 FREResult::FRE_WRONG_THREAD => Self::WrongThread,
99 FREResult::FRE_ILLEGAL_STATE => Self::IllegalState,
100 FREResult::FRE_INSUFFICIENT_MEMORY => Self::InsufficientMemory,
101 _ => Self::UnexpectedResult(value),
102 })
103 }
104}
105
106
107#[derive(Debug, Clone, Copy, PartialEq, Eq)]
108#[repr(transparent)]
109pub struct ActionScriptError<'a> (as3::Object<'a>);
110impl<'a> ActionScriptError<'a> {
111 const IGNORED: ActionScriptError<'static> = ActionScriptError(as3::null);
112
113 pub fn thrown (self) -> as3::Object<'a> {self.0}
115}
116impl Display for ActionScriptError<'_> {
117 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
118 write!(f, "[ActionScriptError] An ActionScript error occurred, and an exception was thrown. The C API functions that can result in this error allow you to specify an FREObject to receive information about the exception.")
119 }
120}
121impl Error for ActionScriptError<'_> {}
122
123
124#[derive(Debug, Clone, Copy, PartialEq, Eq)]
125pub enum ContextError {
126 InvalidContext,
127 NullRegistry,
128 InvalidRegistry,
129 MethodsNotRegistered,
130 MethodNotFound,
131 FfiCallFailed(FfiError),
132 BorrowRegistryConflict,
133}
134impl Display for ContextError {
135 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
136 write!(f, "[ContextError] {self:?}")
137 }
138}
139impl Error for ContextError {}
140impl From<FfiError> for ContextError {
141 fn from(value: FfiError) -> Self {Self::FfiCallFailed(value)}
142}