1
use std::fmt;
2

            
3
use serde::{ser, Serialize};
4

            
5
use super::{Error, Result};
6

            
7
pub struct Serializer<'a, W: fmt::Write> {
8
    ser: &'a mut super::Serializer<W>,
9
}
10

            
11
impl<'a, W: fmt::Write> Serializer<'a, W> {
12
196
    pub fn new(ser: &'a mut super::Serializer<W>) -> Self {
13
196
        Self { ser }
14
196
    }
15
}
16

            
17
impl<'a, W: fmt::Write> ser::Serializer for Serializer<'a, W> {
18
    type Error = Error;
19
    type Ok = ();
20
    type SerializeMap = ser::Impossible<(), Error>;
21
    type SerializeSeq = ser::Impossible<(), Error>;
22
    type SerializeStruct = ser::Impossible<(), Error>;
23
    type SerializeStructVariant = ser::Impossible<(), Error>;
24
    type SerializeTuple = ser::Impossible<(), Error>;
25
    type SerializeTupleStruct = ser::Impossible<(), Error>;
26
    type SerializeTupleVariant = ser::Impossible<(), Error>;
27

            
28
4
    fn serialize_bool(self, _: bool) -> Result<()> {
29
4
        Err(Error::ExpectedRawValue)
30
4
    }
31

            
32
4
    fn serialize_i8(self, _: i8) -> Result<()> {
33
4
        Err(Error::ExpectedRawValue)
34
4
    }
35

            
36
4
    fn serialize_i16(self, _: i16) -> Result<()> {
37
4
        Err(Error::ExpectedRawValue)
38
4
    }
39

            
40
4
    fn serialize_i32(self, _: i32) -> Result<()> {
41
4
        Err(Error::ExpectedRawValue)
42
4
    }
43

            
44
4
    fn serialize_i64(self, _: i64) -> Result<()> {
45
4
        Err(Error::ExpectedRawValue)
46
4
    }
47

            
48
    #[cfg(feature = "integer128")]
49
2
    fn serialize_i128(self, _: i128) -> Result<()> {
50
2
        Err(Error::ExpectedRawValue)
51
2
    }
52

            
53
4
    fn serialize_u8(self, _: u8) -> Result<()> {
54
4
        Err(Error::ExpectedRawValue)
55
4
    }
56

            
57
4
    fn serialize_u16(self, _: u16) -> Result<()> {
58
4
        Err(Error::ExpectedRawValue)
59
4
    }
60

            
61
4
    fn serialize_u32(self, _: u32) -> Result<()> {
62
4
        Err(Error::ExpectedRawValue)
63
4
    }
64

            
65
4
    fn serialize_u64(self, _: u64) -> Result<()> {
66
4
        Err(Error::ExpectedRawValue)
67
4
    }
68

            
69
    #[cfg(feature = "integer128")]
70
2
    fn serialize_u128(self, _: u128) -> Result<()> {
71
2
        Err(Error::ExpectedRawValue)
72
2
    }
73

            
74
4
    fn serialize_f32(self, _: f32) -> Result<()> {
75
4
        Err(Error::ExpectedRawValue)
76
4
    }
77

            
78
4
    fn serialize_f64(self, _: f64) -> Result<()> {
79
4
        Err(Error::ExpectedRawValue)
80
4
    }
81

            
82
4
    fn serialize_char(self, _: char) -> Result<()> {
83
4
        Err(Error::ExpectedRawValue)
84
4
    }
85

            
86
84
    fn serialize_str(self, ron: &str) -> Result<()> {
87
84
        self.ser.output.write_str(ron)?;
88
84
        Ok(())
89
84
    }
90

            
91
4
    fn serialize_bytes(self, _: &[u8]) -> Result<()> {
92
4
        Err(Error::ExpectedRawValue)
93
4
    }
94

            
95
4
    fn serialize_none(self) -> Result<()> {
96
4
        Err(Error::ExpectedRawValue)
97
4
    }
98

            
99
4
    fn serialize_some<T: ?Sized + Serialize>(self, _: &T) -> Result<()> {
100
4
        Err(Error::ExpectedRawValue)
101
4
    }
102

            
103
4
    fn serialize_unit(self) -> Result<()> {
104
4
        Err(Error::ExpectedRawValue)
105
4
    }
106

            
107
4
    fn serialize_unit_struct(self, _: &'static str) -> Result<()> {
108
4
        Err(Error::ExpectedRawValue)
109
4
    }
110

            
111
4
    fn serialize_unit_variant(self, _: &'static str, _: u32, _: &'static str) -> Result<()> {
112
4
        Err(Error::ExpectedRawValue)
113
4
    }
114

            
115
4
    fn serialize_newtype_struct<T: ?Sized + Serialize>(self, _: &'static str, _: &T) -> Result<()> {
116
4
        Err(Error::ExpectedRawValue)
117
4
    }
118

            
119
4
    fn serialize_newtype_variant<T: ?Sized + Serialize>(
120
4
        self,
121
4
        _: &'static str,
122
4
        _: u32,
123
4
        _: &'static str,
124
4
        _: &T,
125
4
    ) -> Result<()> {
126
4
        Err(Error::ExpectedRawValue)
127
4
    }
128

            
129
4
    fn serialize_seq(self, _: Option<usize>) -> Result<Self::SerializeSeq> {
130
4
        Err(Error::ExpectedRawValue)
131
4
    }
132

            
133
4
    fn serialize_tuple(self, _: usize) -> Result<Self::SerializeTuple> {
134
4
        Err(Error::ExpectedRawValue)
135
4
    }
136

            
137
4
    fn serialize_tuple_struct(
138
4
        self,
139
4
        _: &'static str,
140
4
        _: usize,
141
4
    ) -> Result<Self::SerializeTupleStruct> {
142
4
        Err(Error::ExpectedRawValue)
143
4
    }
144

            
145
4
    fn serialize_tuple_variant(
146
4
        self,
147
4
        _: &'static str,
148
4
        _: u32,
149
4
        _: &'static str,
150
4
        _: usize,
151
4
    ) -> Result<Self::SerializeTupleVariant> {
152
4
        Err(Error::ExpectedRawValue)
153
4
    }
154

            
155
4
    fn serialize_map(self, _: Option<usize>) -> Result<Self::SerializeMap> {
156
4
        Err(Error::ExpectedRawValue)
157
4
    }
158

            
159
4
    fn serialize_struct(self, _: &'static str, _: usize) -> Result<Self::SerializeStruct> {
160
4
        Err(Error::ExpectedRawValue)
161
4
    }
162

            
163
4
    fn serialize_struct_variant(
164
4
        self,
165
4
        _: &'static str,
166
4
        _: u32,
167
4
        _: &'static str,
168
4
        _: usize,
169
4
    ) -> Result<Self::SerializeStructVariant> {
170
4
        Err(Error::ExpectedRawValue)
171
4
    }
172
}
173

            
174
#[cfg(test)]
175
mod tests {
176
    macro_rules! test_non_raw_value {
177
        ($test_name:ident => $serialize_method:ident($($serialize_param:expr),*)) => {
178
            #[test]
179
112
            fn $test_name() {
180
                use serde::{Serialize, Serializer};
181

            
182
                struct Inner;
183

            
184
                impl Serialize for Inner {
185
112
                    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
186
112
                        serializer.$serialize_method($($serialize_param),*).map(|_| unreachable!())
187
112
                    }
188
                }
189

            
190
                #[derive(Debug)]
191
                struct Newtype;
192

            
193
                impl Serialize for Newtype {
194
112
                    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
195
112
                        serializer.serialize_newtype_struct(
196
112
                            crate::value::raw::RAW_VALUE_TOKEN, &Inner,
197
112
                        )
198
112
                    }
199
                }
200

            
201
112
                assert_eq!(
202
112
                    crate::to_string(&Newtype).unwrap_err(),
203
112
                    crate::Error::ExpectedRawValue
204
112
                )
205
112
            }
206
        };
207
    }
208

            
209
    test_non_raw_value! { test_bool => serialize_bool(false) }
210
    test_non_raw_value! { test_i8 => serialize_i8(0) }
211
    test_non_raw_value! { test_i16 => serialize_i16(0) }
212
    test_non_raw_value! { test_i32 => serialize_i32(0) }
213
    test_non_raw_value! { test_i64 => serialize_i64(0) }
214
    #[cfg(feature = "integer128")]
215
    test_non_raw_value! { test_i128 => serialize_i128(0) }
216
    test_non_raw_value! { test_u8 => serialize_u8(0) }
217
    test_non_raw_value! { test_u16 => serialize_u16(0) }
218
    test_non_raw_value! { test_u32 => serialize_u32(0) }
219
    test_non_raw_value! { test_u64 => serialize_u64(0) }
220
    #[cfg(feature = "integer128")]
221
    test_non_raw_value! { test_u128 => serialize_u128(0) }
222
    test_non_raw_value! { test_f32 => serialize_f32(0.0) }
223
    test_non_raw_value! { test_f64 => serialize_f64(0.0) }
224
    test_non_raw_value! { test_char => serialize_char('\0') }
225
    test_non_raw_value! { test_bytes => serialize_bytes(b"") }
226
    test_non_raw_value! { test_none => serialize_none() }
227
    test_non_raw_value! { test_some => serialize_some(&()) }
228
    test_non_raw_value! { test_unit => serialize_unit() }
229
    test_non_raw_value! { test_unit_struct => serialize_unit_struct("U") }
230
    test_non_raw_value! { test_unit_variant => serialize_unit_variant("E", 0, "U") }
231
    test_non_raw_value! { test_newtype_struct => serialize_newtype_struct("N", &()) }
232
    test_non_raw_value! { test_newtype_variant => serialize_newtype_variant("E", 0, "N", &()) }
233
    test_non_raw_value! { test_seq => serialize_seq(None) }
234
    test_non_raw_value! { test_tuple => serialize_tuple(0) }
235
    test_non_raw_value! { test_tuple_struct => serialize_tuple_struct("T", 0) }
236
    test_non_raw_value! { test_tuple_variant => serialize_tuple_variant("E", 0, "T", 0) }
237
    test_non_raw_value! { test_map => serialize_map(None) }
238
    test_non_raw_value! { test_struct => serialize_struct("S", 0) }
239
    test_non_raw_value! { test_struct_variant => serialize_struct_variant("E", 0, "S", 0) }
240
}