foundry_blob_explorers/
request.rs1use serde::{ser::SerializeStruct, Deserialize, Serialize};
4
5#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
9pub struct GetBlockQuery {
10 pub expand: BlockExpansion,
12 pub kind: BlockKind,
14}
15
16impl GetBlockQuery {
17 pub const fn new(expand: BlockExpansion) -> Self {
19 Self { expand, kind: BlockKind::Canonical }
20 }
21
22 pub const fn with_all(self) -> Self {
24 Self { expand: BlockExpansion::all(), ..self }
25 }
26
27 pub const fn with_none(self) -> Self {
29 Self { expand: BlockExpansion::none(), ..self }
30 }
31
32 pub const fn reorged(self) -> Self {
34 Self { kind: BlockKind::Reorged, ..self }
35 }
36
37 pub const fn with_transaction(self) -> Self {
39 Self { expand: self.expand.with_transaction(), ..self }
40 }
41
42 pub const fn no_transactions(self) -> Self {
44 Self { expand: self.expand.no_transactions(), ..self }
45 }
46
47 pub const fn with_blob(self) -> Self {
49 Self { expand: self.expand.with_blob(), ..self }
50 }
51
52 pub const fn no_blob(self) -> Self {
54 Self { expand: self.expand.no_blob(), ..self }
55 }
56
57 pub const fn with_blob_data(self) -> Self {
59 Self { expand: self.expand.with_blob_data(), ..self }
60 }
61
62 pub const fn no_blob_data(self) -> Self {
64 Self { expand: self.expand.no_blob_data(), ..self }
65 }
66}
67
68impl Serialize for GetBlockQuery {
69 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
70 where
71 S: serde::Serializer,
72 {
73 let mut s = serializer.serialize_struct("GetBlockQuery", 2)?;
74 let expand = self.expand.query_string();
75 if !expand.is_empty() {
76 s.serialize_field("expand", &expand)?;
77 }
78 s.serialize_field("type", &self.kind)?;
79 s.end()
80 }
81}
82
83#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
85pub enum BlockKind {
86 #[default]
88 #[serde(rename = "canonical")]
89 Canonical,
90 #[serde(rename = "reorged")]
92 Reorged,
93}
94
95#[derive(Clone, Copy, Debug, PartialEq, Eq)]
99pub struct BlockExpansion {
100 pub transaction: bool,
102 pub blob: bool,
104 pub blob_data: bool,
106}
107
108impl BlockExpansion {
109 pub const fn all() -> Self {
111 Self { transaction: true, blob: true, blob_data: true }
112 }
113
114 pub const fn none() -> Self {
116 Self { transaction: false, blob: false, blob_data: false }
117 }
118
119 pub fn query_string(self) -> String {
121 let Self { transaction, blob, blob_data } = self;
122 transaction
123 .then_some("transaction")
124 .into_iter()
125 .chain(blob.then_some("blob"))
126 .chain(blob_data.then_some("blob_data"))
127 .collect::<Vec<_>>()
128 .join(",")
129 }
130
131 pub const fn no_blob_data(self) -> Self {
133 Self { blob_data: false, ..self }
134 }
135
136 pub const fn no_transactions(self) -> Self {
138 Self { transaction: false, ..self }
139 }
140
141 pub const fn no_blob(self) -> Self {
143 Self { blob: false, ..self }
144 }
145
146 pub const fn with_transaction(self) -> Self {
148 Self { transaction: true, ..self }
149 }
150
151 pub const fn with_blob(self) -> Self {
153 Self { blob: true, ..self }
154 }
155
156 pub const fn with_blob_data(self) -> Self {
158 Self { blob_data: true, ..self }
159 }
160}
161
162impl Default for BlockExpansion {
163 fn default() -> Self {
164 Self::all()
165 }
166}
167
168#[derive(Clone, Copy, Debug, PartialEq, Eq)]
172pub struct GetTransactionQuery {
173 pub block: bool,
175 pub blob: bool,
177 pub blob_data: bool,
179}
180
181impl GetTransactionQuery {
182 pub const fn all() -> Self {
184 Self { block: true, blob: true, blob_data: true }
185 }
186
187 pub const fn none() -> Self {
189 Self { block: false, blob: false, blob_data: false }
190 }
191
192 pub fn query_string(self) -> String {
194 let Self { block, blob, blob_data } = self;
195 block
196 .then_some("block")
197 .into_iter()
198 .chain(blob.then_some("blob"))
199 .chain(blob_data.then_some("blob_data"))
200 .collect::<Vec<_>>()
201 .join(",")
202 }
203
204 pub const fn no_blob_data(self) -> Self {
206 Self { blob_data: false, ..self }
207 }
208
209 pub const fn no_block(self) -> Self {
211 Self { block: false, ..self }
212 }
213
214 pub const fn no_blob(self) -> Self {
216 Self { blob: false, ..self }
217 }
218}
219
220impl Default for GetTransactionQuery {
221 fn default() -> Self {
222 Self::all()
223 }
224}
225
226impl Serialize for GetTransactionQuery {
227 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
228 where
229 S: serde::Serializer,
230 {
231 let mut s = serializer.serialize_struct("GetTransactionQuery", 1)?;
232 let expand = self.query_string();
233 if !expand.is_empty() {
234 s.serialize_field("expand", &expand)?;
235 }
236 s.end()
237 }
238}