| 1 | #![allow(dead_code)] |
| 2 | |
| 3 | use std::{collections::HashMap, fs::File}; |
| 4 | |
| 5 | use serde::Serialize; |
| 6 | |
| 7 | #[derive(Debug, Serialize)] |
| 8 | struct Config { |
| 9 | boolean: bool, |
| 10 | float: f32, |
| 11 | map: HashMap<u8, char>, |
| 12 | nested: Nested, |
| 13 | tuple: (u32, u32), |
| 14 | vec: Vec<Nested>, |
| 15 | } |
| 16 | |
| 17 | #[derive(Debug, Serialize)] |
| 18 | struct Nested { |
| 19 | a: String, |
| 20 | b: char, |
| 21 | } |
| 22 | |
| 23 | fn main() { |
| 24 | let config = Config { |
| 25 | boolean: true, |
| 26 | float: 8.2, |
| 27 | map: [(1, '1'), (2, '4'), (3, '9'), (4, '1'), (5, '2'), (6, '3')] |
| 28 | .into_iter() |
| 29 | .collect(), |
| 30 | nested: Nested { |
| 31 | a: String::from("Decode me!"), |
| 32 | b: 'z', |
| 33 | }, |
| 34 | tuple: (3, 7), |
| 35 | vec: vec![ |
| 36 | Nested { |
| 37 | a: String::from("Nested 1"), |
| 38 | b: 'x', |
| 39 | }, |
| 40 | Nested { |
| 41 | a: String::from("Nested 2"), |
| 42 | b: 'y', |
| 43 | }, |
| 44 | Nested { |
| 45 | a: String::from("Nested 3"), |
| 46 | b: 'z', |
| 47 | }, |
| 48 | ], |
| 49 | }; |
| 50 | |
| 51 | let f = File::options() |
| 52 | .create(true) |
| 53 | .write(true) |
| 54 | .open("example-out.ron") |
| 55 | .expect("Failed opening file"); |
| 56 | |
| 57 | ron::Options::default() |
| 58 | .to_io_writer_pretty(f, &config, ron::ser::PrettyConfig::new()) |
| 59 | .expect("Failed to write to file"); |
| 60 | } |