solana_package_metadata/
lib.rs

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