summaryrefslogtreecommitdiffstats
path: root/crates/core/tedge_api/examples/print_config.rs
blob: b7796a65b56d5d186526d1577a693d016ea172fd (plain)
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
use std::collections::HashMap;

use nu_ansi_term::Color;
use pretty::Arena;
use tedge_api::config::{AsConfig, Config, ConfigKind};
struct Port(u64);

impl AsConfig for Port {
    fn as_config() -> Config {
        Config::new(
            String::from("Integer"),
            ConfigKind::Integer,
            Some("A TCP port number is an integer between 0 and 65535"),
        )
    }
}

struct VHost;

impl AsConfig for VHost {
    fn as_config() -> Config {
        Config::new(
            String::from("VHost"),
            ConfigKind::Struct(HashMap::from([(String::from("name"), String::as_config())])),
            Some("A virtual host definition"),
        )
    }
}

fn main() {
    let arena = Arena::new();

    let doc = Vec::<String>::as_config();
    let rendered_doc = doc.as_terminal_doc(&arena);

    let mut output = String::new();

    rendered_doc.render_fmt(80, &mut output).unwrap();

    println!(
        "------- Output for {}",
        std::any::type_name::<Vec<String>>()
    );
    println!("{}", output);

    let arena = Arena::new();

    let doc = Config::new(
            String::from("ServerConfig"),
            ConfigKind::Struct(HashMap::from([
                (String::from("port"), Port::as_config()),
                (String::from("interface"), String::as_config()),
                (String::from("virtual_hosts"), Vec::<VHost>::as_config()),
                (String::from("headers"), HashMap::<String, String>::as_config()),
            ])),
            Some("Specify how the server should be started\n\n## Note\n\nThis is a reallly really loooooooooooooooooong loooooooooooooooooooong new *line*."),
        );
    let rendered_doc = doc.as_terminal_doc(&arena);

    let mut output = String::new();

    rendered_doc.render_fmt(80, &mut output).unwrap();

    println!(
        "Configuration for {} plugin kinds",
        Color::White.bold().paint(doc.name())
    );
    println!(
        "{}",
        Color::White.dimmed().bold().paint(format!(
            "=================={}=============",
            std::iter::repeat('=')
                .take(doc.name().len())
                .collect::<String>()
        ))
    );
    println!("------- Output for ServerConfig");
    println!("{}", output);
    println!("-------");
}