usage/spec/
choices.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
use kdl::KdlNode;
use serde::{Deserialize, Serialize};

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

#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct SpecChoices {
    pub choices: Vec<String>,
}

impl SpecChoices {
    pub(crate) fn parse(_ctx: &ParsingContext, node: &NodeHelper) -> Result<Self, UsageErr> {
        let mut config = Self::default();
        node.ensure_arg_len(1..)?;
        config.choices = node
            .args()
            .map(|e| e.ensure_string().map(String::from))
            .collect::<Result<_, _>>()?;
        Ok(config)
    }
}

impl From<&SpecChoices> for KdlNode {
    fn from(arg: &SpecChoices) -> Self {
        let mut node = KdlNode::new("choices");
        for choice in &arg.choices {
            node.push(choice.to_string());
        }
        node
    }
}