1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// Copyright 2024-, Semiotic AI, Inc.
// SPDX-License-Identifier: Apache-2.0
use Display;
use crateBlockNumber;
use ;
/// Work with block numbers or slots in a unified way.
///
/// This trait provides a common interface for accessing block identifiers,
/// which can either be a block number (for execution layer blocks) or a slot
/// (for consensus layer blocks). By implementing this trait, types can expose
/// their block number or slot in a standardized manner, enabling generic handling
/// of different block types in streaming and processing workflows.
///
/// # Requirements
///
/// Types implementing this trait must also implement [`Clone`], [`Send`], and
/// `'static` to ensure compatibility with asynchronous and concurrent contexts.
///
/// # Provided Method
///
/// * `number_or_slot`: Returns the block number or slot as a `u64`.
///
/// # Example
///
/// ```rust,ignore
/// use firehose_client::HasNumberOrSlot;
///
/// #[derive(Clone)]
/// struct ExecutionBlock {
/// block_number: u64,
/// }
///
/// #[derive(Clone)]
/// struct ConsensusBlock {
/// slot: u64,
/// }
///
/// impl HasNumberOrSlot for ExecutionBlock {
/// fn number_or_slot(&self) -> u64 {
/// self.block_number
/// }
/// }
///
/// impl HasNumberOrSlot for ConsensusBlock {
/// fn number_or_slot(&self) -> u64 {
/// self.slot
/// }
/// }
///
/// fn process_block<T: HasNumberOrSlot>(block: &T) {
/// println!("Processing block with identifier: {}", block.number_or_slot());
/// }
///
/// let execution_block = ExecutionBlock { block_number: 42 };
/// let consensus_block = ConsensusBlock { slot: 24 };
///
/// process_block(&execution_block);
/// process_block(&consensus_block);
/// ```
///
/// # Use Case
///
/// This trait is particularly useful in scenarios where both execution and
/// consensus layer blocks need to be processed generically, such as in blockchain
/// indexing or synchronization applications.
///
/// [`Clone`]: std::clone::Clone
/// [`Send`]: std::marker::Send
///
/// Convert protocol buffer messages into domain-specific block types.
///
/// This trait is intended to simplify the deserialization and conversion process
/// when streaming data from a Firehose gRPC service. Implementations of this trait
/// provide a uniform way to transform a `firehose_protos::Response` message into
/// a concrete type.
///
/// # Example
///
/// ```rust,ignore
/// use firehose_client::FromResponse;
/// use firehose_protos::Response;
///
/// struct MyBlock;
///
/// impl FromResponse for MyBlock {
/// fn from_response(msg: Response) -> Result<Self, ClientError> {
/// // Perform conversion logic here.
/// Ok(MyBlock)
/// }
/// }
/// ```
///
/// # Errors
///
/// Implementations should return a `ProtosError` if the conversion fails. This can
/// occur due to invalid data, missing fields, or other deserialization issues.
///
/// # Usage
///
/// The `FromResponse` trait is typically used in conjunction with generic streaming
/// methods, such as `stream_blocks`, allowing these methods to work with
/// different block types by specifying the type parameter:
///
/// ```rust, ignore
/// let stream = client
/// .stream_blocks_generic::<FirehoseBeaconBlock>(start, total)
/// .await?;
/// ```
///