Skip to content

Commit ae5bede

Browse files
committed
feat: udpate macro and fix doc
1 parent 2977998 commit ae5bede

File tree

2 files changed

+27
-17
lines changed

2 files changed

+27
-17
lines changed

src/error.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
use std::sync::Arc;
1+
use std::{mem::discriminant, sync::Arc};
22

33
use alloy::{
44
eips::BlockId,
55
transports::{RpcError, TransportErrorKind},
66
};
77
use thiserror::Error;
88

9-
use crate::robust_provider::Error as RobustProviderError;
9+
use crate::{robust_provider::Error as RobustProviderError, types::ScannerResult};
1010

1111
#[derive(Error, Debug, Clone)]
1212
pub enum ScannerError {
@@ -47,3 +47,11 @@ impl From<RpcError<TransportErrorKind>> for ScannerError {
4747
ScannerError::RpcError(Arc::new(error))
4848
}
4949
}
50+
impl<T: Clone> PartialEq<ScannerError> for ScannerResult<T> {
51+
fn eq(&self, other: &ScannerError) -> bool {
52+
match self {
53+
Ok(_) => false,
54+
Err(err) => discriminant(err) == discriminant(other),
55+
}
56+
}
57+
}

src/test_utils/macros.rs

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,33 @@
11
use alloy::primitives::LogData;
22
use tokio_stream::Stream;
33

4-
use crate::{Message, event_scanner::EventScannerResult};
4+
use crate::{ScannerMessage, event_scanner::EventScannerResult};
55

66
#[macro_export]
77
macro_rules! assert_next {
8-
// Convenience form with default timeout
9-
($stream: expr, Err($expected_err:pat)) => {
8+
// 1. Explicit Error Matching (Value based) - uses the new PartialEq implementation
9+
($stream: expr, Err($expected_err:expr)) => {
1010
$crate::assert_next!($stream, Err($expected_err), timeout = 5)
1111
};
12-
($stream: expr, $expected: expr) => {
13-
$crate::assert_next!($stream, $expected, timeout = 5)
14-
};
15-
// Result::Err expectation – assert the next item is an Err matching the pattern
16-
($stream: expr, Err($expected_err:pat), timeout = $secs: expr) => {
12+
($stream: expr, Err($expected_err:expr), timeout = $secs: expr) => {
1713
let message = tokio::time::timeout(
1814
std::time::Duration::from_secs($secs),
1915
tokio_stream::StreamExt::next(&mut $stream),
2016
)
2117
.await
2218
.expect("timed out");
2319
if let Some(msg) = message {
24-
assert!(matches!(msg, Err($expected_err)));
20+
let expected = &$expected_err;
21+
assert_eq!(&msg, expected, "Expected error {:?}, got {:?}", expected, msg);
2522
} else {
26-
panic!("Expected Err(..), but channel was closed");
23+
panic!("Expected error {:?}, but channel was closed", $expected_err);
2724
}
2825
};
26+
27+
// 2. Success Matching (Implicit unwrapping) - existing behavior
28+
($stream: expr, $expected: expr) => {
29+
$crate::assert_next!($stream, $expected, timeout = 5)
30+
};
2931
($stream: expr, $expected: expr, timeout = $secs: expr) => {
3032
let message = tokio::time::timeout(
3133
std::time::Duration::from_secs($secs),
@@ -211,7 +213,7 @@ pub async fn assert_event_sequence<S: Stream<Item = EventScannerResult> + Unpin>
211213
.expect("timed out waiting for next batch");
212214

213215
match message {
214-
Some(Ok(Message::Data(batch))) => {
216+
Some(Ok(ScannerMessage::Data(batch))) => {
215217
let mut batch = batch.iter();
216218
let event = batch.next().expect("Streamed batch should not be empty");
217219
assert_eq!(
@@ -250,15 +252,15 @@ pub async fn assert_event_sequence<S: Stream<Item = EventScannerResult> + Unpin>
250252
/// range must start exactly where the previous one ended, and all ranges must fit within
251253
/// the expected bounds.
252254
///
253-
/// The macro expects the stream to yield `Message::Data(range)` variants containing
255+
/// The macro expects the stream to yield `ScannerMessage::Data(range)` variants containing
254256
/// `RangeInclusive<u64>` values representing block ranges. It tracks coverage by ensuring
255257
/// each new range starts at the next expected block number and doesn't exceed the end of
256258
/// the expected range. Once the entire range is covered, the assertion succeeds.
257259
///
258260
/// # Example
259261
///
260262
/// ```rust
261-
/// use event_scanner::{assert_range_coverage, block_range_scanner::Message};
263+
/// use event_scanner::{ScannerMessage, assert_range_coverage};
262264
/// use tokio::sync::mpsc;
263265
/// use tokio_stream::wrappers::ReceiverStream;
264266
///
@@ -269,8 +271,8 @@ pub async fn assert_event_sequence<S: Stream<Item = EventScannerResult> + Unpin>
269271
///
270272
/// // Simulate a scanner that splits blocks 100-199 into chunks
271273
/// tokio::spawn(async move {
272-
/// tx.send(Message::Data(100..=149)).await.unwrap();
273-
/// tx.send(Message::Data(150..=199)).await.unwrap();
274+
/// tx.send(ScannerMessage::Data(100..=149)).await.unwrap();
275+
/// tx.send(ScannerMessage::Data(150..=199)).await.unwrap();
274276
/// });
275277
///
276278
/// // Assert that the stream covers blocks 100-199

0 commit comments

Comments
 (0)