Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow rename with str|bool|int type for internally tagged enums #1392

Closed
wants to merge 12 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
113 changes: 62 additions & 51 deletions serde_derive/src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2119,7 +2119,7 @@ fn deserialize_identifier(
let variant_indices = 0_u64..;
let fallthrough_msg = format!("{} index 0 <= i < {}", index_expecting, fields.len());
let visit_other = if collect_other_fields {
quote! {
Some(quote! {
fn visit_bool<__E>(self, __value: bool) -> _serde::export::Result<Self::Value, __E>
where
__E: _serde::de::Error,
Expand Down Expand Up @@ -2241,22 +2241,26 @@ fn deserialize_identifier(
}
}
}
}
})
} else {
quote! {
fn visit_u64<__E>(self, __value: u64) -> _serde::export::Result<Self::Value, __E>
where
__E: _serde::de::Error,
{
match __value {
#(
#field_ints => _serde::export::Ok(#constructors_ints),
)*
_ => _serde::export::Err(_serde::de::Error::invalid_value(
_serde::de::Unexpected::Unsigned(__value),
&#fallthrough_msg))
if constructors_ints.is_empty() {
None
} else {
Some(quote! {
fn visit_u64<__E>(self, __value: u64) -> _serde::export::Result<Self::Value, __E>
where
__E: _serde::de::Error,
{
match __value {
#(
#field_ints => _serde::export::Ok(#constructors_ints),
)*
_ => _serde::export::Err(_serde::de::Error::invalid_value(
_serde::de::Unexpected::Unsigned(__value),
&#fallthrough_msg))
}
}
}
})
}
};

Expand All @@ -2279,20 +2283,56 @@ fn deserialize_identifier(
quote!()
};

let visit_bool = quote! {
Some(quote! {
fn visit_bool<__E>(self, __value: bool) -> _serde::export::Result<Self::Value, __E>
where __E: _serde::de::Error
{
match __value {
where __E: _serde::de::Error
{
match __value {
#(
#field_bools => _serde::export::Ok(#constructors_bools),
)*
#fallthrough_true_arm
#fallthrough_false_arm
}
}
};
Some(visit_bool)
})
};

let visit_str_and_bytes = if constructors_strs.is_empty() {
None
} else {
Some(quote! {
fn visit_str<__E>(self, __value: &str) -> _serde::export::Result<Self::Value, __E>
where
__E: _serde::de::Error,
{
match __value {
#(
#field_strs => _serde::export::Ok(#constructors_strs),
)*
_ => {
#value_as_str_content
#fallthrough_arm
}
}
}

fn visit_bytes<__E>(self, __value: &[u8]) -> _serde::export::Result<Self::Value, __E>
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the visit_bytes use the constructors_strstoo so I put them together.

where
__E: _serde::de::Error,
{
match __value {
#(
#field_bytes => _serde::export::Ok(#constructors_strs),
)*
_ => {
#bytes_to_str
#value_as_bytes_content
#fallthrough_arm
}
}
}
})
};

quote_block! {
Expand All @@ -2304,36 +2344,7 @@ fn deserialize_identifier(

#visit_bool

fn visit_str<__E>(self, __value: &str) -> _serde::export::Result<Self::Value, __E>
where
__E: _serde::de::Error,
{
match __value {
#(
#field_strs => _serde::export::Ok(#constructors_strs),
)*
_ => {
#value_as_str_content
#fallthrough_arm
}
}
}

fn visit_bytes<__E>(self, __value: &[u8]) -> _serde::export::Result<Self::Value, __E>
where
__E: _serde::de::Error,
{
match __value {
#(
#field_bytes => _serde::export::Ok(#constructors_strs),
)*
_ => {
#bytes_to_str
#value_as_bytes_content
#fallthrough_arm
}
}
}
#visit_str_and_bytes
}
}

Expand Down