summaryrefslogtreecommitdiffstats
path: root/crates/core/tedge_api/examples
diff options
context:
space:
mode:
authorMarcel Müller <m.mueller@ifm.com>2022-05-11 09:05:53 +0200
committerMarcel Müller <m.mueller@ifm.com>2022-05-12 08:51:18 +0200
commit296e0bc82c532acd77a4ceeb83490bc93628442d (patch)
tree5d5ab8b56678f2c5bc65a32cc6fd4ca609de886b /crates/core/tedge_api/examples
parent28cecb8697b4a5eec80503fe2b13a2d1b4917bbe (diff)
Add Config derive macro
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.rs63
1 files changed, 57 insertions, 6 deletions
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::<VHost>::as_config()),
- ("headers", HashMap::<String, String>::as_config()),
+ ("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*."),
);
@@ -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<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!("-------");
}