Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 62 additions & 7 deletions src/utils/yaml_ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ impl ser::Serializer for YamlSerializer {
type SerializeSeq = SeqSerializer;
type SerializeTuple = SeqSerializer;
type SerializeTupleStruct = SeqSerializer;
type SerializeTupleVariant = SeqSerializer;
type SerializeTupleVariant = VariantSeqSerializer;
type SerializeMap = MapSerializer;
type SerializeStruct = MapSerializer;
type SerializeStructVariant = MapSerializer;
type SerializeStructVariant = VariantMapSerializer;

fn serialize_bool(self, v: bool) -> Result<Self::Ok, Self::Error> {
Ok(Yaml::Boolean(v))
Expand Down Expand Up @@ -182,10 +182,11 @@ impl ser::Serializer for YamlSerializer {
self,
_name: &'static str,
_variant_index: u32,
_variant: &'static str,
variant: &'static str,
len: usize,
) -> Result<Self::SerializeTupleVariant, Self::Error> {
Ok(SeqSerializer {
Ok(VariantSeqSerializer {
variant_name: variant.to_owned(),
vec: Vec::with_capacity(len),
})
}
Expand All @@ -209,10 +210,13 @@ impl ser::Serializer for YamlSerializer {
self,
_name: &'static str,
_variant_index: u32,
_variant: &'static str,
len: usize,
variant: &'static str,
_len: usize,
) -> Result<Self::SerializeStructVariant, Self::Error> {
self.serialize_map(Some(len))
Ok(VariantMapSerializer {
variant_name: variant.to_owned(),
map: yaml_rust2::yaml::Hash::new(),
})
}
}

Expand Down Expand Up @@ -347,3 +351,54 @@ impl ser::SerializeStructVariant for MapSerializer {
ser::SerializeMap::end(self)
}
}

pub struct VariantMapSerializer {
variant_name: String,
map: yaml_rust2::yaml::Hash,
}

impl ser::SerializeStructVariant for VariantMapSerializer {
type Ok = Yaml;
type Error = YamlSerializerError;

fn serialize_field<T>(&mut self, key: &'static str, value: &T) -> Result<(), Self::Error>
where
T: Serialize + ?Sized,
{
self.map.insert(
Yaml::String(key.to_owned()),
value.serialize(YamlSerializer)?,
);
Ok(())
}

fn end(self) -> Result<Self::Ok, Self::Error> {
let mut outer_map = yaml_rust2::yaml::Hash::new();
outer_map.insert(Yaml::String(self.variant_name), Yaml::Hash(self.map));
Ok(Yaml::Hash(outer_map))
}
}

pub struct VariantSeqSerializer {
variant_name: String,
vec: Vec<Yaml>,
}

impl ser::SerializeTupleVariant for VariantSeqSerializer {
type Ok = Yaml;
type Error = YamlSerializerError;

fn serialize_field<T>(&mut self, value: &T) -> Result<(), Self::Error>
where
T: Serialize + ?Sized,
{
self.vec.push(value.serialize(YamlSerializer)?);
Ok(())
}

fn end(self) -> Result<Self::Ok, Self::Error> {
let mut map = yaml_rust2::yaml::Hash::new();
map.insert(Yaml::String(self.variant_name), Yaml::Array(self.vec));
Ok(Yaml::Hash(map))
}
}