1pub mod http;
6pub mod log;
7pub mod wrapping;
8
9pub fn to_fixed<T, const N: usize>(v: Vec<T>) -> [T; N] {
10 v.try_into()
11 .unwrap_or_else(|v: Vec<T>| panic!("Expected a Vec of length {} but it was {}", N, v.len()))
12}
13
14#[repr(u32)]
15pub enum ZephyrStatus {
16 Unknown = 0,
17 Success = 1,
18 DbWriteError = 2,
19 DbReadError = 3,
20 NoValOnStack = 4,
21 HostConfiguration = 5,
22}
23
24use http::AgnosticRequest;
25use log::ZephyrLog;
26use serde::{Deserialize, Serialize};
27use stellar_xdr::next::{LedgerEntry, ScAddress, ScVal};
28use thiserror::Error;
29
30#[derive(Error, Debug)]
31pub enum DatabaseError {
32 #[error("Invalid permissions. Tried reading when in write-only")]
33 ReadOnWriteOnly,
34
35 #[error("Invalid permissions. Tried writing when in read-only")]
36 WriteOnReadOnly,
37
38 #[error("Zephyr query malformed.")]
39 ZephyrQueryMalformed,
40
41 #[error("Zephyr query error.")]
42 ZephyrQueryError,
43
44 #[error("Unable to write to DB.")]
45 WriteError,
46
47 #[error("Unable to parse operator.")]
48 OperatorError,
49}
50
51impl From<anyhow::Error> for ZephyrStatus {
52 fn from(value: anyhow::Error) -> Self {
53 match value.downcast_ref() {
54 Some(DatabaseError::WriteError) => ZephyrStatus::DbWriteError,
55 Some(DatabaseError::ZephyrQueryError) => ZephyrStatus::DbReadError,
56 Some(DatabaseError::ZephyrQueryMalformed) => ZephyrStatus::DbReadError,
57 Some(DatabaseError::ReadOnWriteOnly) => ZephyrStatus::HostConfiguration,
58 Some(DatabaseError::WriteOnReadOnly) => ZephyrStatus::HostConfiguration,
59 Some(DatabaseError::OperatorError) => ZephyrStatus::DbWriteError, None => ZephyrStatus::Unknown,
61 }
62 }
63}
64
65impl From<u32> for ZephyrStatus {
66 fn from(value: u32) -> Self {
67 match value {
68 0 => Self::Unknown,
69 1 => Self::Success,
70 2 => Self::DbWriteError,
71 3 => Self::DbReadError,
72 4 => Self::NoValOnStack,
73 5 => Self::HostConfiguration,
74 _ => panic!("Unrecoverable status"),
75 }
76 }
77}
78
79#[derive(Deserialize, Serialize, Clone)]
80pub enum ZephyrVal {
81 I128(i128),
82 I64(i64),
83 U64(u64),
84 F64(f64),
85 U32(u32),
86 I32(i32),
87 F32(f32),
88 String(String),
89 Bytes(Vec<u8>),
90}
91
92#[derive(Debug)]
93pub enum ZephyrValError {
94 ConversionError,
95}
96
97#[derive(Debug, Deserialize, Serialize, Clone)]
98pub struct ContractDataEntry {
99 pub contract_id: ScAddress,
100 pub key: ScVal,
101 pub entry: LedgerEntry,
102 pub durability: i32,
103 pub last_modified: i32,
104}
105
106
107#[derive(Debug, Deserialize, Serialize, Clone, Default)]
108pub struct Account {
109 pub account_id: String,
110 pub native_balance: f64,
111 pub buying_liabilities: f64,
112 pub selling_liabilities: f64,
113 pub seq_num: f64,
114 pub num_subentries: i32,
115 pub num_sponsored: i32,
116 pub num_sponsoring: i32,
117}
118
119macro_rules! impl_inner_from_serialize_only {
120 ($variant:ident, $inner:ty) => {
121 impl From<$inner> for ZephyrVal {
122 fn from(value: $inner) -> Self {
123 ZephyrVal::$variant(value)
124 }
125 }
126 };
127}
128
129macro_rules! impl_inner_from_deserialize_generic {
130 ($variant:ident, $inner:ty) => {
131 impl From<ZephyrVal> for $inner {
132 fn from(value: ZephyrVal) -> Self {
133 match value {
134 ZephyrVal::$variant(inner_val) => inner_val,
135 _ => panic!("Attempted to convert ZephyrVal variant to different inner type"),
136 }
137 }
138 }
139 };
140}
141
142macro_rules! impl_inner_from_deserialize_numeric {
143 ($inner:ty) => {
144 impl From<ZephyrVal> for $inner {
145 fn from(value: ZephyrVal) -> Self {
146 match value {
147 ZephyrVal::I128(num) => num as $inner,
150 ZephyrVal::I32(num) => num as $inner,
151 ZephyrVal::I64(num) => num as $inner,
152 ZephyrVal::U32(num) => num as $inner,
153 ZephyrVal::U64(num) => num as $inner,
154 _ => panic!("Attempted to convert ZephyrVal variant to different inner type"),
155 }
156 }
157 }
158 };
159}
160
161impl_inner_from_serialize_only!(I128, i128);
163impl_inner_from_serialize_only!(I64, i64);
164impl_inner_from_serialize_only!(U64, u64);
165impl_inner_from_serialize_only!(F64, f64);
166impl_inner_from_serialize_only!(U32, u32);
167impl_inner_from_serialize_only!(I32, i32);
168impl_inner_from_serialize_only!(F32, f32);
169impl_inner_from_serialize_only!(String, String);
170impl_inner_from_serialize_only!(Bytes, Vec<u8>);
171
172impl_inner_from_deserialize_numeric!(i128);
174impl_inner_from_deserialize_numeric!(i64);
175impl_inner_from_deserialize_numeric!(u64);
176impl_inner_from_deserialize_numeric!(u32);
177impl_inner_from_deserialize_numeric!(i32);
178impl_inner_from_deserialize_generic!(String, String);
179impl_inner_from_deserialize_generic!(Bytes, Vec<u8>);
180impl_inner_from_deserialize_generic!(F64, f64);
181impl_inner_from_deserialize_generic!(F32, f32);
182
183
184#[derive(Clone, Serialize, Deserialize, Debug)]
185pub enum RelayedMessageRequest {
186 Http(AgnosticRequest),
187 Log(ZephyrLog),
188}