Skip to main content

solana_package_metadata/
lib.rs

1#![cfg_attr(docsrs, feature(doc_cfg))]
2/// Macro for accessing data from the `package.metadata` section of the Cargo manifest
3///
4/// # Arguments
5/// * `key` - A string slice of a dot-separated path to the TOML key of interest
6///
7/// # Example
8/// Given the following `Cargo.toml`:
9/// ```ignore
10/// [package]
11/// name = "MyApp"
12/// version = "0.1.0"
13///
14/// [package.metadata]
15/// copyright = "Copyright (c) 2024 ACME Inc."
16/// ```
17///
18/// You can fetch the copyright with the following:
19/// ```ignore
20/// use solana_package_metadata::package_metadata;
21///
22/// pub fn main() {
23///     let copyright = package_metadata!("copyright");
24///     assert_eq!(copyright, "Copyright (c) 2024 ACME Inc.");
25/// }
26/// ```
27///
28/// ## TOML Support
29/// This macro only supports static data:
30/// * Strings
31/// * Integers
32/// * Floating-point numbers
33/// * Booleans
34/// * Datetimes
35/// * Arrays
36///
37/// ## Array Example
38/// Given the following Cargo manifest:
39/// ```ignore
40/// [package.metadata.arrays]
41/// some_array = [ 1, 2, 3 ]
42/// ```
43///
44/// This is legal:
45/// ```ignore
46/// static ARR: [i64; 3] = package_metadata!("arrays.some_array");
47/// ```
48///
49/// It does *not* currently support accessing TOML array elements directly.
50/// TOML tables are not supported.
51pub use solana_package_metadata_macro::package_metadata;
52/// Re-export solana_pubkey::declare_id for easy usage within the macro
53pub use solana_pubkey::declare_id;
54
55/// Convenience macro for declaring a program id from Cargo.toml package metadata.
56///
57/// # Arguments
58/// * `key` - A string slice of a dot-separated path to the TOML key of interest
59///
60/// # Example
61/// Given the following `Cargo.toml`:
62/// ```ignore
63/// [package]
64/// name = "my-solana-program"
65/// version = "0.1.0"
66///
67/// [package.metadata.solana]
68/// program-id = "MyProgram1111111111111111111111111111111111"
69/// ```
70///
71/// A program can use the program id declared in its `Cargo.toml` as the program
72/// id in code:
73///
74/// ```ignore
75/// declare_id_with_package_metadata!("solana.program-id");
76/// ```
77///
78/// This program id behaves exactly as if the developer had written:
79///
80/// ```
81/// solana_pubkey::declare_id!("MyProgram1111111111111111111111111111111111");
82/// ```
83///
84/// Meaning that it's possible to refer to the program id using `crate::id()`,
85/// without needing to specify the program id in multiple places.
86#[macro_export]
87macro_rules! declare_id_with_package_metadata {
88    ($key:literal) => {
89        $crate::declare_id!($crate::package_metadata!($key));
90    };
91}