From 296e0bc82c532acd77a4ceeb83490bc93628442d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcel=20M=C3=BCller?= Date: Wed, 11 May 2022 09:05:53 +0200 Subject: Add Config derive macro 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 | 63 +++++++++++++++++++++++--- 1 file changed, 57 insertions(+), 6 deletions(-) (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 index 9cefa5a1..498a79b7 100644 --- a/crates/core/tedge_api/examples/print_config.rs +++ b/crates/core/tedge_api/examples/print_config.rs @@ -2,7 +2,10 @@ use std::collections::HashMap; use nu_ansi_term::Color; use pretty::Arena; -use tedge_api::config::{AsConfig, ConfigDescription, ConfigKind}; +use tedge_api::{ + config::{AsConfig, ConfigDescription, ConfigKind}, + Config, +}; struct Port(u64); impl AsConfig for Port { @@ -21,7 +24,7 @@ impl AsConfig for VHost { fn as_config() -> ConfigDescription { ConfigDescription::new( String::from("VHost"), - ConfigKind::Struct(vec![("name", String::as_config())]), + ConfigKind::Struct(vec![("name", None, String::as_config())]), Some("A virtual host definition"), ) } @@ -48,10 +51,10 @@ fn main() { let doc = ConfigDescription::new( String::from("ServerConfig"), ConfigKind::Struct(vec![ - ("port", Port::as_config()), - ("interface", String::as_config()), - ("virtual_hosts", Vec::::as_config()), - ("headers", HashMap::::as_config()), + ("port", None, Port::as_config()), + ("interface", None, String::as_config()), + ("virtual_hosts", None, Vec::::as_config()), + ("headers", None, HashMap::::as_config()), ]), Some("Specify how the server should be started\n\n## Note\n\nThis is a reallly really loooooooooooooooooong loooooooooooooooooooong new *line*."), ); @@ -76,5 +79,53 @@ fn main() { ); 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, + 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::() + )) + ); + println!("{}", output); println!("-------"); } -- cgit v1.2.3