assemble_freight/
project_properties.rs

1use std::collections::HashMap;
2
3#[derive(Debug, clap::Args, Clone, merge::Merge)]
4pub struct ProjectProperties {
5    /// Property flags
6    #[clap(short = 'P', long = "project-property")]
7    #[clap(value_parser(try_parse_property))]
8    #[clap(value_name = "KEY[=VALUE]")]
9    #[merge(strategy = merge::vec::append)]
10    properties: Vec<(String, Option<String>)>,
11}
12
13const MISSING_VALUE: &str = "";
14
15impl ProjectProperties {
16    pub fn properties(&self) -> HashMap<String, Option<String>> {
17        self.properties.clone().into_iter().collect()
18    }
19
20    /// Get a property
21    pub fn property<S: AsRef<str>>(&self, prop: S) -> Option<&str> {
22        let prop = prop.as_ref();
23        for (key, value) in &self.properties {
24            if key == prop {
25                return Some(value.as_ref().map(|s| s.as_str()).unwrap_or(MISSING_VALUE));
26            }
27        }
28        None
29    }
30}
31
32fn try_parse_property(prop: &str) -> Result<(String, Option<String>), String> {
33    if prop.contains('=') {
34        let (prop, value) = prop.split_once('=').unwrap();
35        Ok((prop.to_string(), Some(value.to_string())))
36    } else {
37        Ok((prop.to_string(), None))
38    }
39}