use super::*;
#[derive(Debug, Clone, PartialEq)]
pub struct Compound {
pub name: String,
pub configurations: Vec<String>,
pub folders: Vec<Option<String>>,
pub pre_launch_task: Option<String>,
pub presentation: Option<Presentation>,
pub unsupported_keys: Vec<&'static str>,
pub source: &'static str,
pub stop_all: bool,
pub answers: BTreeMap<String, String>,
}
pub fn parse_compounds(text: &str, source: &'static str) -> Vec<Compound> {
let Ok(v) = serde_json::from_str::<Value>(&crate::jsonc::strip_jsonc(text)) else {
return Vec::new();
};
let Some(Value::Array(list)) = v.get("compounds") else {
return Vec::new();
};
list.iter()
.filter_map(|c| {
let obj = c.as_object()?;
let name = obj.get("name")?.as_str()?.to_string();
let members: Vec<(String, Option<String>)> = obj
.get("configurations")?
.as_array()?
.iter()
.map(|m| match m {
Value::String(s) => Some((s.clone(), None)),
Value::Object(o) => {
let name = o.get("name")?.as_str()?.to_string();
let folder = match o.get("folder") {
None | Some(Value::Null) => None,
Some(f) => Some(f.as_str()?.to_string()),
};
Some((name, folder))
}
_ => None,
})
.collect::<Option<Vec<_>>>()?;
let (configurations, folders): (Vec<String>, Vec<Option<String>>) =
members.into_iter().unzip();
if configurations.is_empty() {
return None;
}
let asks_for_something = |k: &str| match obj.get(k) {
None | Some(Value::Null) => false,
Some(Value::String(s)) => !s.is_empty(),
Some(Value::Bool(b)) => *b,
Some(Value::Object(o)) => !o.is_empty(),
Some(_) => true,
};
let presentation = parse_presentation(obj);
let mut unsupported_keys: Vec<&'static str> = malformed_presentation_keys(obj);
let stop_all = matches!(obj.get("stopAll"), Some(Value::Bool(true)));
if configurations.len() > 1
&& !matches!(
obj.get("stopAll"),
None | Some(Value::Null) | Some(Value::Bool(_))
)
{
unsupported_keys.push("stopAll");
}
let pre_launch_task = asks_for_something("preLaunchTask")
.then(|| obj.get("preLaunchTask").and_then(|v| v.as_str()))
.flatten()
.map(str::to_string);
if pre_launch_task.is_none() && asks_for_something("preLaunchTask") {
unsupported_keys.push("preLaunchTask");
}
Some(Compound {
name,
configurations,
folders,
source,
pre_launch_task,
presentation,
unsupported_keys,
stop_all,
answers: BTreeMap::new(),
})
})
.collect()
}
pub fn resolve_compound(
compound: &Compound,
configs: &[DebugConfig],
folders: &[(String, PathBuf)],
) -> Result<Vec<DebugConfig>, String> {
compound
.configurations
.iter()
.enumerate()
.map(|(i, want)| {
if let Some(folder) = compound.folders.get(i).cloned().flatten() {
let root = folder_root(folders, &folder).ok_or_else(|| {
format!(
"compound \"{}\" names folder \"{folder}\", which is not a folder of this workspace",
compound.name
)
})?;
let mut cfg = discover_configs_all(&root)
.into_iter()
.find(|c| &c.name == want)
.ok_or_else(|| {
format!(
"compound \"{}\" names configuration \"{want}\" in folder \"{folder}\", which no launch.json there declares",
compound.name
)
})?;
cfg.root = Some(root);
return Ok(cfg);
}
configs
.iter()
.find(|c| &c.name == want && c.source == compound.source)
.or_else(|| configs.iter().find(|c| &c.name == want))
.cloned()
.ok_or_else(|| {
format!(
"compound \"{}\" names configuration \"{}\", which no launch.json declares",
compound.name, want
)
})
})
.collect()
}