mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-10-27 11:17:01 -06:00
fix(selection): reject malformed anchor references before selection changes
This commit is contained in:
committed by
bflatastic
parent
2f458842ae
commit
2f984f2e7a
@@ -301,10 +301,34 @@ pub(crate) fn parse_refs(v: Option<&Value>) -> Vec<(usize, usize)> {
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
/// Parse an explicitly supplied anchor list without dropping invalid entries.
|
||||
fn checked_refs(v: Option<&Value>, cmd: &str) -> Result<Vec<(usize, usize)>> {
|
||||
let refs = v.and_then(Value::as_array).ok_or_else(|| bad(cmd, "`anchors` must be an array of [subpath, anchor] pairs"))?;
|
||||
refs.iter()
|
||||
.map(|v| {
|
||||
let Some([subpath, anchor]) = v.as_array().map(Vec::as_slice) else {
|
||||
return Err(bad(cmd, format!("invalid anchor reference {v}: expected [subpath, anchor]")));
|
||||
};
|
||||
let coordinate = |v: &Value| {
|
||||
v.as_u64()
|
||||
.and_then(|n| usize::try_from(n).ok())
|
||||
.ok_or_else(|| bad(cmd, format!("invalid anchor index {v}: expected a non-negative integer")))
|
||||
};
|
||||
Ok((coordinate(subpath)?, coordinate(anchor)?))
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn anchors(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let id = id_param(p, "id").ok_or_else(|| bad("select.anchors", "missing id"))?;
|
||||
let refs = parse_refs(p.get("anchors"));
|
||||
let mode = str_param(p, "mode").unwrap_or("set").to_string();
|
||||
const C: &str = "select.anchors";
|
||||
let id = checked_id(s, p.get("id").ok_or_else(|| bad(C, "missing id"))?, C)?;
|
||||
let refs = checked_refs(p.get("anchors"), C)?;
|
||||
let mode = match str_param(p, "mode") {
|
||||
None | Some("set") => "set",
|
||||
Some("add") => "add",
|
||||
Some("toggle") => "toggle",
|
||||
Some(m) => return Err(bad(C, format!("unknown mode {m:?}"))),
|
||||
};
|
||||
s.select(|_, sel| {
|
||||
if mode == "set" {
|
||||
sel.clear();
|
||||
@@ -323,11 +347,16 @@ fn anchors(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
}
|
||||
|
||||
fn anchors_many(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let items: Vec<(NodeId, BTreeSet<(usize, usize)>)> = p
|
||||
.get("items")
|
||||
.and_then(Value::as_array)
|
||||
.map(|a| a.iter().filter_map(|it| Some((NodeId(it.get("id")?.as_u64()?), parse_refs(it.get("anchors")).into_iter().collect()))).collect())
|
||||
.unwrap_or_default();
|
||||
const C: &str = "select.anchorsMany";
|
||||
let raw = p.get("items").and_then(Value::as_array).ok_or_else(|| bad(C, "`items` must be an array of {id, anchors}"))?;
|
||||
let items: Vec<(NodeId, BTreeSet<(usize, usize)>)> = raw
|
||||
.iter()
|
||||
.map(|it| {
|
||||
let id = checked_id(s, it.get("id").ok_or_else(|| bad(C, "each item needs an id"))?, C)?;
|
||||
let refs = checked_refs(it.get("anchors"), C)?;
|
||||
Ok((id, refs.into_iter().collect()))
|
||||
})
|
||||
.collect::<Result<_>>()?;
|
||||
// `add: true` is the older spelling of mode add.
|
||||
let mode = match str_param(p, "mode") {
|
||||
Some(m @ ("set" | "add" | "toggle" | "subtract")) => m,
|
||||
|
||||
Reference in New Issue
Block a user