| 1 |
use std::collections::HashMap; |
| 2 |
|
| 3 |
use ron::extensions::Extensions; |
| 4 |
use serde::{Deserialize, Serialize}; |
| 5 |
|
| 6 |
#[derive(Debug, PartialEq, Deserialize, Serialize)] |
| 7 |
struct UnitStruct; |
| 8 |
|
| 9 |
#[derive(Debug, PartialEq, Deserialize, Serialize)] |
| 10 |
struct NewType(f32); |
| 11 |
|
| 12 |
#[derive(Debug, PartialEq, Deserialize, Serialize)] |
| 13 |
struct TupleStruct(UnitStruct, i8); |
| 14 |
|
| 15 |
#[derive(Debug, PartialEq, Eq, Hash, Deserialize, Serialize)] |
| 16 |
struct Key(u32); |
| 17 |
|
| 18 |
#[derive(Debug, PartialEq, Eq, Hash, Deserialize, Serialize)] |
| 19 |
enum Enum { |
| 20 |
Unit, |
| 21 |
Bool(bool), |
| 22 |
Chars(char, String), |
| 23 |
} |
| 24 |
|
| 25 |
#[derive(Debug, PartialEq, Deserialize, Serialize)] |
| 26 |
struct Struct { |
| 27 |
tuple: ((), NewType, TupleStruct), |
| 28 |
vec: Vec<Option<UnitStruct>>, |
| 29 |
map: HashMap<Key, Enum>, |
| 30 |
} |
| 31 |
|
| 32 |
#[test] |
| 33 |
fn roundtrip() { |
| 34 |
let value = Struct { |
| 35 |
tuple: ((), NewType(0.5), TupleStruct(UnitStruct, -5)), |
| 36 |
vec: vec![None, Some(UnitStruct)], |
| 37 |
map: vec![ |
| 38 |
(Key(5), Enum::Unit), |
| 39 |
(Key(6), Enum::Bool(false)), |
| 40 |
(Key(7), Enum::Bool(true)), |
| 41 |
(Key(9), Enum::Chars('x', "".to_string())), |
| 42 |
] |
| 43 |
.into_iter() |
| 44 |
.collect(), |
| 45 |
}; |
| 46 |
|
| 47 |
let serial = ron::ser::to_string(&value).unwrap(); |
| 48 |
|
| 49 |
println!("Serialized: {}", serial); |
| 50 |
|
| 51 |
let deserial = ron::de::from_str(&serial); |
| 52 |
|
| 53 |
assert_eq!(Ok(value), deserial); |
| 54 |
} |
| 55 |
|
| 56 |
#[test] |
| 57 |
fn roundtrip_pretty() { |
| 58 |
let value = Struct { |
| 59 |
tuple: ((), NewType(0.5), TupleStruct(UnitStruct, -5)), |
| 60 |
vec: vec![None, Some(UnitStruct)], |
| 61 |
map: vec![ |
| 62 |
(Key(5), Enum::Unit), |
| 63 |
(Key(6), Enum::Bool(false)), |
| 64 |
(Key(7), Enum::Bool(true)), |
| 65 |
(Key(9), Enum::Chars('x', "".to_string())), |
| 66 |
] |
| 67 |
.into_iter() |
| 68 |
.collect(), |
| 69 |
}; |
| 70 |
|
| 71 |
let pretty = ron::ser::PrettyConfig::new() |
| 72 |
.enumerate_arrays(true) |
| 73 |
.extensions(Extensions::IMPLICIT_SOME); |
| 74 |
let serial = ron::ser::to_string_pretty(&value, pretty).unwrap(); |
| 75 |
|
| 76 |
println!("Serialized: {}", serial); |
| 77 |
|
| 78 |
let deserial = ron::de::from_str(&serial); |
| 79 |
|
| 80 |
assert_eq!(Ok(value), deserial); |
| 81 |
} |
| 82 |
|
| 83 |
#[test] |
| 84 |
fn roundtrip_sep_tuple_members() { |
| 85 |
#[derive(Debug, Deserialize, PartialEq, Eq, Serialize)] |
| 86 |
pub enum FileOrMem { |
| 87 |
File(String), |
| 88 |
Memory, |
| 89 |
} |
| 90 |
|
| 91 |
#[derive(Debug, Deserialize, PartialEq, Serialize)] |
| 92 |
struct Both { |
| 93 |
a: Struct, |
| 94 |
b: FileOrMem, |
| 95 |
} |
| 96 |
|
| 97 |
let a = Struct { |
| 98 |
tuple: ((), NewType(0.5), TupleStruct(UnitStruct, -5)), |
| 99 |
vec: vec![None, Some(UnitStruct)], |
| 100 |
map: vec![ |
| 101 |
(Key(5), Enum::Unit), |
| 102 |
(Key(6), Enum::Bool(false)), |
| 103 |
(Key(7), Enum::Bool(true)), |
| 104 |
(Key(9), Enum::Chars('x', "".to_string())), |
| 105 |
] |
| 106 |
.into_iter() |
| 107 |
.collect(), |
| 108 |
}; |
| 109 |
let b = FileOrMem::File("foo".to_owned()); |
| 110 |
|
| 111 |
let value = Both { a, b }; |
| 112 |
|
| 113 |
let pretty = ron::ser::PrettyConfig::new().separate_tuple_members(true); |
| 114 |
let serial = ron::ser::to_string_pretty(&value, pretty).unwrap(); |
| 115 |
|
| 116 |
println!("Serialized: {}", serial); |
| 117 |
|
| 118 |
let deserial = ron::de::from_str(&serial); |
| 119 |
|
| 120 |
assert_eq!(Ok(value), deserial); |
| 121 |
} |