Skip to content

Commit 999fc16

Browse files
Add a wit-encoder tool (#1580)
* init wit-encoder * implement base types for `wit-encoder` * fix warnings * tidy up * tidy world * Mostly interface updates * Interface updates * Function accessors * Result accessors * Some type updates * impl Display for inline types * Some type additions * Added render trait * TypeDef flags, enums, and variants * params construct from iterator * Added resource * Standalone functions * Removed some unused types * Added some tests * Docs * Handle intensifiers with keywords * Cleaned up some comments * Clean up render a bit * Made some things more private * Some smaller usability changes * Added serde dependency * Same derives everywhere * interface and package items should be a single Vec, to keep global order * Cargo.lock * Fix up world * Fix type * Renamed StandaloneFunction to StandaloneFunc * Revert formatting of items outside of wit-encoder * Removed serde * Not all types can be borrowed * Some small result related improvements * Added wit-encoder to workspace properly * Updated Cargo.lock * Added wit-encoder to publish list --------- Co-authored-by: Yosh <github@yosh.is>
1 parent 0ef50ac commit 999fc16

23 files changed

Lines changed: 1981 additions & 0 deletions

Cargo.lock

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ members = [
3737
'crates/fuzz-stats',
3838
'crates/wasm-mutate-stats',
3939
'fuzz',
40+
'crates/wit-encoder',
4041
'crates/wit-parser/fuzz',
4142
'crates/wit-component/dl',
4243
'playground/component',
@@ -94,6 +95,7 @@ wasmprinter = { version = "0.210.0", path = "crates/wasmprinter" }
9495
wast = { version = "210.0.0", path = "crates/wast" }
9596
wat = { version = "1.210.0", path = "crates/wat" }
9697
wit-component = { version = "0.210.0", path = "crates/wit-component" }
98+
wit-encoder = { version = "0.210.0", path = "crates/wit-encoder" }
9799
wit-parser = { version = "0.210.0", path = "crates/wit-parser" }
98100
wit-smith = { version = "0.210.0", path = "crates/wit-smith" }
99101

@@ -140,6 +142,7 @@ cpp_demangle = { version = "0.4.0", optional = true }
140142

141143
# Dependencies of `component`
142144
wit-component = { workspace = true, optional = true, features = ['dummy-module', 'wat', 'semver-check'] }
145+
wit-encoder = { workspace = true, optional = true }
143146
wit-parser = { workspace = true, optional = true, features = ['decoding', 'wat', 'serde'] }
144147
wast = { workspace = true, optional = true }
145148

@@ -208,6 +211,7 @@ compose = ['wasm-compose', 'dep:wasmparser']
208211
demangle = ['rustc-demangle', 'cpp_demangle', 'dep:wasmparser', 'wasm-encoder']
209212
component = [
210213
'wit-component',
214+
'wit-encoder',
211215
'wit-parser',
212216
'dep:wast',
213217
'wasm-encoder',

ci/publish.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const CRATES_TO_PUBLISH: &[&str] = &[
2828
"wit-parser",
2929
"wasm-metadata",
3030
"wit-component",
31+
"wit-encoder",
3132
"wasm-compose",
3233
"wit-smith",
3334
"wasm-tools",

crates/wit-encoder/Cargo.toml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[package]
2+
description = "A WIT encoder for Rust"
3+
documentation = "https://docs.rs/wit-encoder"
4+
edition.workspace = true
5+
license = "Apache-2.0 WITH LLVM-exception"
6+
name = "wit-encoder"
7+
repository = "https://github.com/bytecodealliance/wasm-tools/tree/main/crates/wit-encoder"
8+
version.workspace = true
9+
10+
[lints]
11+
workspace = true
12+
13+
[dependencies]
14+
semver = { workspace = true }
15+
pretty_assertions = { workspace = true }

crates/wit-encoder/src/docs.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
use std::fmt;
2+
3+
use crate::{Render, RenderOpts};
4+
5+
/// Documentation
6+
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
7+
pub struct Docs {
8+
contents: String,
9+
}
10+
11+
impl Docs {
12+
pub fn new(contents: impl Into<String>) -> Self {
13+
Self {
14+
contents: contents.into(),
15+
}
16+
}
17+
}
18+
19+
impl<S> From<S> for Docs
20+
where
21+
S: Into<String>,
22+
{
23+
fn from(value: S) -> Self {
24+
Self {
25+
contents: value.into(),
26+
}
27+
}
28+
}
29+
30+
impl Render for Docs {
31+
fn render(&self, f: &mut fmt::Formatter<'_>, opts: &RenderOpts) -> fmt::Result {
32+
for line in self.contents.lines() {
33+
write!(f, "{}/// {}\n", opts.spaces(), line)?;
34+
}
35+
Ok(())
36+
}
37+
}

crates/wit-encoder/src/enum_.rs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
use crate::{Docs, Ident};
2+
3+
/// A variant without a payload
4+
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
5+
pub struct Enum {
6+
pub(crate) cases: Vec<EnumCase>,
7+
}
8+
9+
impl<C> FromIterator<C> for Enum
10+
where
11+
C: Into<EnumCase>,
12+
{
13+
fn from_iter<T: IntoIterator<Item = C>>(iter: T) -> Self {
14+
Self {
15+
cases: iter.into_iter().map(|c| c.into()).collect(),
16+
}
17+
}
18+
}
19+
20+
impl Enum {
21+
pub fn cases(&self) -> &[EnumCase] {
22+
&self.cases
23+
}
24+
25+
pub fn cases_mut(&mut self) -> &mut Vec<EnumCase> {
26+
&mut self.cases
27+
}
28+
}
29+
30+
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
31+
pub struct EnumCase {
32+
pub(crate) name: Ident,
33+
pub(crate) docs: Option<Docs>,
34+
}
35+
36+
impl<N> From<N> for EnumCase
37+
where
38+
N: Into<Ident>,
39+
{
40+
fn from(value: N) -> Self {
41+
Self {
42+
name: value.into(),
43+
docs: None,
44+
}
45+
}
46+
}
47+
48+
impl EnumCase {
49+
pub fn new(name: impl Into<Ident>) -> Self {
50+
Self {
51+
name: name.into(),
52+
docs: None,
53+
}
54+
}
55+
56+
pub fn docs(&mut self, docs: Option<impl Into<Docs>>) {
57+
self.docs = docs.map(|d| d.into());
58+
}
59+
}

crates/wit-encoder/src/flags.rs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
use crate::{ident::Ident, Docs};
2+
3+
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
4+
pub struct Flags {
5+
pub(crate) flags: Vec<Flag>,
6+
}
7+
8+
impl Flags {
9+
pub fn new(flags: impl IntoIterator<Item = impl Into<Flag>>) -> Self {
10+
Self {
11+
flags: flags.into_iter().map(|f| f.into()).collect(),
12+
}
13+
}
14+
15+
pub fn flags(&self) -> &[Flag] {
16+
&self.flags
17+
}
18+
19+
pub fn flags_mut(&mut self) -> &mut Vec<Flag> {
20+
&mut self.flags
21+
}
22+
}
23+
24+
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
25+
pub struct Flag {
26+
pub(crate) name: Ident,
27+
pub(crate) docs: Option<Docs>,
28+
}
29+
30+
impl Flag {
31+
pub fn new(name: impl Into<Ident>) -> Self {
32+
Flag {
33+
name: name.into(),
34+
docs: None,
35+
}
36+
}
37+
38+
pub fn docs(&mut self, docs: Option<impl Into<Docs>>) {
39+
self.docs = docs.map(|d| d.into());
40+
}
41+
}
42+
43+
impl<T> Into<Flag> for (T,)
44+
where
45+
T: Into<Ident>,
46+
{
47+
fn into(self) -> Flag {
48+
Flag::new(self.0)
49+
}
50+
}
51+
52+
impl<T, D> Into<Flag> for (T, D)
53+
where
54+
T: Into<Ident>,
55+
D: Into<Docs>,
56+
{
57+
fn into(self) -> Flag {
58+
let mut flag = Flag::new(self.0);
59+
flag.docs(Some(self.1));
60+
flag
61+
}
62+
}

0 commit comments

Comments
 (0)