nautilus_blockchain/cache/
types.rs1use std::str::FromStr;
17
18use alloy::primitives::{I256, U160, U256};
19use sqlx::{
20 Database, Decode, Encode, Postgres, Type,
21 encode::IsNull,
22 error::BoxDynError,
23 postgres::{PgHasArrayType, PgTypeInfo},
24};
25
26#[derive(Debug, Clone, PartialEq, Eq)]
27pub struct I256Pg(pub I256);
28
29#[derive(Debug, Clone, PartialEq, Eq)]
30pub struct U256Pg(pub U256);
31
32#[derive(Debug, Clone, PartialEq, Eq)]
33pub struct U160Pg(pub U160);
34
35#[derive(Debug, Clone, Copy, PartialEq, Eq)]
36pub struct U128Pg(pub u128);
37
38impl Type<Postgres> for I256Pg {
40 fn type_info() -> PgTypeInfo {
41 PgTypeInfo::with_name("i256")
42 }
43}
44
45impl Type<Postgres> for U256Pg {
47 fn type_info() -> PgTypeInfo {
48 PgTypeInfo::with_name("u256")
49 }
50}
51
52impl Type<Postgres> for U160Pg {
54 fn type_info() -> PgTypeInfo {
55 PgTypeInfo::with_name("u160")
56 }
57}
58
59impl Type<Postgres> for U128Pg {
61 fn type_info() -> PgTypeInfo {
62 PgTypeInfo::with_name("u128")
63 }
64}
65
66impl<'q> Encode<'q, Postgres> for I256Pg {
67 fn encode_by_ref(
68 &self,
69 buf: &mut <Postgres as Database>::ArgumentBuffer<'_>,
70 ) -> Result<IsNull, BoxDynError> {
71 let s = self.0.to_string();
72 <&str as Encode<Postgres>>::encode(&s, buf)
73 }
74}
75
76impl<'q> Encode<'q, Postgres> for U256Pg {
77 fn encode_by_ref(
78 &self,
79 buf: &mut <Postgres as Database>::ArgumentBuffer<'_>,
80 ) -> Result<IsNull, BoxDynError> {
81 let decimal_str = format!("{}", self.0);
83 <&str as Encode<Postgres>>::encode(&decimal_str, buf)
84 }
85}
86
87impl<'q> Encode<'q, Postgres> for U160Pg {
88 fn encode_by_ref(
89 &self,
90 buf: &mut <Postgres as Database>::ArgumentBuffer<'_>,
91 ) -> Result<IsNull, BoxDynError> {
92 let decimal_str = format!("{}", self.0);
93 <&str as Encode<Postgres>>::encode(&decimal_str, buf)
94 }
95}
96
97impl<'q> Encode<'q, Postgres> for U128Pg {
98 fn encode_by_ref(
99 &self,
100 buf: &mut <Postgres as Database>::ArgumentBuffer<'_>,
101 ) -> Result<IsNull, BoxDynError> {
102 let decimal_str = self.0.to_string();
103 <&str as Encode<Postgres>>::encode(&decimal_str, buf)
104 }
105}
106
107impl<'r> Decode<'r, Postgres> for I256Pg {
109 fn decode(value: sqlx::postgres::PgValueRef<'r>) -> Result<Self, sqlx::error::BoxDynError> {
110 let s = <String as Decode<Postgres>>::decode(value)?;
111 let i256 = I256::from_str(&s).map_err(|e| format!("Failed to parse I256: {e}"))?;
112 Ok(Self(i256))
113 }
114}
115
116impl<'r> Decode<'r, Postgres> for U256Pg {
118 fn decode(value: sqlx::postgres::PgValueRef<'r>) -> Result<Self, sqlx::error::BoxDynError> {
119 let s = <String as Decode<Postgres>>::decode(value)?;
120 let u256 = U256::from_str(&s).map_err(|e| format!("Failed to parse U256: {e}"))?;
121 Ok(Self(u256))
122 }
123}
124
125impl<'r> Decode<'r, Postgres> for U160Pg {
127 fn decode(value: sqlx::postgres::PgValueRef<'r>) -> Result<Self, sqlx::error::BoxDynError> {
128 let s = <String as Decode<Postgres>>::decode(value)?;
129 let u160 = U160::from_str(&s).map_err(|e| format!("Failed to parse U160: {e}"))?;
130 Ok(Self(u160))
131 }
132}
133
134impl<'r> Decode<'r, Postgres> for U128Pg {
136 fn decode(value: sqlx::postgres::PgValueRef<'r>) -> Result<Self, sqlx::error::BoxDynError> {
137 let s = <String as Decode<Postgres>>::decode(value)?;
138 let u128_val = u128::from_str(&s).map_err(|e| format!("Failed to parse U128: {e}"))?;
139 Ok(Self(u128_val))
140 }
141}
142
143impl PgHasArrayType for I256Pg {
145 fn array_type_info() -> PgTypeInfo {
146 PgTypeInfo::with_name("_i256")
147 }
148}
149
150impl PgHasArrayType for U256Pg {
151 fn array_type_info() -> PgTypeInfo {
152 PgTypeInfo::with_name("_u256")
153 }
154}
155
156impl PgHasArrayType for U160Pg {
157 fn array_type_info() -> PgTypeInfo {
158 PgTypeInfo::with_name("_u160")
159 }
160}
161
162impl PgHasArrayType for U128Pg {
163 fn array_type_info() -> PgTypeInfo {
164 PgTypeInfo::with_name("_u128")
165 }
166}