stratus/eth/primitives/
log_topic.rs

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