1#[derive(thiserror::Error, Debug, Copy, Clone, PartialEq, Eq)]
2pub enum FFIErrorCode {
3 #[error("No error")]
4 None,
5 #[error("Invalid pointer")]
6 InvalidPointer,
7 #[error(
8 "Cannot deploy a contract that has not yet been created (either by a transaction in an earlier block, or by the setup_create_contract function)"
9 )]
10 DeployNonexistentContract,
11 #[error(
12 "Cannot create a mock contract for a mock contract ID that has not yet been registered (with add_contract)"
13 )]
14 CreateNonexistentMockContract,
15 #[error(
16 "Supplied data structs failed to deserialize into expected format"
17 )]
18 DeserializeError,
19 #[error("Unrecoverable error")]
20 UnrecoverableError,
21 #[error("Contract verification failed")]
22 ContractVerificationFailed,
23 #[error(
24 "An argument violated a documented precondition (e.g. a negative storage-token grant, or a mock invocation frame opened for an address that already has an active frame)"
25 )]
26 InvalidArgument,
27 #[error("Unknown FFI error code: {0}")]
28 Unknown(u32),
29}
30
31impl From<crate::bindings::TestFFIErrorCode> for FFIErrorCode {
32 fn from(error: crate::bindings::TestFFIErrorCode) -> Self {
33 match error {
34 x if x == crate::bindings::TestFFIErrorCode_kTestFFIErrorCodeNone => FFIErrorCode::None,
35 x if x == crate::bindings::TestFFIErrorCode_kTestFFIErrorCodeInvalidPointer => FFIErrorCode::InvalidPointer,
36 x if x == crate::bindings::TestFFIErrorCode_kTestFFIErrorCodeDeployNonexistentContract => FFIErrorCode::DeployNonexistentContract,
37 x if x == crate::bindings::TestFFIErrorCode_kTestFFIErrorCodeCreateNonexistentMockContract => FFIErrorCode::CreateNonexistentMockContract,
38 x if x == crate::bindings::TestFFIErrorCode_kTestFFIErrorCodeDeserializeError => FFIErrorCode::DeserializeError,
39 x if x == crate::bindings::TestFFIErrorCode_kTestFFIErrorCodeUnrecoverableError => FFIErrorCode::UnrecoverableError,
40 x if x == crate::bindings::TestFFIErrorCode_kTestFFIErrorCodeContractVerificationFailed => FFIErrorCode::ContractVerificationFailed,
41 x if x == crate::bindings::TestFFIErrorCode_kTestFFIErrorCodeInvalidArgument => FFIErrorCode::InvalidArgument,
42 other => FFIErrorCode::Unknown(other),
43 }
44 }
45}
46
47#[derive(thiserror::Error, Debug, Copy, Clone, PartialEq, Eq)]
48#[repr(u32)]
49pub enum FFILogLevel {
50 #[error("Trace logging")]
51 Trace = crate::bindings::TestFFILogLevel_kTestFFILogLevelTrace,
52 #[error("Debug logging")]
53 Debug = crate::bindings::TestFFILogLevel_kTestFFILogLevelDebug,
54 #[error("Info logging")]
55 Info = crate::bindings::TestFFILogLevel_kTestFFILogLevelInfo,
56 #[error("Warn logging")]
57 Warn = crate::bindings::TestFFILogLevel_kTestFFILogLevelWarn,
58 #[error("Error logging")]
59 Error = crate::bindings::TestFFILogLevel_kTestFFILogLevelError,
60 #[error("Critical logging")]
61 Critical = crate::bindings::TestFFILogLevel_kTestFFILogLevelCritical,
62}
63
64impl From<crate::bindings::TestFFILogLevel> for FFILogLevel {
65 fn from(level: crate::bindings::TestFFILogLevel) -> Self {
66 match level {
67 x if x == crate::bindings::TestFFILogLevel_kTestFFILogLevelTrace => FFILogLevel::Trace,
68 x if x == crate::bindings::TestFFILogLevel_kTestFFILogLevelDebug => FFILogLevel::Debug,
69 x if x == crate::bindings::TestFFILogLevel_kTestFFILogLevelInfo => FFILogLevel::Info,
70 x if x == crate::bindings::TestFFILogLevel_kTestFFILogLevelWarn => FFILogLevel::Warn,
71 x if x == crate::bindings::TestFFILogLevel_kTestFFILogLevelError => FFILogLevel::Error,
72 x if x == crate::bindings::TestFFILogLevel_kTestFFILogLevelCritical => FFILogLevel::Critical,
73 _ => panic!("unknown FFI log level: {}", level),
74 }
75 }
76}
77
78impl Drop for crate::FFIDataBuffer {
79 fn drop(&mut self) {
80 match unsafe {
81 crate::bindings::free_ffi_data_buffer(core::ptr::read(self))
82 } {
83 crate::bindings::TestFFIErrorCode_kTestFFIErrorCodeNone => (),
84 error => panic!("failed to free FFI data buffer: {}", error),
85 }
86 }
87}
88
89impl crate::FFIDataBuffer {
90 pub fn as_slice(&self) -> Option<&[u8]> {
91 if self.len == 0 {
92 return None;
93 }
94 Some(unsafe {
95 std::slice::from_raw_parts(self.data, self.len.try_into().ok()?)
96 })
97 }
98}