use cratefield_core::{
HARNESS_ONE_WORKER, HARNESS_SIDECARS, Harness, MapConfig, SidecarMount, SidecarMounts,
};
#[must_use]
pub fn from_cli_or_env(flag: Option<&str>) -> Option<String> {
flag.map(str::to_owned)
.or_else(|| std::env::var(HARNESS_SIDECARS).ok())
.filter(|table| !table.trim().is_empty())
}
pub fn parse(table: Option<&str>) -> Result<SidecarMounts, Vec<String>> {
let Some(table) = table else {
return Ok(SidecarMounts::default());
};
let mut pairs = vec![(HARNESS_SIDECARS.to_string(), table.to_string())];
if let Some(flag) = std::env::var(HARNESS_ONE_WORKER)
.ok()
.filter(|value| !value.trim().is_empty())
{
pairs.push((HARNESS_ONE_WORKER.to_string(), flag));
}
SidecarMounts::from_config(&MapConfig::from_pairs(pairs))
}
#[must_use]
pub fn shadowing<'a>(harness: &Harness, mounts: &'a SidecarMounts) -> Vec<&'a SidecarMount> {
let names: Vec<&str> = harness.modules().iter().map(|m| m.name()).collect();
mounts
.iter()
.filter(|mount| names.contains(&mount.name.as_str()))
.collect()
}
#[must_use]
pub fn listed(mounts: &SidecarMounts) -> String {
mounts
.iter()
.map(|mount| format!("`{}` (binding `{}`)", mount.name, mount.binding))
.collect::<Vec<_>>()
.join(", ")
}