diff options
author | Marcel Müller <m.mueller@ifm.com> | 2022-05-11 09:04:44 +0200 |
---|---|---|
committer | Marcel Müller <m.mueller@ifm.com> | 2022-05-12 08:51:18 +0200 |
commit | 28cecb8697b4a5eec80503fe2b13a2d1b4917bbe (patch) | |
tree | e565f36c0c5bfb7b5b71388a9f978d3ea7fe25c9 /crates | |
parent | 8cf8816048a82aa62db2f08e9d6183bca2732154 (diff) |
Add enumerations to ConfigKinds
Signed-off-by: Marcel Müller <m.mueller@ifm.com>
Diffstat (limited to 'crates')
-rw-r--r-- | crates/core/tedge_api/src/config.rs | 124 |
1 files changed, 104 insertions, 20 deletions
diff --git a/crates/core/tedge_api/src/config.rs b/crates/core/tedge_api/src/config.rs index b64fefab..2cd2ce78 100644 --- a/crates/core/tedge_api/src/config.rs +++ b/crates/core/tedge_api/src/config.rs @@ -6,7 +6,7 @@ use serde::Serialize; use termimad::MadSkin; /// Generic config that represents what kind of config a plugin wishes to accept -#[derive(Debug, Serialize)] +#[derive(Debug, Serialize, PartialEq)] pub struct ConfigDescription { name: String, kind: ConfigKind, @@ -46,8 +46,17 @@ impl ConfigDescription { } } +/// The kind of enum tagging used by the [`ConfigKind`] +#[derive(Debug, Serialize, PartialEq)] +pub enum ConfigEnumKind { + /// An internal tag with the given tag name + Tagged(&'static str), + /// An untagged enum variant + Untagged, +} + /// The specific kind a [`Config`] represents -#[derive(Debug, Serialize)] +#[derive(Debug, Serialize, PartialEq)] pub enum ConfigKind { /// Config represents a boolean `true`/`false` Bool, @@ -80,17 +89,23 @@ pub enum ConfigKind { /// Config represents an array of values of the given [`ConfigKind`] Array(Box<ConfigDescription>), - /// Config represents a map of different configurations - /// - /// The tuple represent `(field_name, config_description)` - Struct(Vec<(&'static str, ConfigDescription)>), - /// Config represents a hashmap of named configurations of the same type /// /// # Note /// /// The key is always a [`String`] so this only holds the value config HashMap(Box<ConfigDescription>), + + /// Config represents a map of different configurations + /// + /// The tuple represent `(field_name, documentation, config_description)` + Struct(Vec<(&'static str, Option<&'static str>, ConfigDescription)>), + + /// Config represents multiple choice of configurations + Enum( + ConfigEnumKind, + Vec<(&'static str, Option<&'static str>, ConfigDescription)>, + ), } /// Turn a plugin configuration into a [`Config`] object @@ -159,12 +174,28 @@ impl ConfigDescription { let mut doc = arena .nil() .append(Color::LightBlue.bold().paint(self.name()).to_string()) + .append(arena.space()) + .append(match self.kind() { + ConfigKind::Bool + | ConfigKind::Integer + | ConfigKind::Float + | ConfigKind::String + | ConfigKind::Wrapped(_) + | ConfigKind::Array(_) + | ConfigKind::HashMap(_) => arena.nil(), + ConfigKind::Struct(_) => { + arena.text(Color::Blue.dimmed().paint("[Table]").to_string()) + } + ConfigKind::Enum(_, _) => { + arena.text(Color::Green.dimmed().paint("[Enum]").to_string()) + } + }) .append(arena.hardline()); - if let Some(conf_doc) = self.doc() { - let skin = MadSkin::default_dark(); - let rendered = skin.text(&conf_doc, None).to_string(); - doc = doc.append(arena.intersperse( + let skin = MadSkin::default_dark(); + let render_markdown = |text: &str| { + let rendered = skin.text(text, None).to_string(); + arena.intersperse( rendered.split("\n").map(|t| { arena.intersperse( t.split(char::is_whitespace).map(|t| t.to_string()), @@ -172,7 +203,11 @@ impl ConfigDescription { ) }), arena.hardline(), - )); + ) + }; + + if let Some(conf_doc) = self.doc() { + doc = doc.append(render_markdown(&conf_doc)); } match self.kind() { @@ -183,18 +218,67 @@ impl ConfigDescription { .append(Color::Blue.paint("[Members]").to_string()) .append(arena.hardline()) .append(arena.intersperse( - stc.iter().map(|(member_name, member_conf)| { - arena - .text(Color::Blue.bold().paint(*member_name).to_string()) - .append(": ") - .append( - Pretty::pretty(member_conf.as_terminal_doc(arena), arena) - .nest(4), - ) + stc.iter().map(|(member_name, member_doc, member_conf)| { + let mut doc = arena.nil(); + + if let Some(member_doc) = member_doc { + doc = doc.append(render_markdown(&member_doc)); + } + doc.append( + arena.text(Color::Blue.bold().paint(*member_name).to_string()), + ) + .append(": ") + .append( + Pretty::pretty(member_conf.as_terminal_doc(arena), arena).nest(4), + ) }), Doc::hardline(), )) } + ConfigKind::Enum(enum_kind, variants) => { + doc = doc + .append(arena.hardline()) + .append(Color::Green.paint("[Variants]").to_string()) + .append(arena.space()) + .append(match enum_kind { + ConfigEnumKind::Tagged(tag) => arena.text("Tagged with ").append( + arena.text( + Color::LightGreen + .italic() + .paint(format!("'{}'", tag)) + .to_string(), + ), + ), + ConfigEnumKind::Untagged => { + arena.text(Color::LightBlue.paint("Untagged").to_string()) + } + }) + .append(arena.hardline()) + .append( + arena.intersperse( + variants + .iter() + .map(|(member_name, member_doc, member_conf)| { + let mut doc = arena.nil(); + + if let Some(member_doc) = member_doc { + doc = doc.append(render_markdown(&member_doc)); + } + doc.append( + arena.text( + Color::Green.bold().paint(*member_name).to_string(), + ), + ) + .append(": ") + .append( + Pretty::pretty(member_conf.as_terminal_doc(arena), arena) + .nest(4), + ) + }), + Doc::hardline(), + ), + ); + } ConfigKind::Array(conf) | ConfigKind::HashMap(conf) | ConfigKind::Wrapped(conf) => { doc = doc.append(Pretty::pretty(conf.as_terminal_doc(arena), arena)) } |