Skip to main content

thegraph_core/fake_impl/
alloy.rs

1//! Implementation of the [`fake`] crate for generating random test data for different [`alloy`]
2//! crate types.
3//!
4//! ```rust
5//! use fake::Fake;
6//! use thegraph_core::{
7//!    alloy::primitives::{Address, B256, Signature, U256},
8//!    fake_impl::alloy::Alloy
9//! };
10//!
11//! // Generate random bytes
12//! let bytes: B256 = Alloy.fake();
13//!
14//! // Generate random U256 value
15//! let value: U256 = Alloy.fake();
16//!
17//! // Generate random address
18//! let address: Address = Alloy.fake();
19//!
20//! // Generate random Signature
21//! let signature: Signature = Alloy.fake();
22//!
23//! // etc.
24//! ```
25
26/// The `Alloy` struct is used to implement the [`fake::Dummy`] trait for the `alloy` crate types.
27pub struct Alloy;
28
29#[doc(hidden)]
30pub mod primitives {
31    use alloy::primitives::{Address, B128, B256, Signature, U256};
32    use fake::{Dummy, Faker, Rng};
33
34    use super::Alloy;
35
36    impl Dummy<Alloy> for B128 {
37        /// Generate a random [`B128`] value.
38        ///
39        /// ```rust
40        /// # use fake::Fake;
41        /// use thegraph_core::{
42        ///     alloy::primitives::B128,
43        ///     fake_impl::alloy::Alloy
44        /// };
45        ///
46        /// let value: B128 = Alloy.fake();
47        /// # assert_ne!(value, B128::ZERO);
48        /// ```
49        fn dummy_with_rng<R: Rng + ?Sized>(_: &Alloy, rng: &mut R) -> Self {
50            <[u8; 16]>::dummy_with_rng(&Faker, rng).into()
51        }
52    }
53
54    impl Dummy<Alloy> for B256 {
55        /// Generate a random [`B256`] value.
56        ///
57        /// ```rust
58        /// # use fake::Fake;
59        /// use thegraph_core::{
60        ///     alloy::primitives::B256,
61        ///     fake_impl::alloy::Alloy
62        /// };
63        ///
64        /// let value: B256 = Alloy.fake();
65        /// # assert_ne!(value, B256::ZERO);
66        /// ```
67        fn dummy_with_rng<R: Rng + ?Sized>(_: &Alloy, rng: &mut R) -> Self {
68            <[u8; 32]>::dummy_with_rng(&Faker, rng).into()
69        }
70    }
71
72    impl Dummy<Alloy> for U256 {
73        /// Generate a random [`U256`] value.
74        ///
75        /// ```rust
76        /// # use fake::Fake;
77        /// use thegraph_core::{
78        ///     alloy::primitives::U256,
79        ///     fake_impl::alloy::Alloy
80        /// };
81        ///
82        /// let value: U256 = Alloy.fake();
83        /// # assert_ne!(value, U256::ZERO);
84        fn dummy_with_rng<R: Rng + ?Sized>(_: &Alloy, rng: &mut R) -> Self {
85            U256::from_be_bytes(<[u8; 32]>::dummy_with_rng(&Faker, rng))
86        }
87    }
88
89    impl Dummy<Alloy> for Address {
90        /// Generate a random [`Address`] value.
91        /// ```rust
92        /// # use fake::Fake;
93        /// use thegraph_core::{
94        ///     alloy::primitives::Address,
95        ///     fake_impl::alloy::Alloy
96        /// };
97        ///
98        /// let value: Address = Alloy.fake();
99        /// # assert_ne!(value, Address::ZERO);
100        /// ```
101        fn dummy_with_rng<R: Rng + ?Sized>(_: &Alloy, rng: &mut R) -> Self {
102            <[u8; 20]>::dummy_with_rng(&Faker, rng).into()
103        }
104    }
105
106    impl Dummy<Alloy> for Signature {
107        /// Generate a random [`Signature`] value.
108        ///
109        /// ```rust
110        /// # use fake::Fake;
111        /// use thegraph_core::{
112        ///     alloy::primitives::Signature,
113        ///     fake_impl::alloy::Alloy
114        /// };
115        ///
116        /// let value: Signature = Alloy.fake();
117        /// ```
118        fn dummy_with_rng<R: Rng + ?Sized>(config: &Alloy, rng: &mut R) -> Self {
119            Signature::from_scalars_and_parity(
120                B256::dummy_with_rng(config, rng),
121                B256::dummy_with_rng(config, rng),
122                rng.random(),
123            )
124        }
125    }
126}