#[non_exhaustive]pub struct PackageMetadata {
pub binary: String,
pub module_version: String,
pub version: String,
pub maintainer: String,
pub name: String,
pub module_type: String,
pub repo: String,
pub branch: String,
pub hash: String,
pub copyright: String,
pub os: String,
pub os_version: String,
}Expand description
Package metadata for embedding in the ELF .note.package section.
PackageMetadata holds the raw (unsanitized) metadata values that will be
serialized to JSON and byte-encoded into the linker script by
embed_package_metadata. Callers may
either populate this struct manually in build.rs (e.g. to supply values
from an outer build system without touching Cargo.toml) or use
PackageMetadata::from_cargo_toml to read the current crate’s metadata.
§Non-exhaustive + Default
This struct is marked #[non_exhaustive] and implements Default so new
fields can be added in future minor releases without breaking downstream
code. From outside the crate, #[non_exhaustive] forbids struct-literal
construction; start from Default::default() and assign the fields you
need:
let mut md = PackageMetadata::default();
md.maintainer = "team@contoso.com".into();
md.module_type = "agent".into();
md.version = "1.2.3".into();
md.module_version = "1.2.3.4".into();§Disabling fields
Seven keys are required in the embedded JSON:
binary, version, moduleVersion, name, maintainer, os, and
osVersion. The remaining fields (type, repo, branch, hash,
copyright) are optional. Leave them as the empty string and the
corresponding JSON value is emitted as "", which downstream tooling
can skip. from_cargo_toml() populates the os/osVersion fields
from /etc/os-release, so most builders get them for free; override
only when the detected values don’t match the target platform.
The JSON shape stays stable (every key is always present) because the
.note.package payload is a fixed-layout byte array built from the
linker script; the empty-string-as-disabled convention keeps the layout
constant while letting consumers opt out of leaking fields they don’t
want in the binary.
use module_info::PackageMetadata;
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Library crate that doesn't want to embed git or repo info:
let mut md = PackageMetadata::from_cargo_toml()?;
md.repo.clear();
md.branch.clear();
md.hash.clear();
// `md` still carries binary/version/moduleVersion/name/maintainer
// plus os/osVersion (auto-populated from /etc/os-release).
Ok(())
}Fields (Non-exhaustive)§
This struct is marked as non-exhaustive
Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.binary: StringBinary name (executable or library)
module_version: StringFull module version (may include build number)
version: StringCrate version from Cargo.toml
maintainer: StringMaintainer contact information
name: StringPackage name
module_type: StringModule type (agent, library, executable, etc.)
repo: StringGit repository name
branch: StringGit branch name
hash: StringGit commit hash
copyright: StringCopyright information
os: StringOperating system name
os_version: StringOperating system version
Implementations§
Source§impl PackageMetadata
impl PackageMetadata
Sourcepub fn from_cargo_toml() -> ModuleInfoResult<Self>
pub fn from_cargo_toml() -> ModuleInfoResult<Self>
Build a PackageMetadata by reading the current crate’s Cargo.toml,
environment-variable overrides, git working copy, and OS release info.
This is the zero-configuration entry point: the build script for a
normal Cargo crate can just call
generate_project_metadata_and_linker_script,
which uses this method under the hood. Call from_cargo_toml directly
only when you need to inspect or mutate the collected metadata before
passing it to embed_package_metadata.
The returned values are unsanitized. embed_package_metadata runs
the sanitize step internally so the linker-script bytes and the JSON
string agree byte-for-byte (the invariant that keeps the .note.package
section 4-byte aligned).
§Errors
Returns a ModuleInfoError if Cargo.toml is unreadable or malformed,
if git invocation fails, or if the OS release info cannot be read.
Sourcepub fn field_value(&self, field: ModuleInfoField) -> &str
pub fn field_value(&self, field: ModuleInfoField) -> &str
Return the string value associated with a given ModuleInfoField.
This is the single source of truth mapping ModuleInfoField variants
to PackageMetadata fields. Both the linker-script emitter and the
build-time JSON dump iterate ModuleInfoField::ALL and call this.
Trait Implementations§
Source§impl Clone for PackageMetadata
impl Clone for PackageMetadata
Source§fn clone(&self) -> PackageMetadata
fn clone(&self) -> PackageMetadata
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more