1use std::fmt::Debug;
8
9use serde::{de::DeserializeOwned, Serialize};
10
11pub trait Abi: ContractAbi + ServiceAbi {}
15impl<T> Abi for T where T: ContractAbi + ServiceAbi {}
19
20pub trait ContractAbi {
23 type Operation: Serialize + DeserializeOwned + Send + Sync + Debug + 'static;
29
30 type Response: Serialize + DeserializeOwned + Send + Sync + Debug + 'static;
32
33 fn deserialize_operation(operation: Vec<u8>) -> Result<Self::Operation, String> {
35 bcs::from_bytes(&operation)
36 .map_err(|e| format!("BCS deserialization error {e:?} for operation {operation:?}"))
37 }
38
39 fn serialize_operation(operation: &Self::Operation) -> Result<Vec<u8>, String> {
41 bcs::to_bytes(operation)
42 .map_err(|e| format!("BCS serialization error {e:?} for operation {operation:?}"))
43 }
44
45 fn deserialize_response(response: Vec<u8>) -> Result<Self::Response, String> {
47 bcs::from_bytes(&response)
48 .map_err(|e| format!("BCS deserialization error {e:?} for response {response:?}"))
49 }
50
51 fn serialize_response(response: Self::Response) -> Result<Vec<u8>, String> {
53 bcs::to_bytes(&response)
54 .map_err(|e| format!("BCS serialization error {e:?} for response {response:?}"))
55 }
56}
57pub trait ServiceAbi {
62 type Query: Serialize + DeserializeOwned + Send + Sync + Debug + 'static;
64
65 type QueryResponse: Serialize + DeserializeOwned + Send + Sync + Debug + 'static;
67}
68pub trait WithContractAbi {
72 type Abi: ContractAbi;
74}
75
76impl<A> ContractAbi for A
77where
78 A: WithContractAbi,
79{
80 type Operation = <<A as WithContractAbi>::Abi as ContractAbi>::Operation;
81 type Response = <<A as WithContractAbi>::Abi as ContractAbi>::Response;
82}
83
84pub trait WithServiceAbi {
86 type Abi: ServiceAbi;
88}
89
90impl<A> ServiceAbi for A
91where
92 A: WithServiceAbi,
93{
94 type Query = <<A as WithServiceAbi>::Abi as ServiceAbi>::Query;
95 type QueryResponse = <<A as WithServiceAbi>::Abi as ServiceAbi>::QueryResponse;
96}