Skip to content

Commit

Permalink
[wasm-metadata] warn on missing docs and Debug impls (bytecodeallianc…
Browse files Browse the repository at this point in the history
…e#1920)

* warn on missing Debug impls in `wasm-metadata`

and impl missing impls

* warn on missing docs in `wasm-metadata`

* undo adding a clone
  • Loading branch information
yoshuawuyts authored Nov 29, 2024
1 parent 0a5433e commit 5acc2df
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 0 deletions.
2 changes: 2 additions & 0 deletions crates/wasm-metadata/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! Read and manipulate WebAssembly metadata
#![warn(missing_debug_implementations, missing_docs)]

pub use add_metadata::AddMetadata;
pub use metadata::Metadata;
pub use names::{ComponentNames, ModuleNames};
Expand Down
10 changes: 10 additions & 0 deletions crates/wasm-metadata/src/names/component.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::fmt::{self, Debug};

use anyhow::Result;
use wasm_encoder::Encode;
use wasmparser::{BinaryReader, ComponentNameSectionReader};
Expand All @@ -10,6 +12,14 @@ pub struct ComponentNames<'a> {
names: Vec<wasmparser::ComponentName<'a>>,
}

impl<'a> Debug for ComponentNames<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("ComponentNames")
.field("component_name", &self.component_name)
.finish_non_exhaustive()
}
}

impl<'a> ComponentNames<'a> {
/// Create an empty component-name section.
pub fn empty() -> Self {
Expand Down
10 changes: 10 additions & 0 deletions crates/wasm-metadata/src/names/module.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::fmt::{self, Debug};

use anyhow::Result;
use wasm_encoder::Encode;
use wasmparser::{BinaryReader, NameSectionReader};
Expand All @@ -10,6 +12,14 @@ pub struct ModuleNames<'a> {
names: Vec<wasmparser::Name<'a>>,
}

impl<'a> Debug for ModuleNames<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("ModuleNames")
.field("module_name", &self.module_name)
.finish_non_exhaustive()
}
}

impl<'a> ModuleNames<'a> {
/// Create an empty name section.
pub fn empty() -> Self {
Expand Down
1 change: 1 addition & 0 deletions crates/wasm-metadata/src/producers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ impl fmt::Display for Producers {
}

/// Contents of a producers field
#[derive(Debug)]
pub struct ProducersField<'a>(&'a IndexMap<String, String>);

impl<'a> ProducersField<'a> {
Expand Down
15 changes: 15 additions & 0 deletions crates/wasm-metadata/src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use wasmparser::Parser;

use crate::{rewrite_wasm, Producers};

/// Metadata used by a Warg registry
#[derive(Debug, Deserialize, Serialize, Clone, Default, PartialEq)]
pub struct RegistryMetadata {
/// List of authors who has created this package.
Expand Down Expand Up @@ -46,6 +47,7 @@ impl RegistryMetadata {
rewrite_wasm(&None, &Producers::empty(), Some(&self), input)
}

/// Parse a Wasm binary and extract the `Registry` section, if there is any.
pub fn from_wasm(bytes: &[u8]) -> Result<Option<Self>> {
let mut depth = 0;
for payload in Parser::new(0).parse_all(bytes) {
Expand All @@ -70,6 +72,8 @@ impl RegistryMetadata {
return Ok(registry);
}

/// Validate the `RegistryMetadata::license` an
/// `RegistryMetadata::CustomLicense` are valid SPDX expressions.
pub fn validate(&self) -> Result<()> {
fn validate_expression(expression: &str) -> Result<Vec<String>> {
let expression = Expression::parse(expression)?;
Expand Down Expand Up @@ -257,9 +261,12 @@ impl Display for RegistryMetadata {
}
}

/// A link from the registry to an external website
#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)]
pub struct Link {
/// The type of link
pub ty: LinkType,
/// The address of the link
pub value: String,
}

Expand All @@ -269,12 +276,18 @@ impl Display for Link {
}
}

/// What kind of link is this?
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
pub enum LinkType {
/// Documentation
Documentation,
/// Homepage
Homepage,
/// Code repository
Repository,
/// Funding and donations
Funding,
/// Some other link type
#[serde(untagged)]
Custom(String),
}
Expand All @@ -293,6 +306,8 @@ impl Display for LinkType {
}
}

/// A human readable short form license identifier for a license not on the SPDX
/// License List.
#[derive(Debug, Deserialize, Serialize, Default, Clone, PartialEq)]
pub struct CustomLicense {
/// License Identifier
Expand Down

0 comments on commit 5acc2df

Please sign in to comment.