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

UPDATED: Integer/boolean tags for internally/adjacently tagged enums #2525

Open
wants to merge 37 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 27 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
02d32f6
initial work
AmaranthineCodices Jul 11, 2021
e698800
clean up comments
AmaranthineCodices Jul 11, 2021
fbdb030
fold VariantNames and Name into single generic struct
AmaranthineCodices Jul 11, 2021
f162636
make adjacent enums serialize
AmaranthineCodices Jul 11, 2021
37b4c68
add test for adjacent enum
AmaranthineCodices Jul 11, 2021
b782173
add ui test for non-string rename on invalid enums
AmaranthineCodices Jul 11, 2021
5d425a0
fix missing_true_arm cond
AmaranthineCodices Jul 11, 2021
cb3c394
fix pattern when we don't generate a bool fallthrough arm
AmaranthineCodices Jul 12, 2021
55b9d71
fix use of type_alias_enum_variants on old rustc
AmaranthineCodices Jul 12, 2021
9855884
maybe fix no_std build
AmaranthineCodices Jul 12, 2021
9869d1c
fix clippy lints
AmaranthineCodices Jul 12, 2021
dcf880d
__value_bytes, not bytes
AmaranthineCodices Jul 12, 2021
7821a30
fix duplicate visit_u64 impl
AmaranthineCodices Jul 12, 2021
52f5d91
handle collect_other_fields properly
AmaranthineCodices Jul 13, 2021
69f6848
cut down on code duplication
AmaranthineCodices Jul 13, 2021
61404e9
add test for non-string aliases
AmaranthineCodices Jul 15, 2021
47ac87c
remove &'static reference to VariantName
AmaranthineCodices Jul 15, 2021
101c4a8
merge serde
Astavie Jul 23, 2023
24565f5
run rustfmt
Astavie Jul 23, 2023
4b653b4
Merge branch 'master' into integer-tags-for-enums
Astavie Jul 26, 2023
cc90915
Merge branch 'master' into integer-tags-for-enums
Astavie Jul 28, 2023
ec2b8dc
Fix case.rs
Astavie Jul 28, 2023
0b792d7
Merge branch 'master' into integer-tags-for-enums
Astavie Jul 31, 2023
e36c9d8
Fix test_annotations.rs
Astavie Jul 31, 2023
60a0906
Merge branch 'master' into integer-tags-for-enums
Astavie Aug 2, 2023
181d85f
Update de.rs
Astavie Aug 2, 2023
c53a298
Fix AdjacentlyTaggedEnumVariant
Astavie Aug 4, 2023
0b29c96
Merge branch 'master' into integer-tags-for-enums
Astavie Sep 22, 2023
f126738
complete merge
Astavie Sep 22, 2023
f1a316c
tidy up VariantNames and VariantMix
Astavie Sep 22, 2023
282890f
improve type names
Astavie Sep 22, 2023
f552a79
remove DeserializeIdentifier for i64 and bool
Astavie Oct 12, 2023
19f108a
feat: support different int representations
Astavie Feb 8, 2024
3e2ca55
remove use of is_some_and
Astavie Feb 8, 2024
1ba8b09
Merge branch 'master' into integer-tags-for-enums
Astavie Jun 30, 2024
050cf1e
move non-string error message to highlight rename attr
Astavie Jul 15, 2024
e210c70
Merge branch 'master' into integer-tags-for-enums
Astavie Aug 17, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions serde/src/private/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2587,6 +2587,28 @@ where
}
}

impl<'de, E> IdentifierDeserializer<'de, E> for i64
where
E: Error,
{
type Deserializer = <i64 as IntoDeserializer<'de, E>>::Deserializer;

fn from(self) -> Self::Deserializer {
self.into_deserializer()
}
}

impl<'de, E> IdentifierDeserializer<'de, E> for bool
Astavie marked this conversation as resolved.
Show resolved Hide resolved
where
E: Error,
{
type Deserializer = <bool as IntoDeserializer<'de, E>>::Deserializer;

fn from(self) -> Self::Deserializer {
self.into_deserializer()
}
}

#[cfg(any(feature = "std", feature = "alloc"))]
pub struct FlatMapDeserializer<'a, 'de: 'a, E>(
pub &'a mut Vec<Option<(Content<'de>, Content<'de>)>>,
Expand Down
39 changes: 29 additions & 10 deletions serde/src/private/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub fn serialize_tagged_newtype<S, T>(
type_ident: &'static str,
variant_ident: &'static str,
tag: &'static str,
variant_name: &'static str,
variant_name: VariantName,
value: &T,
) -> Result<S::Ok, S::Error>
where
Expand All @@ -39,7 +39,7 @@ struct TaggedSerializer<S> {
type_ident: &'static str,
variant_ident: &'static str,
tag: &'static str,
variant_name: &'static str,
variant_name: VariantName,
delegate: S,
}

Expand Down Expand Up @@ -183,13 +183,13 @@ where

fn serialize_unit(self) -> Result<Self::Ok, Self::Error> {
let mut map = tri!(self.delegate.serialize_map(Some(1)));
tri!(map.serialize_entry(self.tag, self.variant_name));
tri!(map.serialize_entry(self.tag, &self.variant_name));
map.end()
}

fn serialize_unit_struct(self, _: &'static str) -> Result<Self::Ok, Self::Error> {
let mut map = tri!(self.delegate.serialize_map(Some(1)));
tri!(map.serialize_entry(self.tag, self.variant_name));
tri!(map.serialize_entry(self.tag, &self.variant_name));
map.end()
}

Expand All @@ -200,7 +200,7 @@ where
inner_variant: &'static str,
) -> Result<Self::Ok, Self::Error> {
let mut map = tri!(self.delegate.serialize_map(Some(2)));
tri!(map.serialize_entry(self.tag, self.variant_name));
tri!(map.serialize_entry(self.tag, &self.variant_name));
tri!(map.serialize_entry(inner_variant, &()));
map.end()
}
Expand All @@ -227,7 +227,7 @@ where
T: Serialize,
{
let mut map = tri!(self.delegate.serialize_map(Some(2)));
tri!(map.serialize_entry(self.tag, self.variant_name));
tri!(map.serialize_entry(self.tag, &self.variant_name));
tri!(map.serialize_entry(inner_variant, inner_value));
map.end()
}
Expand Down Expand Up @@ -270,7 +270,7 @@ where
len: usize,
) -> Result<Self::SerializeTupleVariant, Self::Error> {
let mut map = tri!(self.delegate.serialize_map(Some(2)));
tri!(map.serialize_entry(self.tag, self.variant_name));
tri!(map.serialize_entry(self.tag, &self.variant_name));
tri!(map.serialize_key(inner_variant));
Ok(SerializeTupleVariantAsMapValue::new(
map,
Expand All @@ -281,7 +281,7 @@ where

fn serialize_map(self, len: Option<usize>) -> Result<Self::SerializeMap, Self::Error> {
let mut map = tri!(self.delegate.serialize_map(len.map(|len| len + 1)));
tri!(map.serialize_entry(self.tag, self.variant_name));
tri!(map.serialize_entry(self.tag, &self.variant_name));
Ok(map)
}

Expand All @@ -291,7 +291,7 @@ where
len: usize,
) -> Result<Self::SerializeStruct, Self::Error> {
let mut state = tri!(self.delegate.serialize_struct(name, len + 1));
tri!(state.serialize_field(self.tag, self.variant_name));
tri!(state.serialize_field(self.tag, &self.variant_name));
Ok(state)
}

Expand All @@ -317,7 +317,7 @@ where
len: usize,
) -> Result<Self::SerializeStructVariant, Self::Error> {
let mut map = tri!(self.delegate.serialize_map(Some(2)));
tri!(map.serialize_entry(self.tag, self.variant_name));
tri!(map.serialize_entry(self.tag, &self.variant_name));
tri!(map.serialize_key(inner_variant));
Ok(SerializeStructVariantAsMapValue::new(
map,
Expand Down Expand Up @@ -1356,6 +1356,25 @@ where
}
}

pub enum VariantName {
String(&'static str),
Integer(i64),
Boolean(bool),
}

impl Serialize for VariantName {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
match self {
VariantName::String(s) => serializer.serialize_str(s),
VariantName::Integer(i) => serializer.serialize_i64(*i),
Astavie marked this conversation as resolved.
Show resolved Hide resolved
VariantName::Boolean(b) => serializer.serialize_bool(*b),
}
}
}

pub struct AdjacentlyTaggedEnumVariant {
pub enum_name: &'static str,
pub variant_index: u32,
Expand Down
4 changes: 2 additions & 2 deletions serde_derive/src/bound.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ pub fn with_where_predicates_from_variants(
from_variant: fn(&attr::Variant) -> Option<&[syn::WherePredicate]>,
) -> syn::Generics {
let variants = match &cont.data {
Data::Enum(variants) => variants,
Data::Enum(_, variants) => variants,
Data::Struct(_, _) => {
return generics.clone();
}
Expand Down Expand Up @@ -255,7 +255,7 @@ pub fn with_bound(
associated_type_usage: Vec::new(),
};
match &cont.data {
Data::Enum(variants) => {
Data::Enum(_, variants) => {
for variant in variants {
let relevant_fields = variant
.fields
Expand Down
Loading