hypersync_net_types/types.rs
1use hypersync_format::FixedSizeData;
2
3pub type Sighash = FixedSizeData<4>;
4
5/// A collection of filter clauses combined with logical OR semantics.
6///
7/// `AnyOf<T>` represents multiple filter conditions where a match occurs if ANY of the
8/// contained filters match. This enables fluent chaining of filters with OR logic using the
9/// `.or()` method.
10///
11/// This type is typically created by calling `.or()` on filter types like `LogFilter`,
12/// `BlockFilter`, `TransactionFilter`, or `TraceFilter`, and can be passed directly to
13/// query methods like `where_logs()`, `where_blocks()`, etc.
14///
15/// # Examples
16///
17/// ```
18/// use hypersync_net_types::{LogFilter, Query};
19///
20/// // Create filters that match logs from USDT OR USDC contracts
21/// let filter = LogFilter::all()
22/// .and_address(["0xdac17f958d2ee523a2206206994597c13d831ec7"])? // USDT
23/// .or(
24/// LogFilter::all()
25/// .and_address(["0xa0b86a33e6c11c8c0c5c0b5e6adee30d1a234567"])? // USDC
26/// );
27///
28/// // Use the combined filter in a query
29/// let query = Query::new()
30/// .from_block(18_000_000)
31/// .where_logs(filter);
32/// # Ok::<(), anyhow::Error>(())
33/// ```
34///
35/// # Type System
36///
37/// The generic parameter `T` represents the filter type being combined. For example:
38/// - `AnyOf<LogFilter>` for combining log filters
39/// - `AnyOf<TransactionFilter>` for combining transaction filters
40/// - `AnyOf<BlockFilter>` for combining block filters
41/// - `AnyOf<TraceFilter>` for combining trace filters
42pub struct AnyOf<T>(Vec<T>);
43
44impl<T> IntoIterator for AnyOf<T> {
45 type Item = T;
46 type IntoIter = std::vec::IntoIter<T>;
47
48 fn into_iter(self) -> Self::IntoIter {
49 self.0.into_iter()
50 }
51}
52
53impl<T> AnyOf<T> {
54 /// Create a new `AnyOf` containing a single filter clause.
55 ///
56 /// This is typically used internally when converting from a single filter to an `AnyOf`.
57 /// Most users will create `AnyOf` instances by calling `.or()` on filter types.
58 ///
59 /// # Arguments
60 /// * `clause` - The initial filter clause
61 ///
62 /// # Examples
63 ///
64 /// ```
65 /// use hypersync_net_types::{LogFilter, types::AnyOf};
66 ///
67 /// let filter = LogFilter::all();
68 /// let any_clause = AnyOf::new(filter);
69 /// ```
70 pub fn new(clause: T) -> Self {
71 Self(vec![clause])
72 }
73
74 /// Create an `AnyOf` from multiple filter clauses.
75 ///
76 /// This method accepts any iterable collection of filter clauses and creates an `AnyOf`
77 /// that matches if any of the provided clauses match. This is useful when you have a
78 /// collection of filters to combine.
79 ///
80 /// # Arguments
81 /// * `clauses` - An iterable collection of filter clauses
82 ///
83 /// # Examples
84 ///
85 /// ```
86 /// use hypersync_net_types::{LogFilter, types::AnyOf};
87 ///
88 /// let filters = vec![
89 /// LogFilter::all().and_address(["0xdac17f958d2ee523a2206206994597c13d831ec7"]).unwrap(),
90 /// LogFilter::all().and_address(["0xa0b86a33e6c11c8c0c5c0b5e6adee30d1a234567"]).unwrap(),
91 /// ];
92 /// let any_clause = AnyOf::any(filters);
93 /// ```
94 pub fn any<I>(clauses: I) -> Self
95 where
96 I: IntoIterator<Item = T>,
97 {
98 Self(clauses.into_iter().collect())
99 }
100
101 /// Add another filter clause to this `AnyOf` with OR logic.
102 ///
103 /// This method extends the current `AnyOf` with an additional filter clause.
104 /// The resulting `AnyOf` will match if any of the contained clauses match,
105 /// including both the existing clauses and the newly added one.
106 ///
107 /// This enables fluent chaining: `clause1.or(clause2).or(clause3)`.
108 ///
109 /// # Arguments
110 /// * `clause` - The filter clause to add
111 ///
112 /// # Returns
113 /// The updated `AnyOf` containing the additional clause
114 ///
115 /// # Examples
116 ///
117 /// ```
118 /// use hypersync_net_types::LogFilter;
119 ///
120 /// // Chain multiple filters with OR logic
121 /// let filter = LogFilter::all()
122 /// .and_address(["0xdac17f958d2ee523a2206206994597c13d831ec7"])? // USDT
123 /// .or(LogFilter::all().and_address(["0xa0b86a33e6c11c8c0c5c0b5e6adee30d1a234567"])?) // USDC
124 /// .or(LogFilter::all().and_address(["0x6b175474e89094c44da98b954eedeac495271d0f"])?); // DAI
125 /// # Ok::<(), anyhow::Error>(())
126 /// ```
127 pub fn or(mut self, clause: T) -> Self {
128 self.0.push(clause);
129 self
130 }
131}