summaryrefslogtreecommitdiffstats
path: root/crates/core/tedge_api/examples
diff options
context:
space:
mode:
authorMarcel Müller <m.mueller@ifm.com>2022-05-05 11:34:07 +0200
committerMarcel Müller <m.mueller@ifm.com>2022-05-05 15:07:19 +0200
commit0106fddceb59f0f183ca9df44271e9ec4103dd4b (patch)
tree3b6e8334a46cdd4ec8b00376e9aac7362f71332e /crates/core/tedge_api/examples
parentec704c858e39d17ea25d5f5931a68ad044819e84 (diff)
Move printing config to example from tests
Signed-off-by: Marcel Müller <m.mueller@ifm.com>
Diffstat (limited to 'crates/core/tedge_api/examples')
-rw-r--r--crates/core/tedge_api/examples/print_config.rs80
1 files changed, 80 insertions, 0 deletions
diff --git a/crates/core/tedge_api/examples/print_config.rs b/crates/core/tedge_api/examples/print_config.rs
new file mode 100644
index 00000000..b7796a65
--- /dev/null
+++ b/crates/core/tedge_api/examples/print_config.rs
@@ -0,0 +1,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!("-------");
+}