-
Notifications
You must be signed in to change notification settings - Fork 251
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[wasm-metadata] Split
lib.rs
into separate files (#1917)
* split tests into their own file * move producers * move component_names * move module_names * continue splitting tests * group names in a subdir * group names in subdir * move metadata to its own file * refactor remaining parts * add doc comment * inline module tests into their respective modules
- Loading branch information
1 parent
7fe32e3
commit 33a8bd0
Showing
12 changed files
with
1,576 additions
and
1,512 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
use crate::{rewrite_wasm, Producers, RegistryMetadata}; | ||
|
||
use anyhow::Result; | ||
|
||
/// Add metadata (module name, producers) to a WebAssembly file. | ||
/// | ||
/// Supports both core WebAssembly modules and components. In components, | ||
/// metadata will be added to the outermost component. | ||
#[cfg_attr(feature = "clap", derive(clap::Parser))] | ||
#[derive(Debug, Clone, Default)] | ||
pub struct AddMetadata { | ||
/// Add a module or component name to the names section | ||
#[cfg_attr(feature = "clap", clap(long, value_name = "NAME"))] | ||
pub name: Option<String>, | ||
|
||
/// Add a programming language to the producers section | ||
#[cfg_attr(feature = "clap", clap(long, value_name = "NAME"))] | ||
pub language: Vec<String>, | ||
|
||
/// Add a tool and its version to the producers section | ||
#[cfg_attr(feature = "clap", clap(long = "processed-by", value_parser = parse_key_value, value_name="NAME=VERSION"))] | ||
pub processed_by: Vec<(String, String)>, | ||
|
||
/// Add an SDK and its version to the producers section | ||
#[cfg_attr(feature="clap", clap(long, value_parser = parse_key_value, value_name="NAME=VERSION"))] | ||
pub sdk: Vec<(String, String)>, | ||
|
||
/// Add an registry metadata to the registry-metadata section | ||
#[cfg_attr(feature="clap", clap(long, value_parser = parse_registry_metadata_value, value_name="PATH"))] | ||
pub registry_metadata: Option<RegistryMetadata>, | ||
} | ||
|
||
#[cfg(feature = "clap")] | ||
pub(crate) fn parse_key_value(s: &str) -> Result<(String, String)> { | ||
s.split_once('=') | ||
.map(|(k, v)| (k.to_owned(), v.to_owned())) | ||
.ok_or_else(|| anyhow::anyhow!("expected KEY=VALUE")) | ||
} | ||
|
||
#[cfg(feature = "clap")] | ||
pub(crate) fn parse_registry_metadata_value(s: &str) -> Result<RegistryMetadata> { | ||
let contents = std::fs::read(s)?; | ||
|
||
let registry_metadata = RegistryMetadata::from_bytes(&contents, 0)?; | ||
|
||
Ok(registry_metadata) | ||
} | ||
|
||
impl AddMetadata { | ||
/// Process a WebAssembly binary. Supports both core WebAssembly modules, and WebAssembly | ||
/// components. The module and component will have, at very least, an empty name and producers | ||
/// section created. | ||
pub fn to_wasm(&self, input: &[u8]) -> Result<Vec<u8>> { | ||
rewrite_wasm( | ||
&self.name, | ||
&Producers::from_meta(self), | ||
self.registry_metadata.as_ref(), | ||
input, | ||
) | ||
} | ||
} |
Oops, something went wrong.