use serde::{Deserialize, Serialize};
use super::Merge;
#[derive(Serialize, Default, Deserialize, PartialEq, Eq, Clone, Debug, schemars::JsonSchema)]
pub struct CapabilityWasiV1 {
#[serde(skip_serializing_if = "Option::is_none")]
pub cli_args: Option<Vec<String>>,
#[serde(skip_serializing_if = "Option::is_none")]
#[schemars(with = "Option<Vec<JsonValidationEnvVar>>")]
pub env_vars: Option<Vec<EnvVarV1>>,
}
impl Merge for CapabilityWasiV1 {
fn merge_extend(self, other: &Self) -> Self {
Self {
cli_args: self.cli_args.merge_extend(&other.cli_args),
env_vars: match (self.env_vars, &other.env_vars) {
(None, None) => None,
(Some(e), None) => Some(e),
(None, Some(e)) => Some(e.clone()),
(Some(a), Some(b)) => Some(merge_env_vars(a, b.clone())),
},
}
}
}
fn merge_env_vars(mut a: Vec<EnvVarV1>, b: Vec<EnvVarV1>) -> Vec<EnvVarV1> {
a.extend(b);
a
}
#[derive(Serialize, Deserialize, PartialEq, Eq, Clone, Debug, schemars::JsonSchema)]
pub struct EnvVarV1 {
pub name: String,
#[serde(flatten)]
pub source: EnvVarSourceV1,
}
impl EnvVarV1 {
pub fn new_value(name: impl Into<String>, value: impl Into<String>) -> Self {
Self {
name: name.into(),
source: EnvVarSourceV1::Value(value.into()),
}
}
}
#[derive(Serialize, Deserialize, PartialEq, Eq, Clone, Debug, schemars::JsonSchema)]
#[serde(untagged)]
enum JsonValidationEnvVar {
Value(JsonValidationEnvVarValue),
}
#[derive(Serialize, Deserialize, PartialEq, Eq, Clone, Debug, schemars::JsonSchema)]
struct JsonValidationEnvVarValue {
name: String,
value: String,
}
#[derive(Serialize, Deserialize, PartialEq, Eq, Clone, Debug, schemars::JsonSchema)]
pub enum EnvVarSourceV1 {
#[serde(rename = "value")]
Value(String),
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_env_var_deser() {
let v = EnvVarV1 {
name: "FOO".to_string(),
source: EnvVarSourceV1::Value("BAR".to_string()),
};
let s = serde_json::to_string(&v).unwrap();
assert_eq!(s, r#"{"name":"FOO","value":"BAR"}"#);
let e2: EnvVarV1 = serde_json::from_str(&s).unwrap();
assert_eq!(e2, v);
}
}