forked from bytecodealliance/wasm-tools
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[wasm-metadata] parse OCI author custom section (bytecodealliance#1925)
* init OCI author parsing * add serialization * fix tests * fix json formatting test * add author parsing
- Loading branch information
1 parent
48cc636
commit b2e621d
Showing
11 changed files
with
185 additions
and
10 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
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
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
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
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,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 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 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 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 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 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 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