File: yaml.rs

package info (click to toggle)
rust-bat 0.25.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,096 kB
  • sloc: sh: 255; python: 39; makefile: 14
file content (36 lines) | stat: -rw-r--r-- 913 bytes parent folder | download | duplicates (2)
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
/// A program that serializes a Rust structure to YAML and pretty-prints the result
use bat::{Input, PrettyPrinter};
use serde::Serialize;

#[derive(Serialize)]
struct Person {
    name: String,
    height: f64,
    adult: bool,
    children: Vec<Person>,
}

fn main() {
    let person = Person {
        name: String::from("Anne Mustermann"),
        height: 1.76f64,
        adult: true,
        children: vec![Person {
            name: String::from("Max Mustermann"),
            height: 1.32f64,
            adult: false,
            children: vec![],
        }],
    };

    let mut bytes = Vec::with_capacity(128);
    serde_yaml::to_writer(&mut bytes, &person).unwrap();
    PrettyPrinter::new()
        .language("yaml")
        .line_numbers(true)
        .grid(true)
        .header(true)
        .input(Input::from_bytes(&bytes).name("person.yaml").kind("File"))
        .print()
        .unwrap();
}