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
Show file tree
Hide file tree
Changes from 11 commits
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
18 changes: 17 additions & 1 deletion serde/src/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ pub use lib::marker::PhantomData;
pub use lib::option::Option::{self, None, Some};
pub use lib::result::Result::{self, Err, Ok};

pub use self::string::from_utf8_lossy;
pub use self::string::{from_utf8_lossy, from_int, from_bool};

#[cfg(any(feature = "alloc", feature = "std"))]
pub use lib::Vec;

mod string {
use lib::*;
use lib::fmt::Write;

#[cfg(any(feature = "std", feature = "alloc"))]
pub fn from_utf8_lossy(bytes: &[u8]) -> Cow<str> {
Expand All @@ -41,4 +42,19 @@ mod string {
// UTF-8.
str::from_utf8(bytes).unwrap_or("\u{fffd}\u{fffd}\u{fffd}")
}

pub fn from_bool(b : bool) -> &'static str {
if b {
"true"
} else {
"false"
}
}

pub fn from_int(i: u64) -> Vec<u8> {
let mut buf = String::with_capacity(20);

write!(&mut buf, "{}", i).ok();
buf.into_bytes()
}
}
72 changes: 61 additions & 11 deletions serde/src/private/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,74 @@ pub fn constrain<T: ?Sized>(t: &T) -> &T {
t
}

pub enum VariantNameType {
Str(&'static str),
Bool(bool),
Int(u64),
}

impl From<&'static str> for VariantNameType {
fn from(src: &'static str) -> Self {
VariantNameType::Str(src)
}
}

impl<'a> From<&'a bool> for VariantNameType {
fn from(src: &bool) -> Self {
VariantNameType::Bool(*src)
}
}

impl<'a> From<&'a u64> for VariantNameType {
fn from(src: &u64) -> Self {
VariantNameType::Int(*src)
}
}

impl From<bool> for VariantNameType {
fn from(src: bool) -> Self {
VariantNameType::Bool(src)
}
}

impl From<u64> for VariantNameType {
fn from(src: u64) -> Self {
VariantNameType::Int(src)
}
}

impl Serialize for VariantNameType {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer
{
match *self {
VariantNameType::Str(s) => serializer.serialize_str(s),
VariantNameType::Bool(b) => serializer.serialize_bool(b),
VariantNameType::Int(i) => serializer.serialize_u64(i),
}
}
}

/// Not public API.
pub fn serialize_tagged_newtype<S, T>(
pub fn serialize_tagged_newtype<S, T, U>(
serializer: S,
type_ident: &'static str,
variant_ident: &'static str,
tag: &'static str,
variant_name: &'static str,
variant_name: U,
value: &T,
) -> Result<S::Ok, S::Error>
where
S: Serializer,
T: Serialize,
U: Into<VariantNameType>,
{
value.serialize(TaggedSerializer {
type_ident: type_ident,
variant_ident: variant_ident,
tag: tag,
variant_name: variant_name,
variant_name: variant_name.into(),
delegate: serializer,
})
}
Expand All @@ -47,7 +97,7 @@ struct TaggedSerializer<S> {
type_ident: &'static str,
variant_ident: &'static str,
tag: &'static str,
variant_name: &'static str,
variant_name: VariantNameType,
delegate: S,
}

Expand Down Expand Up @@ -197,7 +247,7 @@ where

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

Expand All @@ -208,7 +258,7 @@ where
inner_variant: &'static str,
) -> Result<Self::Ok, Self::Error> {
let mut map = try!(self.delegate.serialize_map(Some(2)));
try!(map.serialize_entry(self.tag, self.variant_name));
try!(map.serialize_entry(self.tag, &self.variant_name));
try!(map.serialize_entry(inner_variant, &()));
map.end()
}
Expand All @@ -235,7 +285,7 @@ where
T: Serialize,
{
let mut map = try!(self.delegate.serialize_map(Some(2)));
try!(map.serialize_entry(self.tag, self.variant_name));
try!(map.serialize_entry(self.tag, &self.variant_name));
try!(map.serialize_entry(inner_variant, inner_value));
map.end()
}
Expand Down Expand Up @@ -278,7 +328,7 @@ where
len: usize,
) -> Result<Self::SerializeTupleVariant, Self::Error> {
let mut map = try!(self.delegate.serialize_map(Some(2)));
try!(map.serialize_entry(self.tag, self.variant_name));
try!(map.serialize_entry(self.tag, &self.variant_name));
try!(map.serialize_key(inner_variant));
Ok(SerializeTupleVariantAsMapValue::new(
map,
Expand All @@ -289,7 +339,7 @@ where

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

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

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