stratus/eth/primitives/
log_topic.rs

1use std::fmt::Display;
2
3use alloy_primitives::B256;
4use alloy_primitives::FixedBytes;
5use display_json::DebugAsJson;
6use fake::Dummy;
7use fake::Faker;
8
9/// Topic is part of a [`Log`](super::Log) emitted by the EVM during contract execution.
10#[derive(DebugAsJson, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize, Default, Hash)]
11pub struct LogTopic(pub B256);
12
13impl Display for LogTopic {
14    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
15        write!(f, "{}", const_hex::encode_prefixed(self.0))
16    }
17}
18
19impl Dummy<Faker> for LogTopic {
20    fn dummy_with_rng<R: rand::Rng + ?Sized>(_: &Faker, rng: &mut R) -> Self {
21        Self(FixedBytes::random_with(rng))
22    }
23}
24
25// -----------------------------------------------------------------------------
26// Conversions: Other -> Self
27// -----------------------------------------------------------------------------
28
29impl From<FixedBytes<32>> for LogTopic {
30    fn from(value: FixedBytes<32>) -> Self {
31        Self(value)
32    }
33}
34
35impl From<[u8; 32]> for LogTopic {
36    fn from(value: [u8; 32]) -> Self {
37        Self(FixedBytes::from(value))
38    }
39}
40
41// -----------------------------------------------------------------------------
42// Conversions: Self -> Other
43// -----------------------------------------------------------------------------
44impl AsRef<[u8]> for LogTopic {
45    fn as_ref(&self) -> &[u8] {
46        self.0.as_ref()
47    }
48}
49
50impl From<LogTopic> for [u8; 32] {
51    fn from(value: LogTopic) -> Self {
52        value.0.0
53    }
54}
55
56impl From<LogTopic> for B256 {
57    fn from(value: LogTopic) -> Self {
58        value.0
59    }
60}