From 3a3550ced5218909057d820ef95f8ab3e4eff0d5 Mon Sep 17 00:00:00 2001 From: Hardhat Chad Date: Sun, 25 Aug 2024 20:00:51 +0000 Subject: [PATCH] error macro --- api/src/error.rs | 8 ++------ utils/src/macros.rs | 17 ++++++++++++++--- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/api/src/error.rs b/api/src/error.rs index 159fc01..b0653ba 100644 --- a/api/src/error.rs +++ b/api/src/error.rs @@ -1,5 +1,5 @@ use num_enum::IntoPrimitive; -use solana_program::program_error::ProgramError; +use ore_utils::*; use thiserror::Error; #[derive(Debug, Error, Clone, Copy, PartialEq, Eq, IntoPrimitive)] @@ -23,8 +23,4 @@ pub enum OreError { AuthFailed = 7, } -impl From for ProgramError { - fn from(e: OreError) -> Self { - ProgramError::Custom(e as u32) - } -} +error!(OreError); diff --git a/utils/src/macros.rs b/utils/src/macros.rs index 53d7fd5..0a2e064 100644 --- a/utils/src/macros.rs +++ b/utils/src/macros.rs @@ -78,10 +78,13 @@ macro_rules! account { } #[macro_export] -macro_rules! instruction { +macro_rules! error { ($struct_name:ident) => { - $crate::impl_to_bytes!($struct_name); - $crate::impl_instruction_from_bytes!($struct_name); + impl From<$struct_name> for solana_program::program_error::ProgramError { + fn from(e: $struct_name) -> Self { + solana_program::program_error::ProgramError::Custom(e as u32) + } + } }; } @@ -92,3 +95,11 @@ macro_rules! event { $crate::impl_from_bytes!($struct_name); }; } + +#[macro_export] +macro_rules! instruction { + ($struct_name:ident) => { + $crate::impl_to_bytes!($struct_name); + $crate::impl_instruction_from_bytes!($struct_name); + }; +}