Skip to content

Commit 5274c27

Browse files
Implement serde::Deserialize for wit-bindgen-rust::Opts (#1244)
* Implement serde::Deserialize for wit-bindgen-rust::Opts * Change serde to an optional dependency * Combine serde feature attributes into a single cfg_attr line * Run `cargo fmt`
1 parent 8cfb805 commit 5274c27

File tree

4 files changed

+29
-2
lines changed

4 files changed

+29
-2
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ anyhow = "1.0.72"
2727
bitflags = "2.3.3"
2828
heck = { version = "0.5" }
2929
pulldown-cmark = { version = "0.9", default-features = false }
30+
serde = { version = "1.0.218", features = ["derive"] }
3031
clap = { version = "4.3.19", features = ["derive"] }
3132
indexmap = "2.0.0"
3233
prettyplease = "0.2.20"

crates/rust/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ clap = { workspace = true, optional = true }
2525
indexmap = { workspace = true }
2626
syn = { workspace = true }
2727
prettyplease = { workspace = true }
28+
serde = { workspace = true, optional = true }
2829

2930
[dev-dependencies]
3031
futures = { workspace = true }
3132
wit-bindgen = { path = '../guest-rust', features = ['async'] }
3233
wit-bindgen-rt = { path = '../guest-rust/rt' }
3334
test-helpers = { path = '../test-helpers' }
3435
# For use with the custom attributes test
35-
serde = { version = "1.0", features = ["derive"] }
3636
serde_json = "1"

crates/rust/src/lib.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,11 @@ enum RuntimeItem {
116116
}
117117

118118
#[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
119+
#[cfg_attr(
120+
feature = "serde",
121+
derive(serde::Deserialize),
122+
serde(rename_all = "kebab-case")
123+
)]
119124
pub enum ExportKey {
120125
World,
121126
Name(String),
@@ -134,6 +139,11 @@ fn parse_with(s: &str) -> Result<(String, WithOption), String> {
134139
}
135140

136141
#[derive(Default, Debug, Clone)]
142+
#[cfg_attr(
143+
feature = "serde",
144+
derive(serde::Deserialize),
145+
serde(rename_all = "kebab-case")
146+
)]
137147
pub enum AsyncConfig {
138148
#[default]
139149
None,
@@ -181,6 +191,11 @@ fn parse_async(s: &str) -> Result<AsyncConfig, String> {
181191

182192
#[derive(Default, Debug, Clone)]
183193
#[cfg_attr(feature = "clap", derive(clap::Parser))]
194+
#[cfg_attr(
195+
feature = "serde",
196+
derive(serde::Deserialize),
197+
serde(rename_all = "kebab-case")
198+
)]
184199
pub struct Opts {
185200
/// Whether or not a formatter is executed to format generated code.
186201
#[cfg_attr(feature = "clap", arg(long))]
@@ -315,6 +330,7 @@ pub struct Opts {
315330
/// - import:<name> or
316331
/// - export:<name>
317332
#[cfg_attr(feature = "clap", arg(long = "async", value_parser = parse_async, default_value = "none"))]
333+
#[cfg_attr(feature = "serde", serde(rename = "async"))]
318334
pub async_: AsyncConfig,
319335
}
320336

@@ -1445,6 +1461,11 @@ fn group_by_resource<'a>(
14451461
}
14461462

14471463
#[derive(Default, Debug, Clone, Copy)]
1464+
#[cfg_attr(
1465+
feature = "serde",
1466+
derive(serde::Deserialize),
1467+
serde(rename_all = "kebab-case")
1468+
)]
14481469
pub enum Ownership {
14491470
/// Generated types will be composed entirely of owning fields, regardless
14501471
/// of whether they are used as parameters to imports or not.
@@ -1499,6 +1520,11 @@ impl fmt::Display for Ownership {
14991520

15001521
/// Options for with "with" remappings.
15011522
#[derive(Debug, Clone)]
1523+
#[cfg_attr(
1524+
feature = "serde",
1525+
derive(serde::Deserialize),
1526+
serde(rename_all = "kebab-case")
1527+
)]
15021528
pub enum WithOption {
15031529
Path(String),
15041530
Generate,

crates/test/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ heck = { workspace = true }
2121
log = "0.4.26"
2222
rayon = "1.10.0"
2323
regex = "1.11.1"
24-
serde = { version = "1.0.218", features = ["derive"] }
24+
serde = { workspace = true }
2525
toml = "0.8.20"
2626
wasi-preview1-component-adapter-provider = "30.0.2"
2727
wac-parser = "0.6.1"

0 commit comments

Comments
 (0)