Issue 3: fixed unevaluating of empty complex items

This commit is contained in:
Cerber Ursi
2021-12-02 23:58:39 +07:00
parent 2ab7891f70
commit 9dbc6b5c1d
2 changed files with 52 additions and 3 deletions
+8 -1
View File
@@ -16,7 +16,7 @@ pub struct Uneval<W: Write> {
} }
impl<W: Write> Uneval<W> { impl<W: Write> Uneval<W> {
pub(crate) fn new(target: W) -> Self { pub fn new(target: W) -> Self {
Self { Self {
writer: target, writer: target,
inside: false, inside: false,
@@ -267,6 +267,7 @@ impl<W: Write> ser::SerializeSeq for &mut Uneval<W> {
fn end(self) -> SerResult { fn end(self) -> SerResult {
write!(self.writer, "].into_iter().collect()")?; write!(self.writer, "].into_iter().collect()")?;
self.inside = true;
Ok(()) Ok(())
} }
} }
@@ -283,6 +284,7 @@ impl<W: Write> ser::SerializeTuple for &mut Uneval<W> {
fn end(self) -> SerResult { fn end(self) -> SerResult {
write!(self.writer, ")) }}")?; write!(self.writer, ")) }}")?;
self.inside = true;
Ok(()) Ok(())
} }
} }
@@ -299,6 +301,7 @@ impl<W: Write> ser::SerializeTupleStruct for &mut Uneval<W> {
fn end(self) -> SerResult { fn end(self) -> SerResult {
write!(self.writer, ")")?; write!(self.writer, ")")?;
self.inside = true;
Ok(()) Ok(())
} }
} }
@@ -315,6 +318,7 @@ impl<W: Write> ser::SerializeTupleVariant for &mut Uneval<W> {
fn end(self) -> SerResult { fn end(self) -> SerResult {
write!(self.writer, ")")?; write!(self.writer, ")")?;
self.inside = true;
Ok(()) Ok(())
} }
} }
@@ -344,6 +348,7 @@ impl<W: Write> ser::SerializeMap for &mut Uneval<W> {
fn end(self) -> SerResult { fn end(self) -> SerResult {
write!(self.writer, "].into_iter().collect()")?; write!(self.writer, "].into_iter().collect()")?;
self.inside = true;
Ok(()) Ok(())
} }
} }
@@ -367,6 +372,7 @@ impl<W: Write> ser::SerializeStruct for &mut Uneval<W> {
fn end(self) -> SerResult { fn end(self) -> SerResult {
write!(self.writer, "}}")?; write!(self.writer, "}}")?;
self.inside = true;
Ok(()) Ok(())
} }
} }
@@ -390,6 +396,7 @@ impl<W: Write> ser::SerializeStructVariant for &mut Uneval<W> {
fn end(self) -> SerResult { fn end(self) -> SerResult {
write!(self.writer, "}}")?; write!(self.writer, "}}")?;
self.inside = true;
Ok(()) Ok(())
} }
} }
+44 -2
View File
@@ -93,6 +93,8 @@ pub enum Enum {
Unit(Unit), Unit(Unit),
Tuple(i32, String), Tuple(i32, String),
Struct{ key: i32, value: String }, Struct{ key: i32, value: String },
EmptyTuple(),
EmptyStruct {},
} }
#[derive(PartialEq, Debug, Serialize)] #[derive(PartialEq, Debug, Serialize)]
@@ -102,10 +104,13 @@ value = """
{ {
use definition::*; use definition::*;
Container(vec![ Container(vec![
Enum::Empty,
Enum::Unit(Unit), Enum::Unit(Unit),
Enum::Tuple(1, "test".into()), Enum::Tuple(1, "test".into()),
Enum::Struct{ key: 1, value: "test".into() }, Enum::Struct{ key: 1, value: "test".into() },
Enum::EmptyTuple(),
Enum::EmptyStruct{},
// this is intentionally last, to catch an error at previous step
Enum::Empty,
]) ])
} }
""" """
@@ -128,4 +133,41 @@ pub struct ZeroSizeContainer(pub [u8; 0]);
""" """
value = """ value = """
definition::ZeroSizeContainer([]) definition::ZeroSizeContainer([])
""" """
[empty_fields_issue_3]
main_type = "Foo"
support_types = "UnitType,UnitStruct,UnitTuple"
definition = """
use std::collections::HashMap;
#[derive(PartialEq, Debug, Serialize)]
pub struct UnitType;
#[derive(PartialEq, Debug, Serialize)]
pub struct UnitStruct {}
#[derive(PartialEq, Debug, Serialize)]
pub struct UnitTuple();
#[derive(PartialEq, Debug, Serialize)]
pub struct Foo {
pub vec: Vec<String>,
pub map: HashMap<String, String>,
pub unit: (),
pub unit_type: UnitType,
pub unit_struct: UnitStruct,
pub unit_tuple: UnitTuple,
pub last: String
}
"""
value = """
{
use definition::*;
Foo {
vec: vec![],
map: std::collections::HashMap::new(),
unit: (),
unit_type: UnitType,
unit_struct: UnitStruct {},
unit_tuple: UnitTuple(),
last: "".into()
}
}
"""