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

Integer/boolean tags for internally/adjacently tagged enums #2056

Closed
Show file tree
Hide file tree
Changes from 13 commits
Commits
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
23 changes: 21 additions & 2 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: &'static VariantName,
AmaranthineCodices marked this conversation as resolved.
Show resolved Hide resolved
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: &'static VariantName,
delegate: S,
}

Expand Down Expand Up @@ -1308,3 +1308,22 @@ where
Ok(())
}
}

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),
VariantName::Boolean(b) => serializer.serialize_bool(*b),
}
}
}
Loading