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
`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)

View File

@ -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(

View File

@ -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 {

View File

@ -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::<i8>() {
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,
)
}
}

View File

@ -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 {