| 1 | use std::collections::BTreeMap; |
| 2 | |
| 3 | use ron::{ |
| 4 | de::from_str, |
| 5 | ser::{to_string_pretty, PrettyConfig}, |
| 6 | }; |
| 7 | use serde::{Deserialize, Serialize}; |
| 8 | |
| 9 | #[derive(Debug, Deserialize, Serialize)] |
| 10 | struct Config { |
| 11 | boolean: bool, |
| 12 | float: f32, |
| 13 | map: BTreeMap<u8, char>, |
| 14 | nested: Nested, |
| 15 | tuple: (u32, u32), |
| 16 | } |
| 17 | |
| 18 | #[derive(Debug, Deserialize, Serialize)] |
| 19 | struct Nested { |
| 20 | a: String, |
| 21 | b: char, |
| 22 | } |
| 23 | |
| 24 | fn read_original(source: &str) -> String { |
| 25 | source.to_string().replace("\r\n", "\n") |
| 26 | } |
| 27 | |
| 28 | fn make_roundtrip(source: &str) -> String { |
| 29 | let config: Config = from_str(source).unwrap(); |
| 30 | let pretty = PrettyConfig::new() |
| 31 | .depth_limit(3) |
| 32 | .separate_tuple_members(true) |
| 33 | .enumerate_arrays(true) |
| 34 | .new_line("\n"); |
| 35 | to_string_pretty(&config, pretty).expect("Serialization failed") |
| 36 | } |
| 37 | |
| 38 | #[test] |
| 39 | fn test_sequence_ex1() { |
| 40 | let file = include_str!("preserve_sequence_ex1.ron"); |
| 41 | assert_eq!(read_original(file), make_roundtrip(file)); |
| 42 | } |
| 43 | |
| 44 | #[test] |
| 45 | fn test_sequence_ex2() { |
| 46 | let file = include_str!("preserve_sequence_ex2.ron"); |
| 47 | assert_eq!(read_original(file), make_roundtrip(file)); |
| 48 | } |