Skip to content

Commit

Permalink
lang: Generate documentation of constants in declare_program! (#3311)
Browse files Browse the repository at this point in the history
  • Loading branch information
acheroncrypto authored Oct 13, 2024
1 parent 6e77838 commit f5f8edf
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ The minor version will be incremented upon a breaking change and the patch versi
- cli: Build IDL if there is only one program when using the `idl build` command ([#3275](https://github.com/coral-xyz/anchor/pull/3275)).
- cli: Add short alias for the `idl build` command ([#3283](https://github.com/coral-xyz/anchor/pull/3283)).
- cli: Add `--program-id` option to `idl convert` command ([#3309](https://github.com/coral-xyz/anchor/pull/3309)).
- lang: Generate documentation of constants in `declare_program!` ([#3311](https://github.com/coral-xyz/anchor/pull/3311)).

### Fixes

Expand Down
9 changes: 6 additions & 3 deletions lang/attribute/program/src/declare_program/mods/constants.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use anchor_lang_idl::types::{Idl, IdlType};
use quote::{format_ident, quote, ToTokens};

use super::common::convert_idl_type_to_syn_type;
use super::common::{convert_idl_type_to_syn_type, gen_docs};

pub fn gen_constants_mod(idl: &Idl) -> proc_macro2::TokenStream {
let constants = idl.constants.iter().map(|c| {
let name = format_ident!("{}", c.name);
let docs = gen_docs(&c.docs);
let val = syn::parse_str::<syn::Expr>(&c.value)
.unwrap()
.to_token_stream();
Expand All @@ -15,8 +16,10 @@ pub fn gen_constants_mod(idl: &Idl) -> proc_macro2::TokenStream {
_ => (convert_idl_type_to_syn_type(&c.ty).to_token_stream(), val),
};

// TODO: Docs
quote! { pub const #name: #ty = #val; }
quote! {
#docs
pub const #name: #ty = #val;
}
});

quote! {
Expand Down
3 changes: 3 additions & 0 deletions tests/declare-program/idls/external.json
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,9 @@
"constants": [
{
"name": "MASTER_SEED",
"docs": [
"Master seed slice"
],
"type": "bytes",
"value": "[109, 97, 115, 116, 101, 114]"
}
Expand Down
1 change: 1 addition & 0 deletions tests/declare-program/programs/external/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use anchor_lang::prelude::*;

declare_id!("Externa111111111111111111111111111111111111");

/// Master seed slice
#[constant]
pub const MASTER_SEED: &[u8] = b"master";

Expand Down

0 comments on commit f5f8edf

Please sign in to comment.