String enum fixes

This commit is contained in:
Joel Wachsler 2022-08-08 18:02:21 +00:00
parent f0c293e704
commit 84bc1c2a60
5 changed files with 56 additions and 31 deletions

View File

@ -1367,7 +1367,7 @@ Property | Type | Description
`seen_complete` | integer | Time (Unix Epoch) when this torrent was last seen complete `seen_complete` | integer | Time (Unix Epoch) when this torrent was last seen complete
`seq_dl` | bool | True if sequential download is enabled `seq_dl` | bool | True if sequential download is enabled
`size` | integer | Total size (bytes) of files selected for download `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 `super_seeding` | bool | True if super seeding is enabled
`tags` | string | Comma-concatenated tag list of the torrent `tags` | string | Comma-concatenated tag list of the torrent
`time_active` | integer | Total active time (seconds) `time_active` | integer | Total active time (seconds)

View File

@ -3700,15 +3700,20 @@
is_list: false, is_list: false,
}, },
), ),
String( Object(
TypeInfo { Object {
type_info: TypeInfo {
name: "state", name: "state",
description: Some( description: Some(
"Torrent state. See table here below for the possible values", "state object see table below.",
), ),
is_optional: false, is_optional: false,
is_list: false, is_list: false,
}, },
ref_type: String(
"State",
),
},
), ),
Bool( Bool(
TypeInfo { TypeInfo {

View File

@ -117,16 +117,8 @@ impl<'a> GroupGeneration<'a> {
) )
} }
pub fn enum_derives(&self) -> TokenStream { pub fn enum_derives(&self, extra: &[&str]) -> TokenStream {
self.derives( self.derives(self.enum_derives, &[&["PartialEq", "Eq"], extra].concat())
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 {

View File

@ -138,12 +138,17 @@ impl<'a> EnumGeneration<'a> {
.iter() .iter()
.map(|enum_value| enum_value.generate()); .map(|enum_value| enum_value.generate());
let name = util::to_ident(&self.enum_.name); 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! { quote! {
#[allow(clippy::enum_variant_names)] #[allow(clippy::enum_variant_names)]
#derives #derives
#[repr(i8)] #repr
pub enum #name { pub enum #name {
#(#values,)* #(#values,)*
} }
@ -151,29 +156,52 @@ impl<'a> EnumGeneration<'a> {
} }
} }
impl parser::EnumValue { enum EnumValueType {
fn generate(&self) -> TokenStream { Number,
util::add_docs(&self.description, self.generate_field()) String,
} }
fn generate_field(&self) -> TokenStream { 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().0)
}
fn generate_field(&self) -> (TokenStream, EnumValueType) {
let orig_name = self.original_value.clone(); let orig_name = self.original_value.clone();
// 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\"" {
// don't know how to handle this one // don't know how to handle this one
// quote! { PathToDownloadTo(String) } // quote! { PathToDownloadTo(String) }
return quote! { PathToDownloadTo = -1 }; return (quote! { PathToDownloadTo = -1 }, EnumValueType::Number);
}; };
let name_camel = self.name_camel(); let name_camel = self.name_camel();
if let Ok(v) = orig_name.parse::<i8>() { if let Ok(v) = orig_name.parse::<i8>() {
quote! { #name_camel = #v } (quote! { #name_camel = #v }, EnumValueType::Number)
} else { } else {
(
quote! { quote! {
#[serde(rename = #orig_name)] #[serde(rename = #orig_name)]
#name_camel #name_camel
} },
EnumValueType::String,
)
} }
} }

View File

@ -6502,11 +6502,11 @@ TokenTree {
], ],
}, },
TableRow { TableRow {
raw: "`state` | string | Torrent state. See table here below for the possible values", raw: "`state` | object | state object see table below.",
columns: [ columns: [
"state", "state",
"string", "object",
"Torrent state. See table here below for the possible values", "state object see table below.",
], ],
}, },
TableRow { TableRow {