Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
15 changes: 8 additions & 7 deletions crates/engine/src/cmd/layout/legend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,14 +383,15 @@ pub(in crate::cmd::layout) fn legend_map<'m>(input: &'m impl crate::cmd::InputSo
s.project.maps.iter().find(|m| m.name.eq_ignore_ascii_case(map_name)).ok_or_else(|| format!("no map '{map_name}'"))
}

/// The name of the layer `n` (any case, ends trimmed) of a legend's `map`, as the map spells it:
/// one of its layers, or a WMS or WMTS layer's (whose legend is its service's picture). One the
/// map hasn't is an error.
/// The name of the layer `n` (any case) of a legend's `map`, as the map spells it: one of its
/// layers, or a WMS or WMTS layer's (whose legend is its service's picture). `n` is looked up as
/// given first, as a layer's name may begin or end with a space, then with its ends trimmed. One
/// the map hasn't is an error.
pub(in crate::cmd::layout) fn legend_layer<'m>(map: &'m Map, n: &str) -> Result<&'m String, String> {
map.layer_by_name(n.trim())
.map(|l| &l.name)
.or_else(|| map.tiles.iter().find(|t| t.name.eq_ignore_ascii_case(n.trim())).map(|t| &t.name))
.ok_or_else(|| format!("no layer '{n}' in map '{}'", map.name))
let named = |n: &str| {
map.layer_by_name(n).map(|l| &l.name).or_else(|| map.tiles.iter().find(|t| t.name.eq_ignore_ascii_case(n)).map(|t| &t.name))
};
named(n).or_else(|| named(n.trim())).ok_or_else(|| format!("no layer '{n}' in map '{}'", map.name))
}

/// The shape a legend's `labels` takes.
Expand Down
22 changes: 22 additions & 0 deletions crates/engine/src/tests/layout_publication_parts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -574,3 +574,25 @@ fn only_modular_and_poster_put_their_surrounds_on_cards() {
assert_eq!(cards, ["modular", "poster"]);
assert!(!publication_style_cards("unknown"));
}

/// A name is found as it is spelled first, so one with edge spaces is left out, not the layer of
/// the trimmed name (#3234); a name no layer spells that way is still trimmed.
#[test]
fn a_legend_name_is_found_as_spelled_before_it_is_trimmed() {
let mut s = world();
let name = s.map().layers[0].name.clone();
let mut padded = s.map().layers[0].clone();
padded.name = format!(" {name} ");
s.map_mut().add_layer(padded);
s.map_mut().tiles.push(rustgis_map::TileLayer::new(" Parcels", rustgis_map::TileKind::Raster, "https://tiles.invalid/{z}/{x}/{y}.png"));
let excluded = |s: &mut Session, asked: Value| {
publish(s, json!({"contextInLegend": true, "legend": {"exclude": asked}})).unwrap();
match &s.project.layouts.last().unwrap().elements.iter().find(|e| e.name == "Legend").unwrap().kind {
ElementKind::Legend { exclude, .. } => exclude.clone(),
k => panic!("{k:?}"),
}
};
assert_eq!(excluded(&mut s, json!([format!(" {name} "), " Parcels"])), [format!(" {name} "), " Parcels".to_string()]);
assert_eq!(excluded(&mut s, json!([format!(" {name} ")])), std::slice::from_ref(&name));
assert_eq!(excluded(&mut s, json!([name.to_uppercase()])), [name]);
}
Loading
Loading