Allow crate-level program(name/address) to override the primary program - #131
Merged
Merged
Conversation
Let a crate-level #![codama(program(name = ..., address = ...))] directive override the primary program's identity, with either argument optional so omitted fields fall back to the crate defaults (Cargo.toml package name and declare_id! / package.metadata.solana.program-id). The program(...) directive is now scope-aware: - a new AttributeContext::Crate distinguishes a crate root from a file-module (both previously mapped to File); - at the crate root, name and address are optional (override semantics); - at every other scope (item, module block, file-module) both remain required, since those declare a distinct program with no defaults. SetProgramMetadataVisitor applies the crate-level directive before the Cargo / declare_id! fallbacks, so an explicit value wins while unset fields still resolve from the manifest. ProgramDirective's name/address become Option, and update_or_wrap_program_node only overrides the fields the directive actually set.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR lets a crate-level
#![codama(program(name = ..., address = ...))]directive override the primary program's identity, with either argument optional so omitted fields fall back to the crate defaults (the Cargo.toml package name and thedeclare_id!/package.metadata.solana.program-idaddress).Scope-aware
program(...)directiveThe directive's required arguments now depend on where it is attached:
nameaddress#![codama(program(...))]inlib.rs)#![...]infoo.rs)#[codama(program(...))] mod { .. })Only the crate root overrides an existing default program, so it may omit either field. Every other scope declares a distinct program that has no defaults to fall back on, so both remain required.
To tell the crate root apart from a file-module (both previously mapped to
AttributeContext::File), this adds a newAttributeContext::Cratevariant, built explicitly byCrateKorok::parse.Behaviour
ProgramDirective'sname/addressbecomeOption;update_or_wrap_program_nodeonly overrides the fields the directive actually set.SetProgramMetadataVisitorapplies the crate-level directive before the Cargo.toml /declare_id!fallbacks, so an explicit value wins while unset fields still resolve from the manifest.Tests
program(name = ..),program(address = ..)andprogram(); item scope still errors when either is missing.Follow-up
Enables renaming a program without touching the crate name, e.g.
#![cfg_attr(feature = "codama", codama(program(name = "associatedTokenAccount")))]in the associated-token-account interface crate, which will be done once a version carrying this change is released.