1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
//! Support for serializing fields as `serde` structs or maps.
use super::*;

#[derive(Debug)]
pub struct SerializeFieldMap<'a, T>(&'a T);

pub trait AsMap: Sized + sealed::Sealed {
    fn field_map(&self) -> SerializeFieldMap<'_, Self> {
        SerializeFieldMap(self)
    }
}

impl<'a> AsMap for Event<'a> {}
impl<'a> AsMap for Attributes<'a> {}
impl<'a> AsMap for Record<'a> {}

// === impl SerializeFieldMap ===

impl<'a> Serialize for SerializeFieldMap<'a, Event<'_>> {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let len = self.0.metadata().fields().len();
        let serializer = serializer.serialize_map(Some(len))?;
        let mut visitor = SerdeMapVisitor::new(serializer);
        self.0.record(&mut visitor);
        visitor.finish()
    }
}

impl<'a> Serialize for SerializeFieldMap<'a, Attributes<'_>> {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let len = self.0.metadata().fields().len();
        let serializer = serializer.serialize_map(Some(len))?;
        let mut visitor = SerdeMapVisitor::new(serializer);
        self.0.record(&mut visitor);
        visitor.finish()
    }
}

impl<'a> Serialize for SerializeFieldMap<'a, Record<'_>> {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let serializer = serializer.serialize_map(None)?;
        let mut visitor = SerdeMapVisitor::new(serializer);
        self.0.record(&mut visitor);
        visitor.finish()
    }
}