summaryrefslogtreecommitdiffstats
path: root/crates/core/tedge_api/examples/print_config.rs
blob: 498a79b7dd4c3a4e52c70e1707897f6cd3b53b9e (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
use std::collections::HashMap;

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

impl AsConfig for Port {
    fn as_config() -> ConfigDescription {
        ConfigDescription::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() -> ConfigDescription {
        ConfigDescription::new(
            String::from("VHost"),
            ConfigKind::Struct(vec![("name", None, 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 = ConfigDescription::new(
            String::from("ServerConfig"),
            ConfigKind::Struct(vec![
                ("port", None, Port::as_config()),
                ("interface", None, String::as_config()),
                ("virtual_hosts", None, Vec::<VHost>::as_config()),
                ("headers", None, 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);
    let arena = Arena::new();

    #[derive(Config)]
    #[config(tag = "type")]
    /// An Nginx virtual host
    ///
    /// # Note
    ///
    /// This is an example and as such is nonsense
    enum NginxVHost {
        /// A simple host consisting of a string
        Simple(String),
        /// A more complex host that can also specify its port
        Complex {
            /// the name of the VHost
            name: String,
            port: Port,
        },
    }

    #[derive(Config)]
    struct NginxConfig {
        vhosts: Vec<NginxVHost>,
        allow_priv_ports: bool,
    }

    let doc = NginxConfig::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 NginxConfig");
    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);
    println!("-------");
}