| 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 |
pub fn new(ser: &'a mut super::Serializer<W>) -> Self { |
| 13 |
Self { ser } |
| 14 |
} |
| 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 |
fn serialize_bool(self, _: bool) -> Result<()> { |
| 29 |
Err(Error::ExpectedRawValue) |
| 30 |
} |
| 31 |
|
| 32 |
fn serialize_i8(self, _: i8) -> Result<()> { |
| 33 |
Err(Error::ExpectedRawValue) |
| 34 |
} |
| 35 |
|
| 36 |
fn serialize_i16(self, _: i16) -> Result<()> { |
| 37 |
Err(Error::ExpectedRawValue) |
| 38 |
} |
| 39 |
|
| 40 |
fn serialize_i32(self, _: i32) -> Result<()> { |
| 41 |
Err(Error::ExpectedRawValue) |
| 42 |
} |
| 43 |
|
| 44 |
fn serialize_i64(self, _: i64) -> Result<()> { |
| 45 |
Err(Error::ExpectedRawValue) |
| 46 |
} |
| 47 |
|
| 48 |
#[cfg(feature = "integer128")] |
| 49 |
fn serialize_i128(self, _: i128) -> Result<()> { |
| 50 |
Err(Error::ExpectedRawValue) |
| 51 |
} |
| 52 |
|
| 53 |
fn serialize_u8(self, _: u8) -> Result<()> { |
| 54 |
Err(Error::ExpectedRawValue) |
| 55 |
} |
| 56 |
|
| 57 |
fn serialize_u16(self, _: u16) -> Result<()> { |
| 58 |
Err(Error::ExpectedRawValue) |
| 59 |
} |
| 60 |
|
| 61 |
fn serialize_u32(self, _: u32) -> Result<()> { |
| 62 |
Err(Error::ExpectedRawValue) |
| 63 |
} |
| 64 |
|
| 65 |
fn serialize_u64(self, _: u64) -> Result<()> { |
| 66 |
Err(Error::ExpectedRawValue) |
| 67 |
} |
| 68 |
|
| 69 |
#[cfg(feature = "integer128")] |
| 70 |
fn serialize_u128(self, _: u128) -> Result<()> { |
| 71 |
Err(Error::ExpectedRawValue) |
| 72 |
} |
| 73 |
|
| 74 |
fn serialize_f32(self, _: f32) -> Result<()> { |
| 75 |
Err(Error::ExpectedRawValue) |
| 76 |
} |
| 77 |
|
| 78 |
fn serialize_f64(self, _: f64) -> Result<()> { |
| 79 |
Err(Error::ExpectedRawValue) |
| 80 |
} |
| 81 |
|
| 82 |
fn serialize_char(self, _: char) -> Result<()> { |
| 83 |
Err(Error::ExpectedRawValue) |
| 84 |
} |
| 85 |
|
| 86 |
fn serialize_str(self, ron: &str) -> Result<()> { |
| 87 |
self.ser.output.write_str(ron)?; |
| 88 |
Ok(()) |
| 89 |
} |
| 90 |
|
| 91 |
fn serialize_bytes(self, _: &[u8]) -> Result<()> { |
| 92 |
Err(Error::ExpectedRawValue) |
| 93 |
} |
| 94 |
|
| 95 |
fn serialize_none(self) -> Result<()> { |
| 96 |
Err(Error::ExpectedRawValue) |
| 97 |
} |
| 98 |
|
| 99 |
fn serialize_some<T: ?Sized + Serialize>(self, _: &T) -> Result<()> { |
| 100 |
Err(Error::ExpectedRawValue) |
| 101 |
} |
| 102 |
|
| 103 |
fn serialize_unit(self) -> Result<()> { |
| 104 |
Err(Error::ExpectedRawValue) |
| 105 |
} |
| 106 |
|
| 107 |
fn serialize_unit_struct(self, _: &'static str) -> Result<()> { |
| 108 |
Err(Error::ExpectedRawValue) |
| 109 |
} |
| 110 |
|
| 111 |
fn serialize_unit_variant(self, _: &'static str, _: u32, _: &'static str) -> Result<()> { |
| 112 |
Err(Error::ExpectedRawValue) |
| 113 |
} |
| 114 |
|
| 115 |
fn serialize_newtype_struct<T: ?Sized + Serialize>(self, _: &'static str, _: &T) -> Result<()> { |
| 116 |
Err(Error::ExpectedRawValue) |
| 117 |
} |
| 118 |
|
| 119 |
fn serialize_newtype_variant<T: ?Sized + Serialize>( |
| 120 |
self, |
| 121 |
_: &'static str, |
| 122 |
_: u32, |
| 123 |
_: &'static str, |
| 124 |
_: &T, |
| 125 |
) -> Result<()> { |
| 126 |
Err(Error::ExpectedRawValue) |
| 127 |
} |
| 128 |
|
| 129 |
fn serialize_seq(self, _: Option<usize>) -> Result<Self::SerializeSeq> { |
| 130 |
Err(Error::ExpectedRawValue) |
| 131 |
} |
| 132 |
|
| 133 |
fn serialize_tuple(self, _: usize) -> Result<Self::SerializeTuple> { |
| 134 |
Err(Error::ExpectedRawValue) |
| 135 |
} |
| 136 |
|
| 137 |
fn serialize_tuple_struct( |
| 138 |
self, |
| 139 |
_: &'static str, |
| 140 |
_: usize, |
| 141 |
) -> Result<Self::SerializeTupleStruct> { |
| 142 |
Err(Error::ExpectedRawValue) |
| 143 |
} |
| 144 |
|
| 145 |
fn serialize_tuple_variant( |
| 146 |
self, |
| 147 |
_: &'static str, |
| 148 |
_: u32, |
| 149 |
_: &'static str, |
| 150 |
_: usize, |
| 151 |
) -> Result<Self::SerializeTupleVariant> { |
| 152 |
Err(Error::ExpectedRawValue) |
| 153 |
} |
| 154 |
|
| 155 |
fn serialize_map(self, _: Option<usize>) -> Result<Self::SerializeMap> { |
| 156 |
Err(Error::ExpectedRawValue) |
| 157 |
} |
| 158 |
|
| 159 |
fn serialize_struct(self, _: &'static str, _: usize) -> Result<Self::SerializeStruct> { |
| 160 |
Err(Error::ExpectedRawValue) |
| 161 |
} |
| 162 |
|
| 163 |
fn serialize_struct_variant( |
| 164 |
self, |
| 165 |
_: &'static str, |
| 166 |
_: u32, |
| 167 |
_: &'static str, |
| 168 |
_: usize, |
| 169 |
) -> Result<Self::SerializeStructVariant> { |
| 170 |
Err(Error::ExpectedRawValue) |
| 171 |
} |
| 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 |
fn $test_name() { |
| 180 |
use serde::{Serialize, Serializer}; |
| 181 |
|
| 182 |
struct Inner; |
| 183 |
|
| 184 |
impl Serialize for Inner { |
| 185 |
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> { |
| 186 |
serializer.$serialize_method($($serialize_param),*).map(|_| unreachable!()) |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
#[derive(Debug)] |
| 191 |
struct Newtype; |
| 192 |
|
| 193 |
impl Serialize for Newtype { |
| 194 |
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> { |
| 195 |
serializer.serialize_newtype_struct( |
| 196 |
crate::value::raw::RAW_VALUE_TOKEN, &Inner, |
| 197 |
) |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
assert_eq!( |
| 202 |
crate::to_string(&Newtype).unwrap_err(), |
| 203 |
crate::Error::ExpectedRawValue |
| 204 |
) |
| 205 |
} |
| 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 |
} |