| 1 | use std::collections::HashMap; |
| 2 | |
| 3 | use ron::{ |
| 4 | de::from_str, |
| 5 | error::Error, |
| 6 | extensions::Extensions, |
| 7 | ser::{to_string_pretty, PrettyConfig}, |
| 8 | }; |
| 9 | use serde::{Deserialize, Serialize}; |
| 10 | |
| 11 | #[derive(Debug, Deserialize, Serialize, Eq, PartialEq)] |
| 12 | enum TestEnum { |
| 13 | Unit, |
| 14 | PrimitiveNewtype(String), |
| 15 | Tuple(u32, bool), |
| 16 | Struct { a: u32, b: bool }, |
| 17 | TupleNewtypeUnit(Unit), |
| 18 | TupleNewtypeNewtype(Newtype), |
| 19 | TupleNewtypeTuple((u32, bool)), |
| 20 | TupleNewtypeTupleStruct(TupleStruct), |
| 21 | TupleNewtypeStruct(Struct), |
| 22 | TupleNewtypeEnum(Enum), |
| 23 | TupleNewtypeOption(Option<Struct>), |
| 24 | TupleNewtypeSeq(Vec<Struct>), |
| 25 | TupleNewtypeMap(HashMap<u32, Struct>), |
| 26 | } |
| 27 | |
| 28 | #[derive(Debug, Deserialize, Serialize, Eq, PartialEq)] |
| 29 | struct Unit; |
| 30 | |
| 31 | #[derive(Debug, Deserialize, Serialize, Eq, PartialEq)] |
| 32 | struct Newtype(i32); |
| 33 | |
| 34 | #[derive(Debug, Deserialize, Serialize, Eq, PartialEq)] |
| 35 | struct TupleStruct(u32, bool); |
| 36 | |
| 37 | #[derive(Debug, Deserialize, Serialize, Eq, PartialEq)] |
| 38 | struct Struct { |
| 39 | a: u32, |
| 40 | b: bool, |
| 41 | } |
| 42 | |
| 43 | #[derive(Debug, Deserialize, Serialize, Eq, PartialEq)] |
| 44 | enum Enum { |
| 45 | A, |
| 46 | B(Struct), |
| 47 | C(u32, bool), |
| 48 | D { a: u32, b: bool }, |
| 49 | } |
| 50 | |
| 51 | #[test] |
| 52 | fn test_deserialise_non_newtypes() { |
| 53 | assert_eq!( |
| 54 | from_str::<TestEnum>(r#"#![enable(unwrap_variant_newtypes)] Unit"#).unwrap(), |
| 55 | TestEnum::Unit, |
| 56 | ); |
| 57 | |
| 58 | assert_eq!( |
| 59 | from_str::<TestEnum>(r#"#![enable(unwrap_variant_newtypes)] PrimitiveNewtype("hi")"#) |
| 60 | .unwrap(), |
| 61 | TestEnum::PrimitiveNewtype(String::from("hi")), |
| 62 | ); |
| 63 | |
| 64 | assert_eq!( |
| 65 | from_str::<TestEnum>(r#"#![enable(unwrap_variant_newtypes)] Tuple(4, false)"#).unwrap(), |
| 66 | TestEnum::Tuple(4, false), |
| 67 | ); |
| 68 | |
| 69 | assert_eq!( |
| 70 | from_str::<TestEnum>(r#"#![enable(unwrap_variant_newtypes)] Struct(a: 4, b: false)"#) |
| 71 | .unwrap(), |
| 72 | TestEnum::Struct { a: 4, b: false }, |
| 73 | ); |
| 74 | } |
| 75 | |
| 76 | #[test] |
| 77 | fn test_deserialise_tuple_newtypes() { |
| 78 | assert_eq!( |
| 79 | from_str::<TestEnum>(r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeUnit(Unit)"#) |
| 80 | .unwrap_err() |
| 81 | .code, |
| 82 | Error::ExpectedStructLikeEnd, |
| 83 | ); |
| 84 | assert_eq!( |
| 85 | from_str::<TestEnum>(r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeUnit(())"#) |
| 86 | .unwrap_err() |
| 87 | .code, |
| 88 | Error::ExpectedStructLikeEnd, |
| 89 | ); |
| 90 | assert_eq!( |
| 91 | from_str::<TestEnum>(r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeUnit()"#).unwrap(), |
| 92 | TestEnum::TupleNewtypeUnit(Unit), |
| 93 | ); |
| 94 | |
| 95 | assert_eq!( |
| 96 | from_str::<TestEnum>( |
| 97 | r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeNewtype(Newtype(4))"# |
| 98 | ) |
| 99 | .unwrap_err() |
| 100 | .code, |
| 101 | Error::ExpectedInteger, |
| 102 | ); |
| 103 | assert_eq!( |
| 104 | from_str::<TestEnum>(r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeNewtype((4))"#) |
| 105 | .unwrap_err() |
| 106 | .code, |
| 107 | Error::ExpectedInteger, |
| 108 | ); |
| 109 | assert_eq!( |
| 110 | from_str::<TestEnum>(r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeNewtype(4)"#) |
| 111 | .unwrap(), |
| 112 | TestEnum::TupleNewtypeNewtype(Newtype(4)), |
| 113 | ); |
| 114 | assert_eq!( |
| 115 | from_str::<TestEnum>(r#"#![enable(unwrap_newtypes)] TupleNewtypeNewtype(4)"#).unwrap(), |
| 116 | TestEnum::TupleNewtypeNewtype(Newtype(4)), |
| 117 | ); |
| 118 | assert_eq!( |
| 119 | from_str::<TestEnum>(r#"#![enable(unwrap_newtypes)] #![enable(unwrap_variant_newtypes)] TupleNewtypeNewtype(4)"#).unwrap(), |
| 120 | TestEnum::TupleNewtypeNewtype(Newtype(4)), |
| 121 | ); |
| 122 | |
| 123 | assert_eq!( |
| 124 | from_str::<TestEnum>( |
| 125 | r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeTuple((4, false))"# |
| 126 | ) |
| 127 | .unwrap_err() |
| 128 | .code, |
| 129 | Error::ExpectedInteger, |
| 130 | ); |
| 131 | assert_eq!( |
| 132 | from_str::<TestEnum>(r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeTuple(4, false)"#) |
| 133 | .unwrap(), |
| 134 | TestEnum::TupleNewtypeTuple((4, false)), |
| 135 | ); |
| 136 | |
| 137 | assert_eq!( |
| 138 | from_str::<TestEnum>( |
| 139 | r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeTupleStruct(TupleStruct(4, false))"# |
| 140 | ) |
| 141 | .unwrap_err() |
| 142 | .code, |
| 143 | Error::ExpectedInteger, |
| 144 | ); |
| 145 | assert_eq!( |
| 146 | from_str::<TestEnum>( |
| 147 | r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeTupleStruct((4, false))"# |
| 148 | ) |
| 149 | .unwrap_err() |
| 150 | .code, |
| 151 | Error::ExpectedInteger, |
| 152 | ); |
| 153 | assert_eq!( |
| 154 | from_str::<TestEnum>( |
| 155 | r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeTupleStruct(4, false)"# |
| 156 | ) |
| 157 | .unwrap(), |
| 158 | TestEnum::TupleNewtypeTupleStruct(TupleStruct(4, false)), |
| 159 | ); |
| 160 | |
| 161 | assert_eq!( |
| 162 | from_str::<TestEnum>( |
| 163 | r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeStruct(Struct(a: 4, b: false))"# |
| 164 | ) |
| 165 | .unwrap_err() |
| 166 | .code, |
| 167 | Error::ExpectedMapColon, |
| 168 | ); |
| 169 | assert_eq!( |
| 170 | from_str::<TestEnum>( |
| 171 | r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeStruct((a: 4, b: false))"# |
| 172 | ) |
| 173 | .unwrap_err() |
| 174 | .code, |
| 175 | Error::ExpectedIdentifier, |
| 176 | ); |
| 177 | assert_eq!( |
| 178 | from_str::<TestEnum>( |
| 179 | r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeStruct(a: 4, b: false)"# |
| 180 | ) |
| 181 | .unwrap(), |
| 182 | TestEnum::TupleNewtypeStruct(Struct { a: 4, b: false }), |
| 183 | ); |
| 184 | |
| 185 | assert_eq!( |
| 186 | from_str::<TestEnum>(r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeEnum(A)"#).unwrap(), |
| 187 | TestEnum::TupleNewtypeEnum(Enum::A), |
| 188 | ); |
| 189 | assert_eq!( |
| 190 | from_str::<TestEnum>( |
| 191 | r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeEnum(B(a: 4, b: false))"# |
| 192 | ) |
| 193 | .unwrap(), |
| 194 | TestEnum::TupleNewtypeEnum(Enum::B(Struct { a: 4, b: false })), |
| 195 | ); |
| 196 | assert_eq!( |
| 197 | from_str::<TestEnum>(r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeEnum(C 4, false)"#) |
| 198 | .unwrap_err() |
| 199 | .code, |
| 200 | Error::ExpectedStructLike, |
| 201 | ); |
| 202 | assert_eq!( |
| 203 | from_str::<TestEnum>( |
| 204 | r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeEnum(C(4, false))"# |
| 205 | ) |
| 206 | .unwrap(), |
| 207 | TestEnum::TupleNewtypeEnum(Enum::C(4, false)), |
| 208 | ); |
| 209 | assert_eq!( |
| 210 | from_str::<TestEnum>( |
| 211 | r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeEnum(D a: 4, b: false)"# |
| 212 | ) |
| 213 | .unwrap_err() |
| 214 | .code, |
| 215 | Error::ExpectedStructLike, |
| 216 | ); |
| 217 | assert_eq!( |
| 218 | from_str::<TestEnum>( |
| 219 | r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeEnum(D(a: 4, b: false))"# |
| 220 | ) |
| 221 | .unwrap(), |
| 222 | TestEnum::TupleNewtypeEnum(Enum::D { a: 4, b: false }), |
| 223 | ); |
| 224 | |
| 225 | assert_eq!( |
| 226 | from_str::<TestEnum>(r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeOption(None)"#) |
| 227 | .unwrap(), |
| 228 | TestEnum::TupleNewtypeOption(None), |
| 229 | ); |
| 230 | assert_eq!( |
| 231 | from_str::<TestEnum>( |
| 232 | r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeOption(Some(a: 4, b: false))"# |
| 233 | ) |
| 234 | .unwrap(), |
| 235 | TestEnum::TupleNewtypeOption(Some(Struct { a: 4, b: false })), |
| 236 | ); |
| 237 | assert_eq!( |
| 238 | from_str::<TestEnum>( |
| 239 | r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeOption(a: 4, b: false)"# |
| 240 | ) |
| 241 | .unwrap_err() |
| 242 | .code, |
| 243 | Error::ExpectedOption, |
| 244 | ); |
| 245 | assert_eq!( |
| 246 | from_str::<TestEnum>(r#"#![enable(unwrap_variant_newtypes, implicit_some)] TupleNewtypeOption(a: 4, b: false)"#).unwrap(), |
| 247 | TestEnum::TupleNewtypeOption(Some(Struct { a: 4, b: false })), |
| 248 | ); |
| 249 | |
| 250 | assert_eq!( |
| 251 | from_str::<TestEnum>(r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeSeq([])"#).unwrap(), |
| 252 | TestEnum::TupleNewtypeSeq(vec![]), |
| 253 | ); |
| 254 | assert_eq!( |
| 255 | from_str::<TestEnum>( |
| 256 | r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeSeq([(a: 4, b: false)])"# |
| 257 | ) |
| 258 | .unwrap(), |
| 259 | TestEnum::TupleNewtypeSeq(vec![Struct { a: 4, b: false }]), |
| 260 | ); |
| 261 | assert_eq!( |
| 262 | from_str::<TestEnum>( |
| 263 | r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeSeq([Struct(a: 4, b: false)])"# |
| 264 | ) |
| 265 | .unwrap(), |
| 266 | TestEnum::TupleNewtypeSeq(vec![Struct { a: 4, b: false }]), |
| 267 | ); |
| 268 | |
| 269 | assert_eq!( |
| 270 | from_str::<TestEnum>(r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeMap({})"#).unwrap(), |
| 271 | TestEnum::TupleNewtypeMap(vec![].into_iter().collect()), |
| 272 | ); |
| 273 | assert_eq!( |
| 274 | from_str::<TestEnum>( |
| 275 | r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeMap({2: (a: 4, b: false)})"# |
| 276 | ) |
| 277 | .unwrap(), |
| 278 | TestEnum::TupleNewtypeMap(vec![(2, Struct { a: 4, b: false })].into_iter().collect()), |
| 279 | ); |
| 280 | assert_eq!( |
| 281 | from_str::<TestEnum>( |
| 282 | r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeMap({8: Struct(a: 4, b: false)})"# |
| 283 | ) |
| 284 | .unwrap(), |
| 285 | TestEnum::TupleNewtypeMap(vec![(8, Struct { a: 4, b: false })].into_iter().collect()), |
| 286 | ); |
| 287 | } |
| 288 | |
| 289 | #[test] |
| 290 | fn test_newtype_some() { |
| 291 | assert_eq!( |
| 292 | from_str::<Option<Struct>>(r#"Some((a: 4, b: false))"#).unwrap(), |
| 293 | Some(Struct { a: 4, b: false }), |
| 294 | ); |
| 295 | |
| 296 | assert_eq!( |
| 297 | from_str::<Option<Struct>>(r#"Some(a: 4, b: false)"#) |
| 298 | .unwrap_err() |
| 299 | .code, |
| 300 | Error::ExpectedDifferentStructName { |
| 301 | expected: "Struct", |
| 302 | found: String::from("a"), |
| 303 | }, |
| 304 | ); |
| 305 | |
| 306 | assert_eq!( |
| 307 | from_str::<Option<Struct>>(r#"#![enable(unwrap_variant_newtypes)] Some(a: 4, b: false)"#) |
| 308 | .unwrap(), |
| 309 | Some(Struct { a: 4, b: false }), |
| 310 | ); |
| 311 | } |
| 312 | |
| 313 | #[test] |
| 314 | fn test_serialise_non_newtypes() { |
| 315 | assert_eq_serialize_roundtrip(TestEnum::Unit, Extensions::UNWRAP_VARIANT_NEWTYPES); |
| 316 | |
| 317 | assert_eq_serialize_roundtrip( |
| 318 | TestEnum::PrimitiveNewtype(String::from("hi")), |
| 319 | Extensions::UNWRAP_VARIANT_NEWTYPES, |
| 320 | ); |
| 321 | |
| 322 | assert_eq_serialize_roundtrip( |
| 323 | TestEnum::Tuple(4, false), |
| 324 | Extensions::UNWRAP_VARIANT_NEWTYPES, |
| 325 | ); |
| 326 | |
| 327 | assert_eq_serialize_roundtrip( |
| 328 | TestEnum::Struct { a: 4, b: false }, |
| 329 | Extensions::UNWRAP_VARIANT_NEWTYPES, |
| 330 | ); |
| 331 | } |
| 332 | |
| 333 | #[test] |
| 334 | fn test_serialise_tuple_newtypes() { |
| 335 | assert_eq_serialize_roundtrip( |
| 336 | TestEnum::TupleNewtypeUnit(Unit), |
| 337 | Extensions::UNWRAP_VARIANT_NEWTYPES, |
| 338 | ); |
| 339 | |
| 340 | assert_eq_serialize_roundtrip( |
| 341 | TestEnum::TupleNewtypeNewtype(Newtype(4)), |
| 342 | Extensions::UNWRAP_VARIANT_NEWTYPES, |
| 343 | ); |
| 344 | assert_eq_serialize_roundtrip( |
| 345 | TestEnum::TupleNewtypeNewtype(Newtype(4)), |
| 346 | Extensions::UNWRAP_NEWTYPES, |
| 347 | ); |
| 348 | assert_eq_serialize_roundtrip( |
| 349 | TestEnum::TupleNewtypeNewtype(Newtype(4)), |
| 350 | Extensions::UNWRAP_VARIANT_NEWTYPES | Extensions::UNWRAP_NEWTYPES, |
| 351 | ); |
| 352 | |
| 353 | assert_eq_serialize_roundtrip( |
| 354 | TestEnum::TupleNewtypeTuple((4, false)), |
| 355 | Extensions::UNWRAP_VARIANT_NEWTYPES, |
| 356 | ); |
| 357 | |
| 358 | assert_eq_serialize_roundtrip( |
| 359 | TestEnum::TupleNewtypeTupleStruct(TupleStruct(4, false)), |
| 360 | Extensions::UNWRAP_VARIANT_NEWTYPES, |
| 361 | ); |
| 362 | |
| 363 | assert_eq_serialize_roundtrip( |
| 364 | TestEnum::TupleNewtypeStruct(Struct { a: 4, b: false }), |
| 365 | Extensions::UNWRAP_VARIANT_NEWTYPES, |
| 366 | ); |
| 367 | |
| 368 | assert_eq_serialize_roundtrip( |
| 369 | TestEnum::TupleNewtypeEnum(Enum::A), |
| 370 | Extensions::UNWRAP_VARIANT_NEWTYPES, |
| 371 | ); |
| 372 | assert_eq_serialize_roundtrip( |
| 373 | TestEnum::TupleNewtypeEnum(Enum::B(Struct { a: 4, b: false })), |
| 374 | Extensions::UNWRAP_VARIANT_NEWTYPES, |
| 375 | ); |
| 376 | assert_eq_serialize_roundtrip( |
| 377 | TestEnum::TupleNewtypeEnum(Enum::C(4, false)), |
| 378 | Extensions::UNWRAP_VARIANT_NEWTYPES, |
| 379 | ); |
| 380 | assert_eq_serialize_roundtrip( |
| 381 | TestEnum::TupleNewtypeEnum(Enum::D { a: 4, b: false }), |
| 382 | Extensions::UNWRAP_VARIANT_NEWTYPES, |
| 383 | ); |
| 384 | |
| 385 | assert_eq_serialize_roundtrip( |
| 386 | TestEnum::TupleNewtypeOption(None), |
| 387 | Extensions::UNWRAP_VARIANT_NEWTYPES, |
| 388 | ); |
| 389 | assert_eq_serialize_roundtrip( |
| 390 | TestEnum::TupleNewtypeOption(Some(Struct { a: 4, b: false })), |
| 391 | Extensions::UNWRAP_VARIANT_NEWTYPES, |
| 392 | ); |
| 393 | assert_eq_serialize_roundtrip( |
| 394 | TestEnum::TupleNewtypeOption(Some(Struct { a: 4, b: false })), |
| 395 | Extensions::IMPLICIT_SOME, |
| 396 | ); |
| 397 | assert_eq_serialize_roundtrip( |
| 398 | TestEnum::TupleNewtypeOption(Some(Struct { a: 4, b: false })), |
| 399 | Extensions::UNWRAP_VARIANT_NEWTYPES | Extensions::IMPLICIT_SOME, |
| 400 | ); |
| 401 | |
| 402 | assert_eq_serialize_roundtrip( |
| 403 | TestEnum::TupleNewtypeSeq(vec![]), |
| 404 | Extensions::UNWRAP_VARIANT_NEWTYPES, |
| 405 | ); |
| 406 | assert_eq_serialize_roundtrip( |
| 407 | TestEnum::TupleNewtypeSeq(vec![Struct { a: 4, b: false }]), |
| 408 | Extensions::UNWRAP_VARIANT_NEWTYPES, |
| 409 | ); |
| 410 | |
| 411 | assert_eq_serialize_roundtrip( |
| 412 | TestEnum::TupleNewtypeMap(vec![].into_iter().collect()), |
| 413 | Extensions::UNWRAP_VARIANT_NEWTYPES, |
| 414 | ); |
| 415 | assert_eq_serialize_roundtrip( |
| 416 | TestEnum::TupleNewtypeMap(vec![(2, Struct { a: 4, b: false })].into_iter().collect()), |
| 417 | Extensions::UNWRAP_VARIANT_NEWTYPES, |
| 418 | ); |
| 419 | } |
| 420 | |
| 421 | fn assert_eq_serialize_roundtrip< |
| 422 | S: Serialize + serde::de::DeserializeOwned + PartialEq + core::fmt::Debug, |
| 423 | >( |
| 424 | value: S, |
| 425 | extensions: Extensions, |
| 426 | ) { |
| 427 | let ron = to_string_pretty(&value, PrettyConfig::default().extensions(extensions)).unwrap(); |
| 428 | let result = from_str::<S>(&ron); |
| 429 | |
| 430 | assert_eq!(result.as_ref(), Ok(&value), "{}", ron,); |
| 431 | } |