forked from solana-labs/solana
-
Notifications
You must be signed in to change notification settings - Fork 259
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
package-metadata: Add macro to define program id from Cargo.toml (#1806
) * package-metadata: Add package and macro * Use the macro in a program as a test * Move test to cargo-build-sbf, update version * Add changelog entry * Revert simulation change
- Loading branch information
Showing
7 changed files
with
133 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
[package] | ||
name = "solana-package-metadata" | ||
description = "Solana Package Metadata" | ||
documentation = "https://docs.rs/solana-package-metadata" | ||
version = { workspace = true } | ||
authors = { workspace = true } | ||
repository = { workspace = true } | ||
homepage = { workspace = true } | ||
license = { workspace = true } | ||
edition = { workspace = true } | ||
|
||
[dependencies] | ||
solana-package-metadata-macro = { workspace = true } | ||
solana-pubkey = { workspace = true } | ||
|
||
[package.metadata.docs.rs] | ||
targets = ["x86_64-unknown-linux-gnu"] |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/// Macro for accessing data from the `package.metadata` section of the Cargo manifest | ||
/// | ||
/// # Arguments | ||
/// * `key` - A string slice of a dot-separated path to the TOML key of interest | ||
/// | ||
/// # Example | ||
/// Given the following `Cargo.toml`: | ||
/// ```ignore | ||
/// [package] | ||
/// name = "MyApp" | ||
/// version = "0.1.0" | ||
/// | ||
/// [package.metadata] | ||
/// copyright = "Copyright (c) 2024 ACME Inc." | ||
/// ``` | ||
/// | ||
/// You can fetch the copyright with the following: | ||
/// ```ignore | ||
/// use solana_package_metadata::package_metadata; | ||
/// | ||
/// pub fn main() { | ||
/// let copyright = package_metadata!("copyright"); | ||
/// assert_eq!(copyright, "Copyright (c) 2024 ACME Inc."); | ||
/// } | ||
/// ``` | ||
/// | ||
/// ## TOML Support | ||
/// This macro only supports static data: | ||
/// * Strings | ||
/// * Integers | ||
/// * Floating-point numbers | ||
/// * Booleans | ||
/// * Datetimes | ||
/// * Arrays | ||
/// | ||
/// ## Array Example | ||
/// Given the following Cargo manifest: | ||
/// ```ignore | ||
/// [package.metadata.arrays] | ||
/// some_array = [ 1, 2, 3 ] | ||
/// ``` | ||
/// | ||
/// This is legal: | ||
/// ```ignore | ||
/// static ARR: [i64; 3] = package_metadata!("arrays.some_array"); | ||
/// ``` | ||
/// | ||
/// It does *not* currently support accessing TOML array elements directly. | ||
/// TOML tables are not supported. | ||
pub use solana_package_metadata_macro::package_metadata; | ||
/// Re-export solana_pubkey::declare_id for easy usage within the macro | ||
pub use solana_pubkey::declare_id; | ||
|
||
/// Convenience macro for declaring a program id from Cargo.toml package metadata. | ||
/// | ||
/// # Arguments | ||
/// * `key` - A string slice of a dot-separated path to the TOML key of interest | ||
/// | ||
/// # Example | ||
/// Given the following `Cargo.toml`: | ||
/// ```ignore | ||
/// [package] | ||
/// name = "my-solana-program" | ||
/// version = "0.1.0" | ||
/// | ||
/// [package.metadata.solana] | ||
/// program-id = "MyProgram1111111111111111111111111111111111" | ||
/// ``` | ||
/// | ||
/// A program can use the program id declared in its `Cargo.toml` as the program | ||
/// id in code: | ||
/// | ||
/// ```ignore | ||
/// declare_id_with_package_metadata!("solana.program-id"); | ||
/// ``` | ||
/// | ||
/// This program id behaves exactly as if the developer had written: | ||
/// | ||
/// ``` | ||
/// solana_pubkey::declare_id!("MyProgram1111111111111111111111111111111111"); | ||
/// ``` | ||
/// | ||
/// Meaning that it's possible to refer to the program id using `crate::id()`, | ||
/// without needing to specify the program id in multiple places. | ||
#[macro_export] | ||
macro_rules! declare_id_with_package_metadata { | ||
($key:literal) => { | ||
$crate::declare_id!($crate::package_metadata!($key)); | ||
}; | ||
} |