diff options
author | Marcel Müller <m.mueller@ifm.com> | 2022-05-12 09:14:42 +0200 |
---|---|---|
committer | Marcel Müller <m.mueller@ifm.com> | 2022-05-12 09:14:42 +0200 |
commit | 85abb91f4fa91c0f3b0991f5d59d2c5ecc68b8d8 (patch) | |
tree | 388986dbd7e4c8ef186646a17d23d6aa3212be17 /crates/core/tedge_api/examples | |
parent | 473801d5dd64ea9c2e669afe650ee210cd9edfa2 (diff) | |
parent | 3773563e6ca33c8340d7d4e9e384809e3d182fe9 (diff) |
Merge branch 'feature/add_config_exploration' into feature/add_tedge_api_only
Diffstat (limited to 'crates/core/tedge_api/examples')
-rw-r--r-- | crates/core/tedge_api/examples/print_config.rs | 85 |
1 files changed, 77 insertions, 8 deletions
diff --git a/crates/core/tedge_api/examples/print_config.rs b/crates/core/tedge_api/examples/print_config.rs index 5ee6458e..ba6e7a6b 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(HashMap::from([(String::from("name"), String::as_config())])), + ConfigKind::Struct(vec![("name", None, String::as_config())]), Some("A virtual host definition"), ) } @@ -47,12 +50,12 @@ fn main() { let doc = ConfigDescription::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()), - ])), + 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); @@ -76,5 +79,71 @@ 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, + }, + UndocumentedComplex { + num: u16, + foo: f32, + }, + } + + #[derive(Config)] + #[config(untagged)] + enum DebugLevel { + /// Enables debug output + /// + /// And info of course + Debug, + /// Only pertinent information will be logged + Info, + /// A custom debug level + Custom(String), + } + + #[derive(Config)] + struct NginxConfig { + vhosts: Vec<NginxVHost>, + debug_level: DebugLevel, + 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!("-------"); } |