reactive_graph_client/client/instances/properties/
args.rs

1use clap::Args;
2use serde_json::Value;
3use std::error::Error;
4use std::str::FromStr;
5
6/// The property type.
7#[derive(Args, Debug, Clone)]
8pub(crate) struct PropertyInstanceArgs {
9    /// The name of the property.
10    pub property_name: String,
11
12    /// The JSON value of the property.
13    ///
14    /// 'true' is boolean true, '"true"' is the string "true"
15    #[clap(value_parser = parse_json)]
16    pub property_value: Value,
17}
18
19impl PropertyInstanceArgs {
20    pub fn new(property_name: String, property_value: Value) -> Self {
21        Self { property_name, property_value }
22    }
23}
24
25impl FromStr for PropertyInstanceArgs {
26    type Err = ();
27
28    fn from_str(s: &str) -> Result<Self, Self::Err> {
29        let (property_name, property_value) = s.split_once("=").ok_or(())?;
30        let property_value = property_value.parse::<Value>().map_err(|_| ())?;
31        Ok(PropertyInstanceArgs::new(property_name.to_string(), property_value))
32    }
33}
34
35pub fn parse_property(s: &str) -> Result<(String, Value), Box<dyn Error + Send + Sync + 'static>> {
36    let pos = s.find('=').ok_or_else(|| format!("invalid KEY=value: no `=` found in `{s}`"))?;
37    let key = s[..pos].parse()?;
38    let value = s[pos + 1..].to_string();
39    let value = Value::from_str(&value)?;
40    Ok((key, value))
41}
42
43pub fn parse_json(s: &str) -> Result<Value, Box<dyn Error + Send + Sync + 'static>> {
44    Ok(Value::from_str(s)?)
45}