use std::str::FromStr;
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
#[schemars(rename = "laboratories.Mount")]
pub struct Mount {
pub host: String,
pub container: String,
}
impl FromStr for Mount {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let mut host: Option<String> = None;
let mut container: Option<String> = None;
for (k, v) in tokenize(s)? {
match k {
"host" => host = Some(v.to_string()),
"container" => container = Some(v.to_string()),
other => return Err(format!("unknown key: {other}")),
}
}
match (host, container) {
(Some(host), Some(container)) => Ok(Mount { host, container }),
(None, _) => Err("host is required".to_string()),
(_, None) => Err("container is required".to_string()),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
#[schemars(rename = "laboratories.EnvVar")]
pub struct EnvVar {
pub key: String,
pub value: String,
}
impl FromStr for EnvVar {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.split_once('=') {
Some((key, value)) => Ok(EnvVar {
key: key.to_string(),
value: value.to_string(),
}),
None => Err(format!("expected KEY=VALUE, got: {s}")),
}
}
}
fn tokenize(s: &str) -> Result<Vec<(&str, &str)>, String> {
s.split(',')
.map(|pair| {
pair.split_once('=')
.map(|(k, v)| (k.trim(), v.trim()))
.ok_or_else(|| format!("expected key=value, got: {pair}"))
})
.collect()
}