Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[wasm-metadata] warn on missing docs and Debug impls #1920

Merged
merged 3 commits into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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