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",
"serde",
"serde_json",
"serde_repr",
"thiserror",
"tokio",
]
@ -614,6 +615,7 @@ dependencies = [
"reqwest",
"serde",
"serde_json",
"serde_repr",
"syn",
"thiserror",
"tokio",
@ -778,6 +780,17 @@ dependencies = [
"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]]
name = "serde_urlencoded"
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" }
serde = { version = "1.0.138", features = ["derive"] }
serde_json = "1.0.82"
serde_repr = "0.1.9"
thiserror = "1.0.31"
[workspace]

View File

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

View File

@ -111,11 +111,22 @@ impl<'a> GroupGeneration<'a> {
}
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 {
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 {
@ -131,13 +142,8 @@ impl<'a> GroupGeneration<'a> {
}
fn all_derives(&self, derives: &'a [&'a str]) -> impl Iterator<Item = &'a str> {
let base = vec!["serde::Deserialize", "serde::Serialize", "Debug"].into_iter();
let additional = derives
.iter()
.copied()
.filter(|item| item != &"serde::Deserialize")
.filter(|item| item != &"serde::Serialize")
.filter(|item| item != &"Debug");
let base = vec!["Debug"].into_iter();
let additional = derives.iter().copied().filter(|item| item != &"Debug");
base.chain(additional)
}

View File

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