Skip to content

Commit b1a2939

Browse files
authored
feat(yaml): store variant name in tuple and struct variants (#444)
1 parent 8a2c9a5 commit b1a2939

File tree

1 file changed

+62
-7
lines changed

1 file changed

+62
-7
lines changed

src/utils/yaml_ser.rs

Lines changed: 62 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ impl ser::Serializer for YamlSerializer {
4444
type SerializeSeq = SeqSerializer;
4545
type SerializeTuple = SeqSerializer;
4646
type SerializeTupleStruct = SeqSerializer;
47-
type SerializeTupleVariant = SeqSerializer;
47+
type SerializeTupleVariant = VariantSeqSerializer;
4848
type SerializeMap = MapSerializer;
4949
type SerializeStruct = MapSerializer;
50-
type SerializeStructVariant = MapSerializer;
50+
type SerializeStructVariant = VariantMapSerializer;
5151

5252
fn serialize_bool(self, v: bool) -> Result<Self::Ok, Self::Error> {
5353
Ok(Yaml::Boolean(v))
@@ -182,10 +182,11 @@ impl ser::Serializer for YamlSerializer {
182182
self,
183183
_name: &'static str,
184184
_variant_index: u32,
185-
_variant: &'static str,
185+
variant: &'static str,
186186
len: usize,
187187
) -> Result<Self::SerializeTupleVariant, Self::Error> {
188-
Ok(SeqSerializer {
188+
Ok(VariantSeqSerializer {
189+
variant_name: variant.to_owned(),
189190
vec: Vec::with_capacity(len),
190191
})
191192
}
@@ -209,10 +210,13 @@ impl ser::Serializer for YamlSerializer {
209210
self,
210211
_name: &'static str,
211212
_variant_index: u32,
212-
_variant: &'static str,
213-
len: usize,
213+
variant: &'static str,
214+
_len: usize,
214215
) -> Result<Self::SerializeStructVariant, Self::Error> {
215-
self.serialize_map(Some(len))
216+
Ok(VariantMapSerializer {
217+
variant_name: variant.to_owned(),
218+
map: yaml_rust2::yaml::Hash::new(),
219+
})
216220
}
217221
}
218222

@@ -347,3 +351,54 @@ impl ser::SerializeStructVariant for MapSerializer {
347351
ser::SerializeMap::end(self)
348352
}
349353
}
354+
355+
pub struct VariantMapSerializer {
356+
variant_name: String,
357+
map: yaml_rust2::yaml::Hash,
358+
}
359+
360+
impl ser::SerializeStructVariant for VariantMapSerializer {
361+
type Ok = Yaml;
362+
type Error = YamlSerializerError;
363+
364+
fn serialize_field<T>(&mut self, key: &'static str, value: &T) -> Result<(), Self::Error>
365+
where
366+
T: Serialize + ?Sized,
367+
{
368+
self.map.insert(
369+
Yaml::String(key.to_owned()),
370+
value.serialize(YamlSerializer)?,
371+
);
372+
Ok(())
373+
}
374+
375+
fn end(self) -> Result<Self::Ok, Self::Error> {
376+
let mut outer_map = yaml_rust2::yaml::Hash::new();
377+
outer_map.insert(Yaml::String(self.variant_name), Yaml::Hash(self.map));
378+
Ok(Yaml::Hash(outer_map))
379+
}
380+
}
381+
382+
pub struct VariantSeqSerializer {
383+
variant_name: String,
384+
vec: Vec<Yaml>,
385+
}
386+
387+
impl ser::SerializeTupleVariant for VariantSeqSerializer {
388+
type Ok = Yaml;
389+
type Error = YamlSerializerError;
390+
391+
fn serialize_field<T>(&mut self, value: &T) -> Result<(), Self::Error>
392+
where
393+
T: Serialize + ?Sized,
394+
{
395+
self.vec.push(value.serialize(YamlSerializer)?);
396+
Ok(())
397+
}
398+
399+
fn end(self) -> Result<Self::Ok, Self::Error> {
400+
let mut map = yaml_rust2::yaml::Hash::new();
401+
map.insert(Yaml::String(self.variant_name), Yaml::Array(self.vec));
402+
Ok(Yaml::Hash(map))
403+
}
404+
}

0 commit comments

Comments
 (0)