Serialize to binary if the serde format is not human readable

This implements the KISS suggested in https://github.com/serde-rs/serde/issues/790.
It is possible that one of the other approaches may be better but this
seemed like the simplest one to reignite som discussion.

Personally I find the original suggestion of adding two traits perhaps slightly
cleaner in theory but I think it ends up more complicated in the end
since the added traits also need to be duplicated to to the `Seed`
traits.

Closes #790
This commit is contained in:
Markus Westerlind
2017-09-07 15:56:22 +02:00
parent d4042872f5
commit 0dccbb1f11
8 changed files with 115 additions and 8 deletions
+10 -1
View File
@@ -15,12 +15,17 @@ use token::Token;
#[derive(Debug)]
pub struct Serializer<'a> {
tokens: &'a [Token],
is_human_readable: bool,
}
impl<'a> Serializer<'a> {
/// Creates the serializer.
pub fn new(tokens: &'a [Token]) -> Self {
Serializer { tokens: tokens }
Serializer::readable(tokens, true)
}
pub fn readable(tokens: &'a [Token], is_human_readable: bool) -> Self {
Serializer { tokens: tokens, is_human_readable: is_human_readable }
}
/// Pulls the next token off of the serializer, ignoring it.
@@ -282,6 +287,10 @@ impl<'s, 'a> ser::Serializer for &'s mut Serializer<'a> {
Ok(Variant { ser: self, end: Token::StructVariantEnd })
}
}
fn is_human_readable(&self) -> bool {
self.is_human_readable
}
}
pub struct Variant<'s, 'a: 's> {