serde_content/ser/
struct.rs

1use crate::ser::Value;
2use crate::Data;
3use crate::Error;
4use crate::Serializer;
5use alloc::borrow::Cow;
6use alloc::boxed::Box;
7use serde::ser;
8use serde::ser::SerializeMap;
9use serde::ser::SerializeStruct;
10use serde::ser::SerializeTupleStruct;
11
12use super::to_static_str;
13
14pub struct Struct<'a> {
15    r#struct: crate::Struct<'a>,
16    human_readable: bool,
17}
18
19impl<'a> Struct<'a> {
20    pub(super) const fn new(r#struct: crate::Struct<'a>, human_readable: bool) -> Self {
21        Self {
22            r#struct,
23            human_readable,
24        }
25    }
26}
27
28impl ser::Serialize for crate::Struct<'_> {
29    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
30    where
31        S: ser::Serializer,
32    {
33        match &self.data {
34            Data::Unit => match &self.name {
35                Cow::Borrowed(name) => serializer.serialize_unit_struct(name),
36                Cow::Owned(_) => serializer.serialize_unit(),
37            },
38            Data::NewType { value } => match &self.name {
39                Cow::Borrowed(name) => serializer.serialize_newtype_struct(name, value),
40                Cow::Owned(_) => value.serialize(serializer),
41            },
42            Data::Tuple { values } => match &self.name {
43                Cow::Borrowed(name) => {
44                    let mut tup = serializer.serialize_tuple_struct(name, values.len())?;
45                    for value in values {
46                        tup.serialize_field(value)?;
47                    }
48                    tup.end()
49                }
50                Cow::Owned(_) => values.serialize(serializer),
51            },
52            Data::Struct { fields } => match &self.name {
53                Cow::Borrowed(name) => {
54                    let mut map = serializer.serialize_struct(name, fields.len())?;
55                    for (key, value) in fields {
56                        let key = to_static_str(key)?;
57                        map.serialize_field(key, value)?;
58                    }
59                    map.end()
60                }
61                Cow::Owned(_) => {
62                    let mut map = serializer.serialize_map(Some(fields.len()))?;
63                    for (key, value) in fields {
64                        map.serialize_entry(key, value)?;
65                    }
66                    map.end()
67                }
68            },
69        }
70    }
71}
72
73impl<'a> ser::SerializeStruct for Struct<'a> {
74    type Ok = Value<'a>;
75    type Error = Error;
76
77    fn serialize_field<T>(&mut self, key: &'static str, value: &T) -> Result<(), Error>
78    where
79        T: ?Sized + ser::Serialize,
80    {
81        if let Data::Struct { fields } = &mut self.r#struct.data {
82            let value = value.serialize(Serializer::with_human_readable(self.human_readable))?;
83            fields.push((Cow::Borrowed(key), value));
84        }
85        Ok(())
86    }
87
88    fn end(self) -> Result<Self::Ok, Error> {
89        Ok(Value::Struct(Box::new(self.r#struct)))
90    }
91}
92
93impl<'a> ser::SerializeTupleStruct for Struct<'a> {
94    type Ok = Value<'a>;
95    type Error = Error;
96
97    fn serialize_field<T>(&mut self, value: &T) -> Result<(), Error>
98    where
99        T: ?Sized + ser::Serialize,
100    {
101        if let Data::Tuple { values } = &mut self.r#struct.data {
102            let value = value.serialize(Serializer::with_human_readable(self.human_readable))?;
103            values.push(value);
104        }
105        Ok(())
106    }
107
108    fn end(self) -> Result<Self::Ok, Error> {
109        Ok(Value::Struct(Box::new(self.r#struct)))
110    }
111}