Skip to content

Commit

Permalink
[wasm-metadata] use tuple enum for payload module
Browse files Browse the repository at this point in the history
  • Loading branch information
yoshuawuyts committed Dec 9, 2024
1 parent 41d6d10 commit 20e6a19
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 54 deletions.
21 changes: 7 additions & 14 deletions crates/wasm-metadata/src/payload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@ pub enum Payload {
children: Vec<Payload>,
},
/// A representation of a Wasm Module
Module {
/// The metadata associated with the Module
metadata: Metadata,
},
Module(Metadata),
}

impl Payload {
Expand Down Expand Up @@ -142,15 +139,15 @@ impl Payload {
pub fn metadata(&self) -> &Metadata {
match self {
Payload::Component { metadata, .. } => metadata,
Payload::Module { metadata } => metadata,
Payload::Module(metadata) => metadata,
}
}

/// Get a mutable reference te the metadata
pub fn metadata_mut(&mut self) -> &mut Metadata {
match self {
Payload::Component { metadata, .. } => metadata,
Payload::Module { metadata } => metadata,
Payload::Module(metadata) => metadata,
}
}

Expand All @@ -164,9 +161,7 @@ impl Payload {
}

fn empty_module(range: Range<usize>) -> Self {
let mut this = Self::Module {
metadata: Metadata::default(),
};
let mut this = Self::Module(Metadata::default());
this.metadata_mut().range = range;
this
}
Expand All @@ -181,11 +176,9 @@ impl Payload {
fn display(&self, f: &mut fmt::Formatter, indent: usize) -> fmt::Result {
let spaces = std::iter::repeat(" ").take(indent).collect::<String>();
match self {
Self::Module {
metadata: Metadata {
name, producers, ..
},
} => {
Self::Module(Metadata {
name, producers, ..
}) => {
if let Some(name) = name {
writeln!(f, "{spaces}module {name}:")?;
} else {
Expand Down
20 changes: 7 additions & 13 deletions crates/wasm-metadata/src/producers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,11 +204,9 @@ mod test {
let module = producers.add_to_wasm(&module).unwrap();

match Payload::from_binary(&module).unwrap() {
Payload::Module {
metadata: Metadata {
name, producers, ..
},
} => {
Payload::Module(Metadata {
name, producers, ..
}) => {
assert_eq!(name, None);
let producers = producers.expect("some producers");
assert_eq!(producers.get("language").unwrap().get("bar").unwrap(), "");
Expand All @@ -234,11 +232,9 @@ mod test {
let module = producers.add_to_wasm(&module).unwrap();

match Payload::from_binary(&module).unwrap() {
Payload::Module {
metadata: Metadata {
name, producers, ..
},
} => {
Payload::Module(Metadata {
name, producers, ..
}) => {
assert_eq!(name, None);
let producers = producers.expect("some producers");
assert_eq!(producers.get("language").unwrap().get("bar").unwrap(), "");
Expand All @@ -264,9 +260,7 @@ mod test {
let module = producers.add_to_wasm(&module).unwrap();

match Payload::from_binary(&module).unwrap() {
Payload::Module {
metadata: Metadata { producers, .. },
} => {
Payload::Module(Metadata { producers, .. }) => {
let producers = producers.expect("some producers");
assert_eq!(
producers.get("processed-by").unwrap().get("baz").unwrap(),
Expand Down
21 changes: 9 additions & 12 deletions crates/wasm-metadata/tests/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,18 +116,15 @@ fn add_to_nested_component() {
assert_eq!(children.len(), 1);

match children.get(0).unwrap() {
Payload::Module {
metadata:
Metadata {
name,
producers,
author,
licenses,
source,
range,
description,
},
} => {
Payload::Module(Metadata {
name,
producers,
author,
licenses,
source,
range,
description,
}) => {
assert_eq!(name, &Some("foo".to_owned()));
let producers = producers.as_ref().expect("some producers");
assert_eq!(
Expand Down
21 changes: 9 additions & 12 deletions crates/wasm-metadata/tests/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,15 @@ fn add_to_empty_module() {
let module = add.to_wasm(&module).unwrap();

match Payload::from_binary(&module).unwrap() {
Payload::Module {
metadata:
Metadata {
name,
producers,
author,
description,
licenses,
source,
range,
},
} => {
Payload::Module(Metadata {
name,
producers,
author,
licenses,
source,
range,
description,
}) => {
assert_eq!(name, Some("foo".to_owned()));
let producers = producers.expect("some producers");
assert_eq!(
Expand Down
4 changes: 1 addition & 3 deletions crates/wit-component/tests/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,7 @@ fn run_test(path: &Path) -> Result<()> {
match Payload::from_binary(&bytes).unwrap() {
// Depends on the ComponentEncoder always putting the first module as the 0th child:
Payload::Component { children, .. } => match &children[0] {
Payload::Module {
metadata: Metadata { producers, .. },
} => {
Payload::Module(Metadata { producers, .. }) => {
let producers = producers.as_ref().expect("child module has producers");
let processed_by = producers
.get("processed-by")
Expand Down

0 comments on commit 20e6a19

Please sign in to comment.