Timeline: resolve exact feature names before ID fallbacks (#9689) (#9748)

* claim: #9689

* Resolve timeline names before falling back to feature IDs
This commit is contained in:
Colin Faulkingham
2026-10-31 00:08:24 +00:00
committed by GitHub
parent 526cdfd247
commit 0fe5bf114c
7 changed files with 94 additions and 48 deletions
+17 -27
View File
@@ -6,7 +6,7 @@ use serde_json::{Value, json};
use solidcraft_doc::{Feature, FeatureKind};
use super::CommandSpec;
use crate::params::{bad, bool_, name_or_id_list, num, str_};
use crate::params::{bad, bool_, name_or_id_values, num, str_};
use crate::{EngineError, Result, Sel, Session, Snapshot};
pub static COMMANDS: &[CommandSpec] = &[
@@ -179,47 +179,37 @@ fn compute_all(s: &mut Session, _p: &Value) -> Result<Value> {
Ok(json!({"features": s.model.results.len(), "errors": errors}))
}
fn feature_id(s: &Session, v: Option<&Value>, cmd: &str) -> Result<u64> {
let key = match v {
Some(Value::Number(n)) => n.to_string(),
Some(Value::String(x)) => x.clone(),
_ => return Err(bad(cmd, "`feature` must be an id or name")),
};
s.doc.find_feature(&key).map(|f| f.id).ok_or_else(|| bad(cmd, format!("no feature `{key}`")))
pub(super) fn feature_id(s: &Session, v: Option<&Value>, cmd: &str) -> Result<u64> {
match v {
Some(Value::Number(n)) => n.as_u64().and_then(|id| s.doc.feature(id)).map(|f| f.id).ok_or_else(|| bad(cmd, format!("no feature `{n}`"))),
Some(Value::String(name)) => s.doc.resolve_feature_name(name).map(|f| f.id).map_err(|e| bad(cmd, e.to_string())),
_ => Err(bad(cmd, "`feature` must be an id or name")),
}
}
fn delete(s: &mut Session, p: &Value) -> Result<Value> {
let cmd = "timeline.delete";
// The features are named or numbered, and both kinds can sit in one list.
let mut keys = name_or_id_list(cmd, p, "features")?;
if let Some(f) = p.get("feature") {
keys.push(match f {
Value::Number(n) => n.to_string(),
Value::String(x) => x.clone(),
_ => String::new(),
});
}
// Resolve every key against the same timeline. A cascade or a repeated key must never
// redirect a later delete to another feature that happens to match after the first one.
let keys = name_or_id_values(cmd, p, "features")?;
let mut ids = keys.iter().chain(p.get("feature")).map(|v| feature_id(s, Some(v), cmd)).collect::<Result<Vec<_>>>()?;
let groups = group_ids(s, p, false, cmd)?;
if keys.is_empty() && groups.is_empty() {
if ids.is_empty() && groups.is_empty() {
return Err(bad(cmd, "`features` must list features, or `groups` timeline folders"));
}
// A folder goes with its features and nested folders, its features in timeline order.
let (folders, contents) = group_tree(&s.doc, &groups);
let joints = group_joint_ids(&s.doc, &groups);
keys.extend(s.doc.features.iter().filter(|f| contents.contains(&f.id)).map(|f| f.id.to_string()));
ids.extend(s.doc.features.iter().filter(|f| contents.contains(&f.id)).map(|f| f.id));
// Features that work now; any of them failing after the delete is reported (a later
// feature that names a deleted body, #267).
let worked: std::collections::BTreeSet<u64> = s.model.results.iter().filter(|r| r.error.is_none() && !r.skipped).map(|r| r.id).collect();
super::joints::delete_joints(s.doc_mut(), &joints);
let mut gone = Vec::new();
for k in keys {
let Some(id) = s.doc.find_feature(&k).map(|f| f.id) else {
if gone.is_empty() {
return Err(bad(cmd, format!("no feature `{k}`")));
}
continue;
};
gone.extend(s.doc_mut().delete_feature(id)?);
for id in ids {
if s.doc.feature(id).is_some() {
gone.extend(s.doc_mut().delete_feature(id)?);
}
}
if !folders.is_empty() {
s.doc_mut().timeline_groups.retain(|group| !folders.contains(&group.id));