From 0106fddceb59f0f183ca9df44271e9ec4103dd4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcel=20M=C3=BCller?= Date: Thu, 5 May 2022 11:34:07 +0200 Subject: Move printing config to example from tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Marcel Müller --- crates/core/tedge_api/examples/print_config.rs | 80 ++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 crates/core/tedge_api/examples/print_config.rs (limited to 'crates/core/tedge_api/examples') 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::::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::>() + ); + 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::::as_config()), + (String::from("headers"), HashMap::::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::() + )) + ); + println!("------- Output for ServerConfig"); + println!("{}", output); + println!("-------"); +} -- cgit v1.2.3