Skip to content

lang: Generate documentation of constants in declare_program! #3311

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading