Fix enum serde

This commit is contained in:
Joel Wachsler 2022-08-06 17:02:27 +00:00
parent 6aefb22163
commit e30f7c2c5d
5 changed files with 39 additions and 13 deletions

13
Cargo.lock generated
View File

@ -597,6 +597,7 @@ dependencies = [
"reqwest", "reqwest",
"serde", "serde",
"serde_json", "serde_json",
"serde_repr",
"thiserror", "thiserror",
"tokio", "tokio",
] ]
@ -614,6 +615,7 @@ dependencies = [
"reqwest", "reqwest",
"serde", "serde",
"serde_json", "serde_json",
"serde_repr",
"syn", "syn",
"thiserror", "thiserror",
"tokio", "tokio",
@ -778,6 +780,17 @@ dependencies = [
"serde", "serde",
] ]
[[package]]
name = "serde_repr"
version = "0.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1fe39d9fbb0ebf5eb2c7cb7e2a47e4f462fad1379f1166b8ae49ad9eae89a7ca"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]] [[package]]
name = "serde_urlencoded" name = "serde_urlencoded"
version = "0.7.1" version = "0.7.1"

View File

@ -13,6 +13,7 @@ tokio = { version = "1.19.2", features = ["full"] }
qbittorrent-web-api-gen = { path = "./qbittorrent-web-api-gen", version = "0.6.0" } qbittorrent-web-api-gen = { path = "./qbittorrent-web-api-gen", version = "0.6.0" }
serde = { version = "1.0.138", features = ["derive"] } serde = { version = "1.0.138", features = ["derive"] }
serde_json = "1.0.82" serde_json = "1.0.82"
serde_repr = "0.1.9"
thiserror = "1.0.31" thiserror = "1.0.31"
[workspace] [workspace]

View File

@ -32,6 +32,7 @@ case = "1.0.0"
thiserror = "1.0.31" thiserror = "1.0.31"
serde = { version = "1.0.138", features = ["derive"] } serde = { version = "1.0.138", features = ["derive"] }
serde_json = "1.0.82" serde_json = "1.0.82"
serde_repr = "0.1.9"
regex = "1.6.0" regex = "1.6.0"
[dev-dependencies] [dev-dependencies]

View File

@ -111,11 +111,22 @@ impl<'a> GroupGeneration<'a> {
} }
pub fn struct_derives(&self) -> TokenStream { pub fn struct_derives(&self) -> TokenStream {
self.derives(self.struct_derives, &[]) self.derives(
self.struct_derives,
&["serde::Deserialize", "serde::Serialize"],
)
} }
pub fn enum_derives(&self) -> TokenStream { pub fn enum_derives(&self) -> TokenStream {
self.derives(self.enum_derives, &["PartialEq", "Eq"]) self.derives(
self.enum_derives,
&[
"PartialEq",
"Eq",
"serde_repr::Deserialize_repr",
"serde_repr::Serialize_repr",
],
)
} }
pub fn derives(&self, derives: &'a [&'a str], additional_derives: &[&str]) -> TokenStream { pub fn derives(&self, derives: &'a [&'a str], additional_derives: &[&str]) -> TokenStream {
@ -131,13 +142,8 @@ impl<'a> GroupGeneration<'a> {
} }
fn all_derives(&self, derives: &'a [&'a str]) -> impl Iterator<Item = &'a str> { fn all_derives(&self, derives: &'a [&'a str]) -> impl Iterator<Item = &'a str> {
let base = vec!["serde::Deserialize", "serde::Serialize", "Debug"].into_iter(); let base = vec!["Debug"].into_iter();
let additional = derives let additional = derives.iter().copied().filter(|item| item != &"Debug");
.iter()
.copied()
.filter(|item| item != &"serde::Deserialize")
.filter(|item| item != &"serde::Serialize")
.filter(|item| item != &"Debug");
base.chain(additional) base.chain(additional)
} }

View File

@ -143,6 +143,7 @@ impl<'a> EnumGeneration<'a> {
quote! { quote! {
#[allow(clippy::enum_variant_names)] #[allow(clippy::enum_variant_names)]
#derives #derives
#[repr(i8)]
pub enum #name { pub enum #name {
#(#values,)* #(#values,)*
} }
@ -160,11 +161,15 @@ impl parser::EnumValue {
// special enum value which does not follow conventions // special enum value which does not follow conventions
if orig_name == "\"/path/to/download/to\"" { if orig_name == "\"/path/to/download/to\"" {
quote! { // don't know how to handle this one
PathToDownloadTo(String) // quote! { PathToDownloadTo(String) }
} return quote! { PathToDownloadTo = -1 };
} else { };
let name_camel = self.name_camel(); let name_camel = self.name_camel();
if let Ok(v) = orig_name.parse::<i8>() {
quote! { #name_camel = #v }
} else {
quote! { quote! {
#[serde(rename = #orig_name)] #[serde(rename = #orig_name)]
#name_camel #name_camel