Skip to main content

PackageMetadata

Struct PackageMetadata 

Source
#[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
Non-exhaustive structs could have additional fields added in future. Therefore, non-exhaustive structs cannot be constructed in external crates using the traditional Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.
§binary: String

Binary name (executable or library)

§module_version: String

Full module version (may include build number)

§version: String

Crate version from Cargo.toml

§maintainer: String

Maintainer contact information

§name: String

Package name

§module_type: String

Module type (agent, library, executable, etc.)

§repo: String

Git repository name

§branch: String

Git branch name

§hash: String

Git commit hash

§copyright: String

Copyright information

§os: String

Operating system name

§os_version: String

Operating system version

Implementations§

Source§

impl PackageMetadata

Source

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.

Source

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

Source§

fn clone(&self) -> PackageMetadata

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for PackageMetadata

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for PackageMetadata

Source§

fn default() -> PackageMetadata

Returns the “default value” for a type. Read more
Source§

impl<'de> Deserialize<'de> for PackageMetadata

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl From<Info> for PackageMetadata

Available on Linux only.
Source§

fn from(info: Info) -> Self

Converts to this type from the input type.
Source§

impl Serialize for PackageMetadata

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,