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 34 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 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
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
61 changes: 51 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,47 @@ where
}
}

pub enum Integer {
U8(u8),
U16(u16),
U32(u32),
U64(u64),

I8(i8),
I16(i16),
I32(i32),
I64(i64),
}

pub enum VariantName {
String(&'static str),
Integer(Integer),
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) => match *i {
Integer::U8(i) => serializer.serialize_u8(i),
Integer::U16(i) => serializer.serialize_u16(i),
Integer::U32(i) => serializer.serialize_u32(i),
Integer::U64(i) => serializer.serialize_u64(i),

Integer::I8(i) => serializer.serialize_i8(i),
Integer::I16(i) => serializer.serialize_i16(i),
Integer::I32(i) => serializer.serialize_i32(i),
Integer::I64(i) => serializer.serialize_i64(i),
},
VariantName::Boolean(b) => serializer.serialize_bool(*b),
}
}
}

pub struct AdjacentlyTaggedEnumVariant {
pub enum_name: &'static str,
pub variant_index: u32,
Expand Down