Explorar el Código

feat: Add `errors` to declare program macro (#3757)

* add errors to declare program macro

* changelog and docs

* cargo fmt
cryptopapi997 hace 3 meses
padre
commit
864ea6731a

+ 2 - 0
CHANGELOG.md

@@ -12,6 +12,8 @@ The minor version will be incremented upon a breaking change and the patch versi
 
 ### Features
 
+- lang: Add `#[error]` attribute to `declare_program!` ([#3757](https://github.com/coral-xyz/anchor/pull/3757)).
+
 ### Fixes
 
 - docker: Upgrade `node` to 20.18.0 LTS ([#3687](https://github.com/solana-foundation/anchor/pull/3687)).

+ 1 - 0
docs/content/docs/features/declare-program.mdx

@@ -23,6 +23,7 @@ The following modules are generated by the `declare_program!()` macro:
 | [`constants`](https://github.com/coral-xyz/anchor/blob/0e5285aecdf410fa0779b7cd09a47f235882c156/lang/attribute/program/src/declare_program/mods/constants.rs) | Program constants defined in the program                                                                 |
 | [`events`](https://github.com/coral-xyz/anchor/blob/0e5285aecdf410fa0779b7cd09a47f235882c156/lang/attribute/program/src/declare_program/mods/events.rs)       | Program events defined in the program                                                                    |
 | [`types`](https://github.com/coral-xyz/anchor/blob/0e5285aecdf410fa0779b7cd09a47f235882c156/lang/attribute/program/src/declare_program/mods/types.rs)         | Program types defined in the program                                                                     |
+| [`errors`](https://github.com/coral-xyz/anchor/blob/0e5285aecdf410fa0779b7cd09a47f235882c156/lang/attribute/program/src/declare_program/mods/errors.rs)       | Program errors defined in the program                                                                     |
 
 ## Examples
 

+ 4 - 2
lang/attribute/program/src/declare_program/mod.rs

@@ -11,8 +11,8 @@ use syn::parse::{Parse, ParseStream};
 use common::gen_docs;
 use mods::{
     accounts::gen_accounts_mod, client::gen_client_mod, constants::gen_constants_mod,
-    cpi::gen_cpi_mod, events::gen_events_mod, internal::gen_internal_mod, program::gen_program_mod,
-    types::gen_types_mod, utils::gen_utils_mod,
+    cpi::gen_cpi_mod, errors::gen_errors_mod, events::gen_events_mod, internal::gen_internal_mod,
+    program::gen_program_mod, types::gen_types_mod, utils::gen_utils_mod,
 };
 
 pub struct DeclareProgram {
@@ -61,6 +61,7 @@ fn gen_program(idl: &Idl, name: &syn::Ident) -> proc_macro2::TokenStream {
     let accounts_mod = gen_accounts_mod(idl);
     let events_mod = gen_events_mod(idl);
     let types_mod = gen_types_mod(idl);
+    let errors_mod = gen_errors_mod(idl);
 
     // Clients
     let cpi_mod = gen_cpi_mod(idl);
@@ -85,6 +86,7 @@ fn gen_program(idl: &Idl, name: &syn::Ident) -> proc_macro2::TokenStream {
             #accounts_mod
             #events_mod
             #types_mod
+            #errors_mod
 
             #cpi_mod
             #client_mod

+ 30 - 0
lang/attribute/program/src/declare_program/mods/errors.rs

@@ -0,0 +1,30 @@
+use anchor_lang_idl::types::Idl;
+use quote::{format_ident, quote};
+
+pub fn gen_errors_mod(idl: &Idl) -> proc_macro2::TokenStream {
+    let errors = idl.errors.iter().map(|e| {
+        let name = format_ident!("{}", e.name);
+        quote! {
+            #name,
+        }
+    });
+
+    if errors.len() == 0 {
+        return quote! {
+            /// Program error type definitions.
+            pub mod errors {
+            }
+        };
+    }
+
+    quote! {
+        /// Program error type definitions.
+        pub mod errors {
+
+            #[anchor_lang::error_code]
+            pub enum ProgramError {
+                #(#errors)*
+            }
+        }
+    }
+}

+ 1 - 0
lang/attribute/program/src/declare_program/mods/mod.rs

@@ -2,6 +2,7 @@ pub mod accounts;
 pub mod client;
 pub mod constants;
 pub mod cpi;
+pub mod errors;
 pub mod events;
 pub mod internal;
 pub mod program;