| 1 | use std::{cmp::PartialEq, fmt::Debug}; |
| 2 | |
| 3 | use ron::{de::from_str, ser::to_string}; |
| 4 | use serde::{Deserialize, Serialize}; |
| 5 | |
| 6 | #[derive(Debug, PartialEq, Serialize, Deserialize)] |
| 7 | struct Unit; |
| 8 | |
| 9 | #[derive(Debug, PartialEq, Serialize, Deserialize)] |
| 10 | struct Tuple(u8, u8); |
| 11 | |
| 12 | #[derive(Debug, PartialEq, Serialize, Deserialize)] |
| 13 | enum Inner { |
| 14 | Foo, |
| 15 | Bar, |
| 16 | } |
| 17 | |
| 18 | #[derive(Debug, PartialEq, Serialize, Deserialize)] |
| 19 | enum EnumStructExternally { |
| 20 | VariantA { |
| 21 | foo: u32, |
| 22 | bar: Unit, |
| 23 | #[serde(with = "ByteStr")] |
| 24 | baz: Vec<u8>, |
| 25 | different: Tuple, |
| 26 | }, |
| 27 | VariantB { |
| 28 | foo: u32, |
| 29 | bar: Unit, |
| 30 | #[serde(with = "ByteStr")] |
| 31 | baz: Vec<u8>, |
| 32 | }, |
| 33 | } |
| 34 | |
| 35 | #[derive(Debug, PartialEq, Serialize, Deserialize)] |
| 36 | #[serde(tag = "type")] |
| 37 | enum EnumStructInternally { |
| 38 | VariantA { foo: u32, bar: u32, different: u32 }, |
| 39 | VariantB { foo: u32, bar: u32 }, |
| 40 | } |
| 41 | |
| 42 | #[derive(Debug, PartialEq, Serialize, Deserialize)] |
| 43 | #[serde(tag = "type", content = "content")] |
| 44 | enum EnumStructAdjacently { |
| 45 | VariantA { foo: f64, bar: (), different: Inner }, |
| 46 | VariantB { foo: f64, bar: () }, |
| 47 | } |
| 48 | |
| 49 | #[derive(Debug, PartialEq, Serialize, Deserialize)] |
| 50 | #[serde(untagged)] |
| 51 | enum EnumStructUntagged { |
| 52 | VariantA { foo: u32, bar: u32, different: u32 }, |
| 53 | VariantB { foo: u32, bar: u32 }, |
| 54 | } |
| 55 | |
| 56 | fn test_ser<T: Serialize>(value: &T, expected: &str) { |
| 57 | let actual = to_string(value).expect("Failed to serialize"); |
| 58 | assert_eq!(actual, expected); |
| 59 | } |
| 60 | |
| 61 | fn test_de<T>(s: &str, expected: T) |
| 62 | where |
| 63 | T: for<'a> Deserialize<'a> + Debug + PartialEq, |
| 64 | { |
| 65 | let actual: Result<T, _> = from_str(s); |
| 66 | assert_eq!(actual, Ok(expected)); |
| 67 | } |
| 68 | |
| 69 | fn test_roundtrip<T>(value: T) |
| 70 | where |
| 71 | T: Serialize + for<'a> Deserialize<'a> + Debug + PartialEq, |
| 72 | { |
| 73 | let s = to_string(&value).expect("Failed to serialize"); |
| 74 | let actual: Result<T, _> = from_str(&s); |
| 75 | assert_eq!(actual, Ok(value)); |
| 76 | } |
| 77 | |
| 78 | #[test] |
| 79 | fn test_externally_a_ser() { |
| 80 | let v = EnumStructExternally::VariantA { |
| 81 | foo: 1, |
| 82 | bar: Unit, |
| 83 | baz: vec![b'a'], |
| 84 | different: Tuple(2, 3), |
| 85 | }; |
| 86 | let e = "VariantA(foo:1,bar:(),baz:b\"a\",different:(2,3))"; |
| 87 | test_ser(&v, e); |
| 88 | } |
| 89 | |
| 90 | #[test] |
| 91 | fn test_externally_b_ser() { |
| 92 | let v = EnumStructExternally::VariantB { |
| 93 | foo: 1, |
| 94 | bar: Unit, |
| 95 | baz: vec![b'a'], |
| 96 | }; |
| 97 | let e = "VariantB(foo:1,bar:(),baz:b\"a\")"; |
| 98 | test_ser(&v, e); |
| 99 | } |
| 100 | |
| 101 | #[test] |
| 102 | fn test_internally_a_ser() { |
| 103 | let v = EnumStructInternally::VariantA { |
| 104 | foo: 1, |
| 105 | bar: 2, |
| 106 | different: 3, |
| 107 | }; |
| 108 | let e = "(type:\"VariantA\",foo:1,bar:2,different:3)"; |
| 109 | test_ser(&v, e); |
| 110 | } |
| 111 | |
| 112 | #[test] |
| 113 | fn test_internally_b_ser() { |
| 114 | let v = EnumStructInternally::VariantB { foo: 1, bar: 2 }; |
| 115 | let e = "(type:\"VariantB\",foo:1,bar:2)"; |
| 116 | test_ser(&v, e); |
| 117 | } |
| 118 | |
| 119 | #[test] |
| 120 | fn test_adjacently_a_ser() { |
| 121 | let v = EnumStructAdjacently::VariantA { |
| 122 | foo: 1.0, |
| 123 | bar: (), |
| 124 | different: Inner::Foo, |
| 125 | }; |
| 126 | let e = "(type:VariantA,content:(foo:1.0,bar:(),different:Foo))"; |
| 127 | test_ser(&v, e); |
| 128 | } |
| 129 | |
| 130 | #[test] |
| 131 | fn test_adjacently_b_ser() { |
| 132 | let v = EnumStructAdjacently::VariantB { foo: 1.0, bar: () }; |
| 133 | let e = "(type:VariantB,content:(foo:1.0,bar:()))"; |
| 134 | test_ser(&v, e); |
| 135 | } |
| 136 | |
| 137 | #[test] |
| 138 | fn test_untagged_a_ser() { |
| 139 | let v = EnumStructUntagged::VariantA { |
| 140 | foo: 1, |
| 141 | bar: 2, |
| 142 | different: 3, |
| 143 | }; |
| 144 | let e = "(foo:1,bar:2,different:3)"; |
| 145 | test_ser(&v, e); |
| 146 | } |
| 147 | |
| 148 | #[test] |
| 149 | fn test_untagged_b_ser() { |
| 150 | let v = EnumStructUntagged::VariantB { foo: 1, bar: 2 }; |
| 151 | let e = "(foo:1,bar:2)"; |
| 152 | test_ser(&v, e); |
| 153 | } |
| 154 | |
| 155 | #[test] |
| 156 | fn test_externally_a_de() { |
| 157 | let s = "VariantA(foo:1,bar:Unit,baz:b\"a\",different:Tuple(2,3))"; |
| 158 | let e = EnumStructExternally::VariantA { |
| 159 | foo: 1, |
| 160 | bar: Unit, |
| 161 | baz: vec![b'a'], |
| 162 | different: Tuple(2, 3), |
| 163 | }; |
| 164 | test_de(s, e); |
| 165 | } |
| 166 | |
| 167 | #[test] |
| 168 | fn test_externally_b_de() { |
| 169 | let s = "VariantB(foo:1,bar:Unit,baz:b\"a\")"; |
| 170 | let e = EnumStructExternally::VariantB { |
| 171 | foo: 1, |
| 172 | bar: Unit, |
| 173 | baz: vec![b'a'], |
| 174 | }; |
| 175 | test_de(s, e); |
| 176 | } |
| 177 | |
| 178 | #[test] |
| 179 | fn test_internally_a_de() { |
| 180 | let s = "(type:\"VariantA\",foo:1,bar:2,different:3)"; |
| 181 | let e = EnumStructInternally::VariantA { |
| 182 | foo: 1, |
| 183 | bar: 2, |
| 184 | different: 3, |
| 185 | }; |
| 186 | test_de(s, e); |
| 187 | } |
| 188 | |
| 189 | #[test] |
| 190 | fn test_internally_b_de() { |
| 191 | let s = "(type:\"VariantB\",foo:1,bar:2)"; |
| 192 | let e = EnumStructInternally::VariantB { foo: 1, bar: 2 }; |
| 193 | test_de(s, e); |
| 194 | } |
| 195 | |
| 196 | #[test] |
| 197 | fn test_adjacently_a_de() { |
| 198 | let s = "(type:VariantA,content:(foo:1.0,bar:(),different:Foo))"; |
| 199 | let e = EnumStructAdjacently::VariantA { |
| 200 | foo: 1.0, |
| 201 | bar: (), |
| 202 | different: Inner::Foo, |
| 203 | }; |
| 204 | test_de(s, e); |
| 205 | } |
| 206 | |
| 207 | #[test] |
| 208 | fn test_adjacently_b_de() { |
| 209 | let s = "(type:VariantB,content:(foo:1.0,bar:()))"; |
| 210 | let e = EnumStructAdjacently::VariantB { foo: 1.0, bar: () }; |
| 211 | test_de(s, e); |
| 212 | } |
| 213 | |
| 214 | #[test] |
| 215 | fn test_untagged_a_de() { |
| 216 | let s = "(foo:1,bar:2,different:3)"; |
| 217 | let e = EnumStructUntagged::VariantA { |
| 218 | foo: 1, |
| 219 | bar: 2, |
| 220 | different: 3, |
| 221 | }; |
| 222 | test_de(s, e); |
| 223 | } |
| 224 | |
| 225 | #[test] |
| 226 | fn test_untagged_b_de() { |
| 227 | let s = "(foo:1,bar:2)"; |
| 228 | let e = EnumStructUntagged::VariantB { foo: 1, bar: 2 }; |
| 229 | test_de(s, e); |
| 230 | } |
| 231 | |
| 232 | #[test] |
| 233 | fn test_externally_a_roundtrip() { |
| 234 | let v = EnumStructExternally::VariantA { |
| 235 | foo: 1, |
| 236 | bar: Unit, |
| 237 | baz: vec![b'a'], |
| 238 | different: Tuple(2, 3), |
| 239 | }; |
| 240 | test_roundtrip(v); |
| 241 | } |
| 242 | |
| 243 | #[test] |
| 244 | fn test_externally_b_roundtrip() { |
| 245 | let v = EnumStructExternally::VariantB { |
| 246 | foo: 1, |
| 247 | bar: Unit, |
| 248 | baz: vec![b'a'], |
| 249 | }; |
| 250 | test_roundtrip(v); |
| 251 | } |
| 252 | |
| 253 | #[test] |
| 254 | fn test_internally_a_roundtrip() { |
| 255 | let v = EnumStructInternally::VariantA { |
| 256 | foo: 1, |
| 257 | bar: 2, |
| 258 | different: 3, |
| 259 | }; |
| 260 | test_roundtrip(v); |
| 261 | } |
| 262 | |
| 263 | #[test] |
| 264 | fn test_internally_b_roundtrip() { |
| 265 | let v = EnumStructInternally::VariantB { foo: 1, bar: 2 }; |
| 266 | test_roundtrip(v); |
| 267 | } |
| 268 | |
| 269 | #[test] |
| 270 | fn test_adjacently_a_roundtrip() { |
| 271 | let v = EnumStructAdjacently::VariantA { |
| 272 | foo: 1.0, |
| 273 | bar: (), |
| 274 | different: Inner::Foo, |
| 275 | }; |
| 276 | test_roundtrip(v); |
| 277 | } |
| 278 | |
| 279 | #[test] |
| 280 | fn test_adjacently_b_roundtrip() { |
| 281 | let v = EnumStructAdjacently::VariantB { foo: 1.0, bar: () }; |
| 282 | test_roundtrip(v); |
| 283 | } |
| 284 | |
| 285 | #[test] |
| 286 | fn test_untagged_a_roundtrip() { |
| 287 | let v = EnumStructUntagged::VariantA { |
| 288 | foo: 1, |
| 289 | bar: 2, |
| 290 | different: 3, |
| 291 | }; |
| 292 | test_roundtrip(v); |
| 293 | } |
| 294 | |
| 295 | #[test] |
| 296 | fn test_untagged_b_roundtrip() { |
| 297 | let v = EnumStructUntagged::VariantB { foo: 1, bar: 2 }; |
| 298 | test_roundtrip(v); |
| 299 | } |
| 300 | |
| 301 | enum ByteStr {} |
| 302 | |
| 303 | impl ByteStr { |
| 304 | fn serialize<S: serde::Serializer>(data: &[u8], serializer: S) -> Result<S::Ok, S::Error> { |
| 305 | serializer.serialize_bytes(data) |
| 306 | } |
| 307 | |
| 308 | fn deserialize<'de, D: serde::Deserializer<'de>>(deserializer: D) -> Result<Vec<u8>, D::Error> { |
| 309 | struct ByteStrVisitor; |
| 310 | |
| 311 | impl<'de> serde::de::Visitor<'de> for ByteStrVisitor { |
| 312 | type Value = Vec<u8>; |
| 313 | |
| 314 | // GRCOV_EXCL_START |
| 315 | fn expecting(&self, fmt: &mut core::fmt::Formatter) -> core::fmt::Result { |
| 316 | fmt.write_str("a Rusty byte string") |
| 317 | } |
| 318 | // GRCOV_EXCL_STOP |
| 319 | |
| 320 | fn visit_bytes<E: serde::de::Error>(self, bytes: &[u8]) -> Result<Self::Value, E> { |
| 321 | Ok(bytes.to_vec()) |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | deserializer.deserialize_bytes(ByteStrVisitor) |
| 326 | } |
| 327 | } |