Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
86b31e3
fix(engine): table and layer edits by field name pick the exact name …
flight1973 Nov 19, 2026
6a7cadc
fix(engine): refuse design on case-twin fields and say which label cl…
flight1973 Nov 19, 2026
d8025e4
fix(engine): keep fields by name the way they're read, and say what d…
flight1973 Nov 19, 2026
c9e8558
Merge remote-tracking branch 'origin/main' into prep/edit-field
flight1973 Nov 20, 2026
5e4ec06
fix(engine): deleting a case twin takes the rules and class expressio…
flight1973 Nov 20, 2026
a988ff6
fix(engine): interpolation takes the exact field name before a case-i…
flight1973 Nov 20, 2026
ef155e2
fix(engine): suggest a way out RustGIS has when refusing a case-twin …
flight1973 Nov 20, 2026
702dbe3
docs: say what keeps a field from converting to dates (#1955)
flight1973 Nov 20, 2026
1b50f5b
fix(engine): a report removes an entry stored with a case twin's name…
flight1973 Nov 20, 2026
e5b8d13
fix(engine): size by field and diagrams set again keep the twin field…
flight1973 Nov 20, 2026
70108c0
fix(engine): deleting a field removes the label class clauses that re…
flight1973 Nov 20, 2026
9b838bc
fix(engine): a report filled in leaves out case twins and says which,…
flight1973 Nov 20, 2026
33ea900
fix(engine): size by field takes its own twin field back from the Sym…
flight1973 Nov 20, 2026
c9e7a6b
docs(engine): Convert Field's doc comment says what design refuses it…
flight1973 Nov 20, 2026
a05cb62
fix(engine): a report filled in finds case twins in one pass and name…
flight1973 Nov 20, 2026
7ff076c
fix(engine): a report filled in says every twin it leaves out and wha…
flight1973 Nov 21, 2026
bf84441
fix(engine): size by field takes a name that reads the field its mapp…
flight1973 Nov 21, 2026
188d8c1
fix(engine): a label class whose clause would read a deleted field's …
flight1973 Nov 21, 2026
7270418
Merge remote-tracking branch 'origin/main' into finish/edit-field
flight1973 Nov 28, 2026
23257da
fix(engine): settings kept by a field's name take a case twin only as…
flight1973 Nov 28, 2026
c2b3959
fix(engine): deleting a twin changes only the reports that read its l…
flight1973 Nov 28, 2026
74e415e
fix(engine): a stored twin name is kept only by its own label class o…
flight1973 Nov 28, 2026
42a8ee9
fix(engine): a stored twin name set again must still be a field, and …
flight1973 Nov 29, 2026
20681c0
fix(engine): a template leaves out twin defaults and says which, and …
flight1973 Nov 29, 2026
b1ad647
fix(engine): the template notice counts its twins and names the first…
flight1973 Nov 29, 2026
cca2665
Merge remote-tracking branch 'origin/main' into finish/edit-field
flight1973 Nov 29, 2026
5686b7a
fix(engine): the template twins notice is singular for one field (#1955)
flight1973 Nov 30, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 1 addition & 20 deletions crates/analysis/src/tools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1625,26 +1625,7 @@ fn keep_source_crs(out: &mut Dataset, ds: &Dataset, overlay: &Dataset) {
/// If multiple fields in `ds.fields` match `name` case-insensitively, returns an error
/// detailing the collision rather than silently selecting an arbitrary field.
pub fn find_field_case_insensitive(ds: &Dataset, name: &str) -> Result<usize, String> {
if let Some(idx) = ds.fields.iter().position(|f| f.name == name) {
return Ok(idx);
}
let mut matches = Vec::new();
for (idx, f) in ds.fields.iter().enumerate() {
if f.name.eq_ignore_ascii_case(name) {
matches.push(idx);
}
}
match matches.as_slice() {
[] => Err(format!("no field named '{name}'")),
[only] => Ok(*only),
_ => {
let names: Vec<String> = matches.iter().filter_map(|&i| ds.fields.get(i).map(|f| f.name.clone())).collect();
Err(format!(
"field name '{name}' is ambiguous: case-insensitive collision between {}",
names.iter().map(|n| format!("'{n}'")).collect::<Vec<_>>().join(" and ")
))
}
}
ds.find_field(name)?.ok_or_else(|| format!("no field named '{name}'"))
}

/// Union of polygons; rings are oriented first because the overlay infers its fill rule from
Expand Down
43 changes: 42 additions & 1 deletion crates/data/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1098,6 +1098,30 @@ pub fn export_error(ds: &Dataset, crs: raster::Crs) -> Option<String> {
ds.crs.and_then(|from| raster::planetary::export_error(from, crs)).map(|e| format!("{}: {e}", ds.name))
}

/// The index of the field `name` names in `fields`, for a command that changes a field (its
/// values, or the field itself) or a tool: the field named exactly so, else the only field named
/// so ignoring case; `None` when none is. A name matching several fields ignoring case but none
/// exactly is an error naming them, rather than an arbitrary pick (#1955).
pub fn find_field(fields: &[Field], name: &str) -> std::result::Result<Option<usize>, String> {
let mut alike = Vec::new();
for (i, f) in fields.iter().enumerate() {
if f.name == name {
return Ok(Some(i));
}
if f.name.eq_ignore_ascii_case(name) {
alike.push(i);
}
}
match alike.as_slice() {
[] => Ok(None),
[only] => Ok(Some(*only)),
_ => Err(format!(
"field name '{name}' is ambiguous: case-insensitive collision between {}; use an exact field name",
alike.iter().filter_map(|i| fields.get(*i)).map(|f| format!("'{}'", f.name)).collect::<Vec<_>>().join(" and ")
)),
}
}

impl Dataset {
/// Marks the dataset as having lost the Z or M values of `features` features (none: nothing)
/// and says so in `report`.
Expand Down Expand Up @@ -1125,11 +1149,28 @@ impl Dataset {
}
}

/// Index of a field by case-insensitive name.
/// Index of a field by case-insensitive name: the first so named. Expressions, queries and
/// what a layer keeps by a field's name (its renderer's, labels', design's…) read fields so.
pub fn field_index(&self, name: &str) -> Option<usize> {
self.fields.iter().position(|f| f.name.eq_ignore_ascii_case(name))
}

/// The field a command that changes a field names ([`find_field`]).
pub fn find_field(&self, name: &str) -> std::result::Result<Option<usize>, String> {
find_field(&self.fields, name)
}

/// Whether `name`, read as [`Dataset::field_index`] reads it, reads field `fi`.
pub fn reads_field(&self, fi: usize, name: &str) -> bool {
self.field_index(name) == Some(fi)
}

/// Whether more than one field is named `name` ignoring case: then what is kept by that name
/// reads the first of them ([`Dataset::field_index`]), whichever was meant.
pub fn has_case_twin(&self, name: &str) -> bool {
self.fields.iter().filter(|f| f.name.eq_ignore_ascii_case(name)).nth(1).is_some()
}

/// Value of `field` for feature `row` (Null when out of range).
pub fn value(&self, row: usize, field: usize) -> &Value {
self.features.value(row, field)
Expand Down
75 changes: 58 additions & 17 deletions crates/engine/src/cmd/design.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2230,17 +2230,27 @@ fn traverse(s: &mut Session, p: &Json) -> CmdResult {
super::edit::add_feature(s, &json!({"layer": id, "geometry": g}))
}

/// The field design names `name` ([`super::edit::kept_field`]): design reads its field by name,
/// the first so named ignoring case, so a name with case twins is refused. Every design command
/// finds its fields through this (#1955).
fn design_field(ds: &rustgis_data::Dataset, name: &str, what: &'static str) -> Result<usize, String> {
super::edit::kept_field(ds, name, what)?.ok_or_else(|| format!("no field named '{name}'"))
}

fn domain(s: &mut Session, p: &Json) -> CmdResult {
let id = s.layer_id(p)?;
let field = ps(p, "field").ok_or("field is required")?;
let l = s.map().layer(id).ok_or("no such layer")?;
let fi = l.dataset.field_index(field).ok_or_else(|| format!("no field named '{field}'"))?;
let (fname, ty) = l.dataset.fields.get(fi).map(|f| (f.name.clone(), f.ty)).ok_or("no field")?;
if b(p, "clear").unwrap_or(false) {
// Clearing takes off whatever domain the name reads, twins or not.
let fi = l.dataset.field_index(field).ok_or_else(|| format!("no field named '{field}'"))?;
let fname = l.dataset.fields.get(fi).map(|f| f.name.clone()).ok_or("no field")?;
let l = s.map_mut().layer_mut(id).ok_or("no such layer")?;
l.design.domains.retain(|d| !d.field.eq_ignore_ascii_case(&fname));
return Ok(Json::Null);
}
let fi = design_field(&l.dataset, field, "a domain")?;
let (fname, ty) = l.dataset.fields.get(fi).map(|f| (f.name.clone(), f.ty)).ok_or("no field")?;
let kind = if let Some(codes) = p.get("coded").and_then(Json::as_array) {
// A code its field can't hold as given is refused, not cut to another or left out.
let mut pairs: Vec<(Value, String)> = Vec::new();
Expand Down Expand Up @@ -2287,7 +2297,7 @@ fn subtypes(s: &mut Session, p: &Json) -> CmdResult {
}
let field = ps(p, "field").ok_or("field is required")?;
let l = s.map().layer(id).ok_or("no such layer")?;
let fi = l.dataset.field_index(field).ok_or_else(|| format!("no field named '{field}'"))?;
let fi = design_field(&l.dataset, field, "subtypes")?;
let f = l.dataset.fields.get(fi).ok_or("no field")?;
if f.ty != FieldType::Integer {
return Err(format!("{} must be an integer field to hold subtype codes", f.name));
Expand All @@ -2305,7 +2315,10 @@ fn subtypes(s: &mut Session, p: &Json) -> CmdResult {
.map(|o| o.iter().map(|(k, v)| (k.clone(), Value::from_json(v))).collect())
.unwrap_or_default();
for (field, value) in &mut defaults {
if let Some(ty) = l.dataset.field_index(field).and_then(|i| l.dataset.fields.get(i)).map(|f| f.ty) {
// (A default for a field the layer doesn't have is kept, as before.)
if let Some(ty) =
super::edit::kept_field(&l.dataset, field, "a subtype default")?.and_then(|i| l.dataset.fields.get(i)).map(|f| f.ty)
{
*value = super::edit::held_default(field, value, ty)?;
}
}
Expand Down Expand Up @@ -2388,12 +2401,9 @@ fn attribute_rule(s: &mut Session, p: &Json) -> CmdResult {
let field = match kind {
RuleKind::Calculation => {
let fl = ps(p, "field").ok_or("a calculation rule sets a field")?;
let fi = design_field(&l.dataset, fl, "a calculation rule")?;
super::edit::Calculation::parse(&expression, &l.dataset)?;
l.dataset
.field_index(fl)
.and_then(|i| l.dataset.fields.get(i))
.map(|f| f.name.clone())
.ok_or_else(|| format!("no field named '{fl}'"))?
l.dataset.fields.get(fi).map(|f| f.name.clone()).ok_or("no field")?
}
RuleKind::Constraint => {
rustgis_data::query::parse(&expression, &l.dataset)?;
Expand Down Expand Up @@ -2422,11 +2432,8 @@ fn contingent(s: &mut Session, p: &Json) -> CmdResult {
.iter()
.map(|v| {
let n = v.as_str().ok_or("fields are names")?;
l.dataset
.field_index(n)
.and_then(|i| l.dataset.fields.get(i))
.map(|f| (f.name.clone(), f.ty))
.ok_or_else(|| format!("no field named '{n}'"))
let fi = design_field(&l.dataset, n, "contingent values")?;
l.dataset.fields.get(fi).map(|f| (f.name.clone(), f.ty)).ok_or_else(|| "no field".to_string())
})
.collect::<Result<_, String>>()?;
if fields.len() < 2 {
Expand Down Expand Up @@ -2549,7 +2556,8 @@ fn relate(s: &mut Session, p: &Json) -> CmdResult {
}
let field_of = |layer: &str, key: &str| -> Result<(String, String), String> {
let l = s.map().layer_by_name(layer).filter(|l| !l.is_raster()).ok_or_else(|| format!("no layer '{layer}'"))?;
let f = l.dataset.field_index(key).and_then(|i| l.dataset.fields.get(i)).ok_or_else(|| format!("{layer} has no field '{key}'"))?;
let fi = super::edit::kept_field(&l.dataset, key, "a relationship")?.ok_or_else(|| format!("{layer} has no field '{key}'"))?;
let f = l.dataset.fields.get(fi).ok_or_else(|| format!("{layer} has no field '{key}'"))?;
Ok((l.name.clone(), f.name.clone()))
};
let [origin_name, destination_name] = relationship_names(p)?;
Expand Down Expand Up @@ -2606,9 +2614,17 @@ fn template(s: &mut Session, p: &Json) -> CmdResult {
return if l.design.templates.len() == before { Err(format!("no template '{name}'")) } else { Ok(Json::Null) };
}
let mut defaults = Vec::new();
// A field with a case twin is left out, and said: a default reads its field by name ignoring
// case, so it would fill the first twin. From Selection sends every field of the feature it
// copies, so refusing would refuse the whole template (#1955).
let mut twins = Vec::new();
if let Some(o) = p.get("defaults").and_then(Json::as_object) {
for (k, v) in o {
let fi = l.dataset.field_index(k).ok_or_else(|| format!("no field named '{k}'"))?;
if l.dataset.has_case_twin(k) {
twins.push(k.clone());
continue;
}
let fi = design_field(&l.dataset, k, "a template default")?;
let ty = l.dataset.fields.get(fi).map(|f| f.ty).unwrap_or(FieldType::Text);
let coerced = super::edit::held_default(k, &Value::from_json(v), ty)?;
defaults.push((l.dataset.fields.get(fi).map(|f| f.name.clone()).unwrap_or_default(), coerced));
Expand All @@ -2618,7 +2634,32 @@ fn template(s: &mut Session, p: &Json) -> CmdResult {
let description = super::within("description", "a template's description", ps(p, "description").unwrap_or(""), 500)?;
l.design.templates.retain(|t| !t.name.eq_ignore_ascii_case(&name));
l.design.templates.push(FeatureTemplate { name: name.clone(), description, defaults });
Ok(json!({"template": name, "templates": l.design.templates.len()}))
let (answer, layer) = (json!({"template": name, "templates": l.design.templates.len()}), l.name.clone());
if !twins.is_empty() {
use crate::notice_text::NoticeText;
// The first ones named, the rest counted, as a report's twins are.
const NAMED: usize = 20;
let shown: NoticeText = twins.iter().take(NAMED).map(String::as_str).collect::<Vec<_>>().join(", ").into();
let more = twins.len().saturating_sub(NAMED);
let names = if more > 0 {
NoticeText::format("{names}, and {count} more", [("names", shown), ("count", more.to_string().into())])
} else {
shown
};
let key = if twins.len() == 1 {
"{layer}: {count} field named the same as another but for case is left out of template {template} ({names}): a template default reads its field by name ignoring case, so it would fill the first"
} else {
"{layer}: {count} fields named the same but for case are left out of template {template} ({names}): a template default reads its field by name ignoring case, so it would fill the first"
};
s.notify_text(
NoticeText::format(
key,
[("layer", layer.into()), ("count", twins.len().to_string().into()), ("template", name.into()), ("names", names)],
),
false,
);
}
Ok(answer)
}

/// The nearest vertex, line end or edge of a visible feature layer within the snapping tolerance.
Expand Down
8 changes: 6 additions & 2 deletions crates/engine/src/cmd/diagram.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,13 @@ fn set(s: &mut Session, p: &Value) -> CmdResult {
if l.is_streamed() && matches!(size, Asked::Scaled(..)) {
return Err("a size scaled by the sum needs all of a streamed layer: layer.loadAll first, or a fixed size".into());
}
// Every field must be there, and hold numbers.
// Every field must be there, and hold numbers. A name with a case twin is refused unless the
// diagram already draws it, as set again over its own fields (#1955).
let current: Vec<String> = l.diagram.as_ref().map(|d| d.categories.iter().map(|c| c.field.clone()).collect()).unwrap_or_default();
for (field, _) in &asked {
let fi = l.dataset.field_index(field).ok_or_else(|| format!("no field named '{field}'"))?;
let fi =
if current.contains(field) { l.dataset.field_index(field) } else { super::edit::kept_field(&l.dataset, field, "a diagram")? }
.ok_or_else(|| format!("no field named '{field}'"))?;
if !l.dataset.fields.get(fi).is_some_and(|fd| fd.ty.is_numeric()) {
return Err(format!("'{field}' is not a numeric field"));
}
Expand Down
Loading
Loading