From e30f7c2c5d817c053036926d2a2f06e9fc6a5fd1 Mon Sep 17 00:00:00 2001 From: Joel Wachsler Date: Sat, 6 Aug 2022 17:02:27 +0000 Subject: [PATCH] Fix enum serde --- Cargo.lock | 13 ++++++++++ Cargo.toml | 1 + qbittorrent-web-api-gen/Cargo.toml | 1 + .../src/generate/api_group.rs | 24 ++++++++++++------- qbittorrent-web-api-gen/src/generate/group.rs | 13 ++++++---- 5 files changed, 39 insertions(+), 13 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index daf43ab..c54737b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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" diff --git a/Cargo.toml b/Cargo.toml index 823db59..17ed794 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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] diff --git a/qbittorrent-web-api-gen/Cargo.toml b/qbittorrent-web-api-gen/Cargo.toml index 3bdcc52..f6fa4a8 100644 --- a/qbittorrent-web-api-gen/Cargo.toml +++ b/qbittorrent-web-api-gen/Cargo.toml @@ -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] diff --git a/qbittorrent-web-api-gen/src/generate/api_group.rs b/qbittorrent-web-api-gen/src/generate/api_group.rs index 7452bb8..c1df457 100644 --- a/qbittorrent-web-api-gen/src/generate/api_group.rs +++ b/qbittorrent-web-api-gen/src/generate/api_group.rs @@ -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 { - 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) } diff --git a/qbittorrent-web-api-gen/src/generate/group.rs b/qbittorrent-web-api-gen/src/generate/group.rs index 5694bcf..8ca7aa0 100644 --- a/qbittorrent-web-api-gen/src/generate/group.rs +++ b/qbittorrent-web-api-gen/src/generate/group.rs @@ -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::() { + quote! { #name_camel = #v } } else { - let name_camel = self.name_camel(); quote! { #[serde(rename = #orig_name)] #name_camel