| 1 |
use crate::{Producers, rewrite_wasm}; |
| 2 |
use anyhow::Result; |
| 3 |
use std::fmt::Debug; |
| 4 |
|
| 5 |
/// Add metadata (module name, producers) to a WebAssembly file. |
| 6 |
/// |
| 7 |
/// Supports both core WebAssembly modules and components. In components, |
| 8 |
/// metadata will be added to the outermost component. |
| 9 |
#[derive(Debug, Clone, Default)] |
| 10 |
#[non_exhaustive] |
| 11 |
pub struct AddMetadata { |
| 12 |
/// Add a module or component name to the names section |
| 13 |
pub name: AddMetadataField<String>, |
| 14 |
|
| 15 |
/// Add a programming language to the producers section |
| 16 |
pub language: Vec<(String, String)>, |
| 17 |
|
| 18 |
/// Add a tool and its version to the producers section |
| 19 |
pub processed_by: Vec<(String, String)>, |
| 20 |
|
| 21 |
/// Add an SDK and its version to the producers section |
| 22 |
pub sdk: Vec<(String, String)>, |
| 23 |
|
| 24 |
/// Contact details of the people or organization responsible, |
| 25 |
/// encoded as a freeform string. |
| 26 |
#[cfg(feature = "oci")] |
| 27 |
pub authors: AddMetadataField<crate::Authors>, |
| 28 |
|
| 29 |
/// A human-readable description of the binary |
| 30 |
#[cfg(feature = "oci")] |
| 31 |
pub description: AddMetadataField<crate::Description>, |
| 32 |
|
| 33 |
/// License(s) under which contained software is distributed as an SPDX License Expression. |
| 34 |
#[cfg(feature = "oci")] |
| 35 |
pub licenses: AddMetadataField<crate::Licenses>, |
| 36 |
|
| 37 |
/// URL to get source code for building the image |
| 38 |
#[cfg(feature = "oci")] |
| 39 |
pub source: AddMetadataField<crate::Source>, |
| 40 |
|
| 41 |
/// URL to find more information on the binary |
| 42 |
#[cfg(feature = "oci")] |
| 43 |
pub homepage: AddMetadataField<crate::Homepage>, |
| 44 |
|
| 45 |
/// Source control revision identifier for the packaged software. |
| 46 |
#[cfg(feature = "oci")] |
| 47 |
pub revision: AddMetadataField<crate::Revision>, |
| 48 |
|
| 49 |
/// Version of the packaged software |
| 50 |
#[cfg(feature = "oci")] |
| 51 |
pub version: AddMetadataField<crate::Version>, |
| 52 |
} |
| 53 |
|
| 54 |
impl AddMetadata { |
| 55 |
/// Process a WebAssembly binary. Supports both core WebAssembly modules, and WebAssembly |
| 56 |
/// components. The module and component will have, at very least, an empty name and producers |
| 57 |
/// section created. |
| 58 |
pub fn to_wasm(&self, input: &[u8]) -> Result<Vec<u8>> { |
| 59 |
let add_producers = Producers::from_meta(self); |
| 60 |
rewrite_wasm(self, &add_producers, input) |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
/// Defines how to modify a field of the component/module metadata |
| 65 |
#[derive(Debug, Clone)] |
| 66 |
pub enum AddMetadataField<T: Debug + Clone> { |
| 67 |
/// Keep the existing value of the field |
| 68 |
Keep, |
| 69 |
/// Remove the existing value of the field |
| 70 |
Clear, |
| 71 |
/// Set the field to a new value |
| 72 |
Set(T), |
| 73 |
} |
| 74 |
|
| 75 |
impl<T: Debug + Clone> AddMetadataField<T> { |
| 76 |
/// Returns true if the field should be cleared |
| 77 |
pub fn is_clear(&self) -> bool { |
| 78 |
matches!(self, Self::Clear) |
| 79 |
} |
| 80 |
|
| 81 |
/// Returns true if the field should be kept |
| 82 |
pub fn is_keep(&self) -> bool { |
| 83 |
matches!(self, Self::Keep) |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
impl<T: Debug + Clone> Default for AddMetadataField<T> { |
| 88 |
fn default() -> Self { |
| 89 |
Self::Keep |
| 90 |
} |
| 91 |
} |