-
Notifications
You must be signed in to change notification settings - Fork 307
[wasm-metadata] parse OCI author custom section #1925
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
Merged
alexcrichton
merged 5 commits into
bytecodealliance:main
from
yoshuawuyts:refactor-registry-metadata
Dec 4, 2024
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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,111 @@ | ||
use std::borrow::Cow; | ||
use std::fmt::{self, Display}; | ||
use std::str::FromStr; | ||
|
||
use anyhow::{ensure, Error, Result}; | ||
use serde::Serialize; | ||
use wasm_encoder::{ComponentSection, CustomSection, Encode, Section}; | ||
use wasmparser::CustomSectionReader; | ||
|
||
/// Contact details of the people or organization responsible, | ||
/// encoded as a freeform string. | ||
#[derive(Debug, Clone, PartialEq)] | ||
pub struct Author(CustomSection<'static>); | ||
|
||
impl Author { | ||
/// Create a new instance of `Author`. | ||
pub fn new<S: Into<Cow<'static, str>>>(s: S) -> Self { | ||
Self(CustomSection { | ||
name: "author".into(), | ||
data: match s.into() { | ||
Cow::Borrowed(s) => Cow::Borrowed(s.as_bytes()), | ||
Cow::Owned(s) => Cow::Owned(s.into()), | ||
}, | ||
}) | ||
} | ||
|
||
/// Parse an `author` custom section from a wasm binary. | ||
pub(crate) fn parse_custom_section(reader: &CustomSectionReader<'_>) -> Result<Self> { | ||
ensure!( | ||
reader.name() == "author", | ||
"The `author` custom section should have a name of 'author'" | ||
); | ||
let data = String::from_utf8(reader.data().to_owned())?; | ||
Ok(Self::new(data)) | ||
} | ||
} | ||
|
||
impl FromStr for Author { | ||
type Err = Error; | ||
|
||
fn from_str(s: &str) -> Result<Self, Self::Err> { | ||
Ok(Self::new(s.to_owned())) | ||
} | ||
} | ||
|
||
impl Serialize for Author { | ||
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error> | ||
where | ||
S: serde::Serializer, | ||
{ | ||
serializer.serialize_str(&self.to_string()) | ||
} | ||
} | ||
|
||
impl Display for Author { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
// NOTE: this will never panic since we always guarantee the data is | ||
// encoded as utf8, even if we internally store it as [u8]. | ||
let data = String::from_utf8(self.0.data.to_vec()).unwrap(); | ||
write!(f, "{data}") | ||
} | ||
} | ||
|
||
impl ComponentSection for Author { | ||
fn id(&self) -> u8 { | ||
ComponentSection::id(&self.0) | ||
} | ||
} | ||
|
||
impl Section for Author { | ||
fn id(&self) -> u8 { | ||
Section::id(&self.0) | ||
} | ||
} | ||
|
||
impl Encode for Author { | ||
fn encode(&self, sink: &mut Vec<u8>) { | ||
self.0.encode(sink); | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use super::*; | ||
use wasm_encoder::Component; | ||
use wasmparser::Payload; | ||
|
||
#[test] | ||
fn roundtrip() { | ||
let mut component = Component::new(); | ||
component.section(&Author::new("Nori Cat")); | ||
let component = component.finish(); | ||
|
||
let mut parsed = false; | ||
for section in wasmparser::Parser::new(0).parse_all(&component) { | ||
if let Payload::CustomSection(reader) = section.unwrap() { | ||
let author = Author::parse_custom_section(&reader).unwrap(); | ||
assert_eq!(author.to_string(), "Nori Cat"); | ||
parsed = true; | ||
} | ||
} | ||
assert!(parsed); | ||
} | ||
|
||
#[test] | ||
fn serialize() { | ||
let author = Author::new("Chashu Cat"); | ||
let json = serde_json::to_string(&author).unwrap(); | ||
assert_eq!(r#""Chashu Cat""#, json); | ||
} | ||
} |
This file contains hidden or 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,20 @@ | ||
//! Annotations following the [OCI Annotations Spec]. | ||
//! | ||
//! The fields of these annotations are encoded into custom sections of | ||
//! component binaries, and are explicitly compatible with the OCI Annotations | ||
//! Spec. That enables Compontents to be encoded to OCI and back without needing | ||
//! to perform any additional parsing. This greatly simplifies adding metadata to | ||
//! component registries, since language-native component toolchains can encode them | ||
//! directly into components. Which in turn can be picked up by Component-to-OCI | ||
//! tooling to take those annotations and display them in a way that registries can | ||
//! understand. | ||
//! | ||
//! For the files in this submodule that means we want to be explicitly | ||
//! compatible with the OCI Annotations specification. Any deviation in our | ||
//! parsing rules from the spec should be considered a bug we have to fix. | ||
//! | ||
//! [OCI Annotations Spec]: https://specs.opencontainers.org/image-spec/annotations/ | ||
|
||
pub use author::Author; | ||
|
||
mod author; |
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The number of function arguments will continue to increase until morale improves.
More seriously though: once I finish adding the custom sections, I plan to refactor the way we handle this. I suspect we can combine
rewrite_wasm
,Metadata
, andAddMetadata
into a unified structure, allowing us to tidy this up.