File: encode_file.rs

package info (click to toggle)
rust-ron 0.12.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,096 kB
  • sloc: makefile: 2
file content (60 lines) | stat: -rw-r--r-- 1,309 bytes parent folder | download | duplicates (18)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#![allow(dead_code)]

use std::{collections::HashMap, fs::File};

use serde::Serialize;

#[derive(Debug, Serialize)]
struct Config {
    boolean: bool,
    float: f32,
    map: HashMap<u8, char>,
    nested: Nested,
    tuple: (u32, u32),
    vec: Vec<Nested>,
}

#[derive(Debug, Serialize)]
struct Nested {
    a: String,
    b: char,
}

fn main() {
    let config = Config {
        boolean: true,
        float: 8.2,
        map: [(1, '1'), (2, '4'), (3, '9'), (4, '1'), (5, '2'), (6, '3')]
            .into_iter()
            .collect(),
        nested: Nested {
            a: String::from("Decode me!"),
            b: 'z',
        },
        tuple: (3, 7),
        vec: vec![
            Nested {
                a: String::from("Nested 1"),
                b: 'x',
            },
            Nested {
                a: String::from("Nested 2"),
                b: 'y',
            },
            Nested {
                a: String::from("Nested 3"),
                b: 'z',
            },
        ],
    };

    let f = File::options()
        .create(true)
        .write(true)
        .open("example-out.ron")
        .expect("Failed opening file");

    ron::Options::default()
        .to_io_writer_pretty(f, &config, ron::ser::PrettyConfig::new())
        .expect("Failed to write to file");
}