diff --git a/src/ser.rs b/src/ser.rs index 242fdc4..7c28585 100644 --- a/src/ser.rs +++ b/src/ser.rs @@ -150,7 +150,7 @@ impl ser::Serializer for &mut Corona { } fn serialize_unit_struct(self, name: &'static str) -> SerResult { - write!(self.writer, "{}()", name)?; + write!(self.writer, "{}", name)?; Ok(()) } @@ -160,7 +160,7 @@ impl ser::Serializer for &mut Corona { _variant_index: u32, variant: &'static str, ) -> SerResult { - write!(self.writer, "{}::{}()", name, variant)?; + write!(self.writer, "{}::{}", name, variant)?; Ok(()) } @@ -216,7 +216,7 @@ impl ser::Serializer for &mut Corona { variant: &'static str, _len: usize, ) -> Result { - write!(self.writer, "{}::{}", name, variant)?; + write!(self.writer, "{}::{}(", name, variant)?; Ok(self.start_sub()) } diff --git a/test_fixtures/data.toml b/test_fixtures/data.toml index a65afa3..6fa4a2f 100644 --- a/test_fixtures/data.toml +++ b/test_fixtures/data.toml @@ -1,5 +1,5 @@ [simple] -types = "Simple" +main_type = "Simple" definition = """ #[derive(PartialEq, Debug, Serialize)] pub struct Simple { @@ -21,7 +21,8 @@ definition::Simple { """ [nested] -types = "Nested,Inner" +main_type = "Nested" +support_types = "Inner" definition = """ #[derive(PartialEq, Debug, Serialize)] pub struct Nested { @@ -39,8 +40,9 @@ definition::Nested { } } """ + [complex] -types = "Complex" +main_type = "Complex" definition = """ use std::collections::HashMap; #[derive(PartialEq, Debug, Serialize)] @@ -62,8 +64,9 @@ value = """ } } """ + [floats] -types = "Floats" +main_type = "Floats" definition = """ #[derive(PartialEq, Debug, Serialize)] pub struct Floats(pub Vec); @@ -74,4 +77,37 @@ definition::Floats(vec![ 123456789e-30, 12345.6789 ]) +""" + +[enums] +main_type = "Container" +support_types = "Enum,Unit,FuncUnit" +definition = """ +#[derive(PartialEq, Debug, Serialize)] +pub struct Unit; +#[derive(PartialEq, Debug, Serialize)] +pub struct FuncUnit; +#[derive(PartialEq, Debug, Serialize)] +pub enum Enum { + Empty, + Unit(Unit), + FuncUnit(FuncUnit), + Tuple(i32, String), + Struct{ key: i32, value: String }, +} + +#[derive(PartialEq, Debug, Serialize)] +pub struct Container(pub Vec); +""" +value = """ +{ + use definition::*; + Container(vec![ + Enum::Empty, + Enum::Unit(Unit), + Enum::FuncUnit(FuncUnit), + Enum::Tuple(1, "test".into()), + Enum::Struct{ key: 1, value: "test".into() }, + ]) +} """ \ No newline at end of file diff --git a/tests/generate_and_use.rs b/tests/generate_and_use.rs index 666859c..10ff2ed 100644 --- a/tests/generate_and_use.rs +++ b/tests/generate_and_use.rs @@ -9,7 +9,8 @@ use toml::from_str; #[derive(Deserialize, Default)] struct Data { - types: String, + main_type: String, + support_types: Option, definition: String, value: String, } @@ -38,8 +39,8 @@ impl Data { write!( File::create(&path.with_file_name(format!("{}-user.rs", name))).unwrap(), include_str!("user.tpl"), - types = self.types, - ser_type = self.types.split(",").next().unwrap(), + types = self.support_types.as_ref().map_or(self.main_type.clone(), |types| format!("{},{}", self.main_type, types)), + ser_type = self.main_type, value = self.value ).unwrap(); write!(