|
| 1 | +use std::sync::Arc; |
| 2 | + |
| 3 | +use super::abi_value::ABIValue; |
| 4 | +use algokit_abi::ABIType as RustABIType; |
| 5 | + |
| 6 | +mod address; |
| 7 | +mod bool; |
| 8 | +mod byte; |
| 9 | +mod dynamic_array; |
| 10 | +mod static_array; |
| 11 | +mod string; |
| 12 | +mod tuple; |
| 13 | +mod ufixed; |
| 14 | +mod uint; |
| 15 | + |
| 16 | +// TODO: remove unwraps in this module |
| 17 | + |
| 18 | +/// This trait is used to convert FFI ABI types to Rust ABI types |
| 19 | +/// This is needed because we can't implement From on Arc<dyn ABIType> |
| 20 | +pub trait FfiToRustABIType { |
| 21 | + /// Convert the FFI ABI type to a Rust ABI type |
| 22 | + fn to_rust_abi_type(&self) -> RustABIType; |
| 23 | +} |
| 24 | + |
| 25 | +/// This trait is used to convert Rust ABI types to FFI ABI types |
| 26 | +/// This is needed because we can't implement From on Arc<dyn ABIType> |
| 27 | +trait RustToFfiABIType { |
| 28 | + fn to_ffi_abi_type(&self) -> Arc<dyn ABIType>; |
| 29 | +} |
| 30 | + |
| 31 | +impl RustToFfiABIType for RustABIType { |
| 32 | + fn to_ffi_abi_type(&self) -> Arc<dyn ABIType> { |
| 33 | + let abi_type: Arc<dyn ABIType> = match self { |
| 34 | + RustABIType::Uint(_) => Arc::new(uint::ABIUint::from(self.clone())), |
| 35 | + RustABIType::Tuple(_) => Arc::new(tuple::ABITuple::from(self.clone())), |
| 36 | + RustABIType::UFixed(_, _) => Arc::new(ufixed::ABIUfixed::from(self.clone())), |
| 37 | + RustABIType::String => Arc::new(string::ABIString::from(self.clone())), |
| 38 | + RustABIType::Byte => Arc::new(byte::ABIByte::from(self.clone())), |
| 39 | + RustABIType::Bool => Arc::new(bool::ABIBool::from(self.clone())), |
| 40 | + RustABIType::DynamicArray(_) => { |
| 41 | + Arc::new(dynamic_array::ABIDynamicArray::from(self.clone())) |
| 42 | + } |
| 43 | + RustABIType::StaticArray(_, _) => { |
| 44 | + Arc::new(static_array::ABIStaticArray::from(self.clone())) |
| 45 | + } |
| 46 | + RustABIType::Address => Arc::new(address::ABIAddress::from(self.clone())), |
| 47 | + }; |
| 48 | + abi_type |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +#[uniffi::export] |
| 53 | +pub trait ABIType: Send + Sync + FfiToRustABIType { |
| 54 | + fn decoode(&self, data: &[u8]) -> ABIValue { |
| 55 | + let rust_abi_type = self.to_rust_abi_type(); |
| 56 | + ABIValue::from(rust_abi_type.decode(data).unwrap()) |
| 57 | + } |
| 58 | + |
| 59 | + fn encode(&self, value: ABIValue) -> Vec<u8> { |
| 60 | + let rust_abi_type = self.to_rust_abi_type(); |
| 61 | + rust_abi_type.encode(&value.into()).unwrap() |
| 62 | + } |
| 63 | +} |
0 commit comments