ore_utils/
macros.rs

1#[macro_export]
2macro_rules! impl_to_bytes {
3    ($struct_name:ident) => {
4        impl $struct_name {
5            pub fn to_bytes(&self) -> &[u8] {
6                bytemuck::bytes_of(self)
7            }
8        }
9    };
10}
11
12#[macro_export]
13macro_rules! impl_from_bytes {
14    ($struct_name:ident) => {
15        impl $struct_name {
16            pub fn from_bytes(data: &[u8]) -> &Self {
17                bytemuck::from_bytes::<Self>(data)
18            }
19        }
20    };
21}
22
23#[macro_export]
24macro_rules! impl_account_from_bytes {
25    ($struct_name:ident) => {
26        impl $crate::AccountDeserialize for $struct_name {
27            fn try_from_bytes(
28                data: &[u8],
29            ) -> Result<&Self, solana_program::program_error::ProgramError> {
30                if Self::discriminator().ne(&data[0]) {
31                    return Err(solana_program::program_error::ProgramError::InvalidAccountData);
32                }
33                bytemuck::try_from_bytes::<Self>(&data[8..]).or(Err(
34                    solana_program::program_error::ProgramError::InvalidAccountData,
35                ))
36            }
37            fn try_from_bytes_mut(
38                data: &mut [u8],
39            ) -> Result<&mut Self, solana_program::program_error::ProgramError> {
40                if Self::discriminator().ne(&data[0]) {
41                    return Err(solana_program::program_error::ProgramError::InvalidAccountData);
42                }
43                bytemuck::try_from_bytes_mut::<Self>(&mut data[8..]).or(Err(
44                    solana_program::program_error::ProgramError::InvalidAccountData,
45                ))
46            }
47        }
48    };
49}
50
51#[macro_export]
52macro_rules! impl_instruction_from_bytes {
53    ($struct_name:ident) => {
54        impl $struct_name {
55            pub fn try_from_bytes(
56                data: &[u8],
57            ) -> Result<&Self, solana_program::program_error::ProgramError> {
58                bytemuck::try_from_bytes::<Self>(data).or(Err(
59                    solana_program::program_error::ProgramError::InvalidInstructionData,
60                ))
61            }
62        }
63    };
64}
65
66#[macro_export]
67macro_rules! account {
68    ($discriminator_name:ident, $struct_name:ident) => {
69        $crate::impl_to_bytes!($struct_name);
70        $crate::impl_account_from_bytes!($struct_name);
71
72        impl $crate::Discriminator for $struct_name {
73            fn discriminator() -> u8 {
74                $discriminator_name::$struct_name.into()
75            }
76        }
77    };
78}
79
80#[macro_export]
81macro_rules! error {
82    ($struct_name:ident) => {
83        impl From<$struct_name> for solana_program::program_error::ProgramError {
84            fn from(e: $struct_name) -> Self {
85                solana_program::program_error::ProgramError::Custom(e as u32)
86            }
87        }
88    };
89}
90
91#[macro_export]
92macro_rules! event {
93    ($struct_name:ident) => {
94        $crate::impl_to_bytes!($struct_name);
95        $crate::impl_from_bytes!($struct_name);
96    };
97}
98
99#[macro_export]
100macro_rules! instruction {
101    ($discriminator_name:ident, $struct_name:ident) => {
102        $crate::impl_instruction_from_bytes!($struct_name);
103
104        impl $crate::Discriminator for $struct_name {
105            fn discriminator() -> u8 {
106                $discriminator_name::$struct_name as u8
107            }
108        }
109
110        impl $struct_name {
111            pub fn to_bytes(&self) -> Vec<u8> {
112                [
113                    [$discriminator_name::$struct_name as u8].to_vec(),
114                    bytemuck::bytes_of(self).to_vec(),
115                ]
116                .concat()
117            }
118        }
119    };
120}