From 84bc1c2a6029a980e1e74a75c3304427f63bc591 Mon Sep 17 00:00:00 2001 From: Joel Wachsler Date: Mon, 8 Aug 2022 18:02:21 +0000 Subject: [PATCH] String enum fixes --- qbittorrent-web-api-gen/api-4_1.md | 2 +- qbittorrent-web-api-gen/groups.txt | 19 +++++--- .../src/generate/api_group.rs | 12 +---- qbittorrent-web-api-gen/src/generate/group.rs | 48 +++++++++++++++---- qbittorrent-web-api-gen/token_tree.txt | 6 +-- 5 files changed, 56 insertions(+), 31 deletions(-) diff --git a/qbittorrent-web-api-gen/api-4_1.md b/qbittorrent-web-api-gen/api-4_1.md index 9068f43..503f803 100644 --- a/qbittorrent-web-api-gen/api-4_1.md +++ b/qbittorrent-web-api-gen/api-4_1.md @@ -1367,7 +1367,7 @@ Property | Type | Description `seen_complete` | integer | Time (Unix Epoch) when this torrent was last seen complete `seq_dl` | bool | True if sequential download is enabled `size` | integer | Total size (bytes) of files selected for download -`state` | string | Torrent state. See table here below for the possible values +`state` | object | state object see table below. `super_seeding` | bool | True if super seeding is enabled `tags` | string | Comma-concatenated tag list of the torrent `time_active` | integer | Total active time (seconds) diff --git a/qbittorrent-web-api-gen/groups.txt b/qbittorrent-web-api-gen/groups.txt index d9b8b3b..20e5b75 100644 --- a/qbittorrent-web-api-gen/groups.txt +++ b/qbittorrent-web-api-gen/groups.txt @@ -3700,14 +3700,19 @@ is_list: false, }, ), - String( - TypeInfo { - name: "state", - description: Some( - "Torrent state. See table here below for the possible values", + Object( + Object { + type_info: TypeInfo { + name: "state", + description: Some( + "state object see table below.", + ), + is_optional: false, + is_list: false, + }, + ref_type: String( + "State", ), - is_optional: false, - is_list: false, }, ), Bool( diff --git a/qbittorrent-web-api-gen/src/generate/api_group.rs b/qbittorrent-web-api-gen/src/generate/api_group.rs index c1df457..09eb679 100644 --- a/qbittorrent-web-api-gen/src/generate/api_group.rs +++ b/qbittorrent-web-api-gen/src/generate/api_group.rs @@ -117,16 +117,8 @@ impl<'a> GroupGeneration<'a> { ) } - pub fn enum_derives(&self) -> TokenStream { - self.derives( - self.enum_derives, - &[ - "PartialEq", - "Eq", - "serde_repr::Deserialize_repr", - "serde_repr::Serialize_repr", - ], - ) + pub fn enum_derives(&self, extra: &[&str]) -> TokenStream { + self.derives(self.enum_derives, &[&["PartialEq", "Eq"], extra].concat()) } pub fn derives(&self, derives: &'a [&'a str], additional_derives: &[&str]) -> TokenStream { diff --git a/qbittorrent-web-api-gen/src/generate/group.rs b/qbittorrent-web-api-gen/src/generate/group.rs index 8ca7aa0..4f5dfe9 100644 --- a/qbittorrent-web-api-gen/src/generate/group.rs +++ b/qbittorrent-web-api-gen/src/generate/group.rs @@ -138,12 +138,17 @@ impl<'a> EnumGeneration<'a> { .iter() .map(|enum_value| enum_value.generate()); let name = util::to_ident(&self.enum_.name); - let derives = self.group.enum_derives(); + let (derives, repr) = self + .enum_ + .values + .first() + .map(|v| v.generate_field().1.repr(self.group)) + .unwrap(); quote! { #[allow(clippy::enum_variant_names)] #derives - #[repr(i8)] + #repr pub enum #name { #(#values,)* } @@ -151,29 +156,52 @@ impl<'a> EnumGeneration<'a> { } } +enum EnumValueType { + Number, + String, +} + +impl EnumValueType { + fn repr(&self, group: &GroupGeneration) -> (TokenStream, TokenStream) { + match self { + EnumValueType::Number => ( + group.enum_derives(&["serde_repr::Deserialize_repr", "serde_repr::Serialize_repr"]), + quote! { #[repr(i8)] }, + ), + EnumValueType::String => ( + group.enum_derives(&["serde::Deserialize", "serde::Serialize"]), + quote! { #[repr(i8)] }, + ), + } + } +} + impl parser::EnumValue { fn generate(&self) -> TokenStream { - util::add_docs(&self.description, self.generate_field()) + util::add_docs(&self.description, self.generate_field().0) } - fn generate_field(&self) -> TokenStream { + fn generate_field(&self) -> (TokenStream, EnumValueType) { let orig_name = self.original_value.clone(); // special enum value which does not follow conventions if orig_name == "\"/path/to/download/to\"" { // don't know how to handle this one // quote! { PathToDownloadTo(String) } - return quote! { PathToDownloadTo = -1 }; + return (quote! { PathToDownloadTo = -1 }, EnumValueType::Number); }; let name_camel = self.name_camel(); if let Ok(v) = orig_name.parse::() { - quote! { #name_camel = #v } + (quote! { #name_camel = #v }, EnumValueType::Number) } else { - quote! { - #[serde(rename = #orig_name)] - #name_camel - } + ( + quote! { + #[serde(rename = #orig_name)] + #name_camel + }, + EnumValueType::String, + ) } } diff --git a/qbittorrent-web-api-gen/token_tree.txt b/qbittorrent-web-api-gen/token_tree.txt index 64162ad..8bec9b1 100644 --- a/qbittorrent-web-api-gen/token_tree.txt +++ b/qbittorrent-web-api-gen/token_tree.txt @@ -6502,11 +6502,11 @@ TokenTree { ], }, TableRow { - raw: "`state` | string | Torrent state. See table here below for the possible values", + raw: "`state` | object | state object see table below.", columns: [ "state", - "string", - "Torrent state. See table here below for the possible values", + "object", + "state object see table below.", ], }, TableRow {