diff options
author | Marcel Müller <m.mueller@ifm.com> | 2022-05-05 11:34:07 +0200 |
---|---|---|
committer | Marcel Müller <m.mueller@ifm.com> | 2022-05-05 15:07:19 +0200 |
commit | 0106fddceb59f0f183ca9df44271e9ec4103dd4b (patch) | |
tree | 3b6e8334a46cdd4ec8b00376e9aac7362f71332e /crates/core | |
parent | ec704c858e39d17ea25d5f5931a68ad044819e84 (diff) |
Move printing config to example from tests
Signed-off-by: Marcel Müller <m.mueller@ifm.com>
Diffstat (limited to 'crates/core')
-rw-r--r-- | crates/core/tedge_api/examples/print_config.rs | 80 | ||||
-rw-r--r-- | crates/core/tedge_api/src/config.rs | 76 |
2 files changed, 80 insertions, 76 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!("-------"); +} diff --git a/crates/core/tedge_api/src/config.rs b/crates/core/tedge_api/src/config.rs index 9ae1d532..3d397e51 100644 --- a/crates/core/tedge_api/src/config.rs +++ b/crates/core/tedge_api/src/config.rs @@ -191,9 +191,6 @@ impl Config { mod tests { use std::collections::HashMap; - use nu_ansi_term::Color; - use pretty::Arena; - use crate::config::{AsConfig, Config, ConfigKind}; #[test] @@ -214,77 +211,4 @@ mod tests { matches!(complex_config.kind(), ConfigKind::HashMap(map) if matches!(map.kind(), ConfigKind::Array(arr) if matches!(arr.kind(), ConfigKind::HashMap(inner_map) if matches!(inner_map.kind(), ConfigKind::String)))) ); } - - 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"), - ) - } - } - - #[test] - fn check_terminal_output() { - 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); - println!("-------"); - - 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); - println!("-------"); - } } |