usage/spec/
config.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
use std::collections::BTreeMap;

use kdl::{KdlDocument, KdlEntry, KdlNode};
use serde::Serialize;

use crate::error::UsageErr;
use crate::spec::context::ParsingContext;
use crate::spec::data_types::SpecDataTypes;
use crate::spec::helpers::NodeHelper;

#[derive(Debug, Default, Clone, Serialize)]
pub struct SpecConfig {
    pub props: BTreeMap<String, SpecConfigProp>,
}

impl SpecConfig {
    pub(crate) fn parse(ctx: &ParsingContext, node: &NodeHelper) -> Result<Self, UsageErr> {
        let mut config = Self::default();
        for node in node.children() {
            node.ensure_arg_len(1..=1)?;
            match node.name() {
                "prop" => {
                    let key = node.arg(0)?;
                    let key = key.ensure_string()?.to_string();
                    let mut prop = SpecConfigProp::default();
                    for (k, v) in node.props() {
                        match k {
                            "default" => prop.default = v.value.to_string().into(),
                            "default_note" => prop.default_note = Some(v.ensure_string()?),
                            "data_type" => prop.data_type = v.ensure_string()?.parse()?,
                            "env" => prop.env = v.ensure_string()?.to_string().into(),
                            "help" => prop.help = v.ensure_string()?.to_string().into(),
                            "long_help" => prop.long_help = v.ensure_string()?.to_string().into(),
                            k => bail_parse!(ctx, node.span(), "unsupported config prop key {k}"),
                        }
                    }
                    config.props.insert(key, prop);
                }
                k => bail_parse!(ctx, *node.node.name().span(), "unsupported config key {k}"),
            }
        }
        Ok(config)
    }

    pub(crate) fn merge(&mut self, other: &Self) {
        for (key, prop) in &other.props {
            self.props
                .entry(key.to_string())
                .or_insert_with(|| prop.clone());
        }
    }
}

impl SpecConfig {
    pub fn is_empty(&self) -> bool {
        self.props.is_empty()
    }
}

#[derive(Debug, Clone, Serialize)]
pub struct SpecConfigProp {
    pub default: Option<String>,
    pub default_note: Option<String>,
    pub data_type: SpecDataTypes,
    pub env: Option<String>,
    pub help: Option<String>,
    pub long_help: Option<String>,
}

impl SpecConfigProp {
    fn to_kdl_node(&self, key: String) -> KdlNode {
        let mut node = KdlNode::new("prop");
        node.push(KdlEntry::new(key));
        if let Some(default) = &self.default {
            node.push(KdlEntry::new_prop("default", default.clone()));
        }
        if let Some(default_note) = &self.default_note {
            node.push(KdlEntry::new_prop("default_note", default_note.clone()));
        }
        if let Some(env) = &self.env {
            node.push(KdlEntry::new_prop("env", env.clone()));
        }
        if let Some(help) = &self.help {
            node.push(KdlEntry::new_prop("help", help.clone()));
        }
        if let Some(long_help) = &self.long_help {
            node.push(KdlEntry::new_prop("long_help", long_help.clone()));
        }
        node
    }
}

impl Default for SpecConfigProp {
    fn default() -> Self {
        Self {
            default: None,
            default_note: None,
            data_type: SpecDataTypes::Null,
            env: None,
            help: None,
            long_help: None,
        }
    }
}

impl From<&SpecConfig> for KdlNode {
    fn from(config: &SpecConfig) -> Self {
        let mut node = KdlNode::new("config");
        for (key, prop) in &config.props {
            let doc = node.children_mut().get_or_insert_with(KdlDocument::new);
            doc.nodes_mut().push(prop.to_kdl_node(key.to_string()));
        }
        node
    }
}

#[cfg(test)]
mod tests {
    use crate::Spec;
    use insta::assert_snapshot;

    #[test]
    fn test_config_defaults() {
        let spec = Spec::parse(
            &Default::default(),
            r#"
config {
    prop "color" default=true env="COLOR" help="Enable color output"
    prop "user" default="admin" env="USER" help="User to run as"
    prop "jobs" default=4 env="JOBS" help="Number of jobs to run"
    prop "timeout" default=1.5 env="TIMEOUT" help="Timeout in seconds" \
        long_help="Timeout in seconds, can be fractional"
}
        "#,
        )
        .unwrap();

        assert_snapshot!(spec, @r###"
        config {
            prop "color" default="true" env="COLOR" help="Enable color output"
            prop "jobs" default="4" env="JOBS" help="Number of jobs to run"
            prop "timeout" default="1.5" env="TIMEOUT" help="Timeout in seconds" long_help="Timeout in seconds, can be fractional"
            prop "user" default="\"admin\"" env="USER" help="User to run as"
        }
        "###);
    }
}