Fixed enum serializing

This commit is contained in:
Cerber Ursi
2021-11-27 19:09:12 +07:00
parent 89cf720cd4
commit 3327951f1f
3 changed files with 47 additions and 10 deletions
+3 -3
View File
@@ -150,7 +150,7 @@ impl<W: Write> ser::Serializer for &mut Corona<W> {
}
fn serialize_unit_struct(self, name: &'static str) -> SerResult {
write!(self.writer, "{}()", name)?;
write!(self.writer, "{}", name)?;
Ok(())
}
@@ -160,7 +160,7 @@ impl<W: Write> ser::Serializer for &mut Corona<W> {
_variant_index: u32,
variant: &'static str,
) -> SerResult {
write!(self.writer, "{}::{}()", name, variant)?;
write!(self.writer, "{}::{}", name, variant)?;
Ok(())
}
@@ -216,7 +216,7 @@ impl<W: Write> ser::Serializer for &mut Corona<W> {
variant: &'static str,
_len: usize,
) -> Result<Self::SerializeTupleVariant, Self::Error> {
write!(self.writer, "{}::{}", name, variant)?;
write!(self.writer, "{}::{}(", name, variant)?;
Ok(self.start_sub())
}
+40 -4
View File
@@ -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<f32>);
@@ -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<Enum>);
"""
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() },
])
}
"""
+4 -3
View File
@@ -9,7 +9,8 @@ use toml::from_str;
#[derive(Deserialize, Default)]
struct Data {
types: String,
main_type: String,
support_types: Option<String>,
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!(