Skip to content

Commit 4c7a005

Browse files
authored
Added serde support to wit-encoder (#1647)
* Added serde support to wit-encoder * Added serde feature notes
1 parent 8078b66 commit 4c7a005

19 files changed

+85
-2
lines changed

Cargo.lock

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

crates/wit-encoder/Cargo.toml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,24 @@ version.workspace = true
1010
[lints]
1111
workspace = true
1212

13+
[features]
14+
default = ["serde"]
15+
16+
# Enables JSON serialization/deserialization of the wit-encoder structures.
17+
18+
# *Note*: The JSON that this generates is different from the JSON generated from wit-parser.
19+
# If you're looking to create WIT from JSON, then this is the crate and feature for you. But if
20+
# you're parsing WIT and reading the output through JSON, then wit-parser is probably the better
21+
# option.
22+
23+
# *Note*: The exact structure of the JSON is likely not going to be very stable over time,
24+
# so slight tweaks and variants should be expected as this crate evolves.
25+
serde = ["dep:serde", "semver/serde"]
26+
1327
[dependencies]
1428
semver = { workspace = true }
1529
pretty_assertions = { workspace = true }
30+
serde = { workspace = true, optional = true, features = ["derive"] }
1631

1732
[dev-dependencies]
1833
indoc = { workspace = true }

crates/wit-encoder/src/docs.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ use crate::{Render, RenderOpts};
44

55
/// Documentation
66
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
7+
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
8+
#[cfg_attr(feature = "serde", serde(rename_all = "kebab-case"))]
79
pub struct Docs {
810
contents: String,
911
}

crates/wit-encoder/src/enum_.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ use crate::{Docs, Ident};
22

33
/// A variant without a payload
44
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
5+
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
6+
#[cfg_attr(feature = "serde", serde(rename_all = "kebab-case"))]
57
pub struct Enum {
68
pub(crate) cases: Vec<EnumCase>,
79
}
@@ -36,6 +38,8 @@ impl Enum {
3638
}
3739

3840
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
41+
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
42+
#[cfg_attr(feature = "serde", serde(rename_all = "kebab-case"))]
3943
pub struct EnumCase {
4044
pub(crate) name: Ident,
4145
pub(crate) docs: Option<Docs>,

crates/wit-encoder/src/flags.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use crate::{ident::Ident, Docs};
22

33
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
4+
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
5+
#[cfg_attr(feature = "serde", serde(rename_all = "kebab-case"))]
46
pub struct Flags {
57
pub(crate) flags: Vec<Flag>,
68
}
@@ -22,6 +24,8 @@ impl Flags {
2224
}
2325

2426
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
27+
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
28+
#[cfg_attr(feature = "serde", serde(rename_all = "kebab-case"))]
2529
pub struct Flag {
2630
pub(crate) name: Ident,
2731
pub(crate) docs: Option<Docs>,

crates/wit-encoder/src/function.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ use std::fmt::{self, Display};
33
use crate::{ident::Ident, Docs, Type};
44

55
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
6+
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
7+
#[cfg_attr(feature = "serde", serde(rename_all = "kebab-case"))]
68
pub struct Params {
79
items: Vec<(Ident, Type)>,
810
}
@@ -61,6 +63,8 @@ impl Params {
6163
}
6264

6365
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
66+
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
67+
#[cfg_attr(feature = "serde", serde(rename_all = "kebab-case"))]
6468
pub enum Results {
6569
Named(Params),
6670
Anon(Type),
@@ -141,6 +145,8 @@ impl Results {
141145
}
142146

143147
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
148+
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
149+
#[cfg_attr(feature = "serde", serde(rename_all = "kebab-case"))]
144150
pub struct StandaloneFunc {
145151
pub(crate) name: Ident,
146152
pub(crate) params: Params,

crates/wit-encoder/src/ident.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use std::{borrow::Cow, fmt};
22

33
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
4+
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
5+
#[cfg_attr(feature = "serde", serde(rename_all = "kebab-case"))]
46
pub struct Ident(Cow<'static, str>);
57

68
impl Ident {

crates/wit-encoder/src/include.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ use crate::{Ident, Render};
44

55
/// Enable the union of a world with another world
66
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
7+
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
8+
#[cfg_attr(feature = "serde", serde(rename_all = "kebab-case"))]
79
pub struct Include {
810
use_path: Ident,
911
include_names_list: Vec<(String, String)>,

crates/wit-encoder/src/interface.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ use std::fmt;
33
use crate::{Docs, Ident, Render, RenderOpts, StandaloneFunc, TypeDef, Use};
44

55
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
6+
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
7+
#[cfg_attr(feature = "serde", serde(rename_all = "kebab-case"))]
68
pub struct Interface {
79
/// Name of this interface.
810
pub(crate) name: Ident,
@@ -54,6 +56,8 @@ impl Interface {
5456
}
5557

5658
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
59+
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
60+
#[cfg_attr(feature = "serde", serde(rename_all = "kebab-case"))]
5761
pub enum InterfaceItem {
5862
TypeDef(TypeDef),
5963
Use(Use),

crates/wit-encoder/src/package.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ use crate::{ident::Ident, Interface, Render, RenderOpts, World};
1010
/// have a unique identifier that affects generated components and uniquely
1111
/// identifiers this particular package.
1212
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
13+
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
14+
#[cfg_attr(feature = "serde", serde(rename_all = "kebab-case"))]
1315
pub struct Package {
1416
/// A unique name corresponding to this package.
1517
name: PackageName,
@@ -81,6 +83,8 @@ impl fmt::Display for Package {
8183
}
8284

8385
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
86+
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
87+
#[cfg_attr(feature = "serde", serde(rename_all = "kebab-case"))]
8488
pub enum PackageItem {
8589
Interface(Interface),
8690
World(World),
@@ -92,6 +96,8 @@ pub enum PackageItem {
9296
/// This is directly encoded as an "ID" in the binary component representation
9397
/// with an interfaced tacked on as well.
9498
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
99+
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
100+
#[cfg_attr(feature = "serde", serde(rename_all = "kebab-case"))]
95101
pub struct PackageName {
96102
/// A namespace such as `wasi` in `wasi:foo/bar`
97103
namespace: String,

0 commit comments

Comments
 (0)