//! Publish › Report (New Report) and Data, and Publish › Save As (Report //! File, Report Template) and Output (Export Report): reports on a layer's records. use std::collections::{BTreeMap, HashMap}; use std::path::Path; use rustgis_map::Layer; use rustgis_map::report::{Block, Doc, MAX_GROUPS, MAX_ITEMS, Report, ReportChart, Section, Stat, StatKind}; use rustgis_map::symbology::fmt_num; use serde_json::{Value, json}; use super::{b, f, ps}; use crate::notice_text::NoticeText; use crate::{CmdResult, CommandSpec as C, Session}; /// Report files and report templates: JSON with these extensions. pub const REPORT_EXTENSION: &str = "rgisreport"; pub const TEMPLATE_EXTENSION: &str = "rgisrpttemplate"; /// Most records a report lists. pub const MAX_RECORDS: usize = 50_000; /// Most pictures (dynamic pictures and image attachments) a report embeds, and most base64 bytes /// they hold together. const MAX_PICTURES: usize = 500; const PICTURE_BUDGET: usize = if cfg!(target_arch = "wasm32") { 64 << 20 } else { 256 << 20 }; /// Most related reports followed, one inside another. const MAX_DEPTH: usize = 3; /// Most related records listed under one record. const MAX_RELATED: usize = 200; /// Largest PDF a report export holds (less in the browser). const PDF_BUDGET: usize = if cfg!(target_arch = "wasm32") { 256 << 20 } else { 1 << 30 }; fn need_report(s: &Session) -> Option<&'static str> { if s.project.reports.is_empty() { Some("add a report first (Publish › New Report)") } else { None } } pub fn commands() -> Vec { vec![ C::new("report.new", "New Report", "Publish › Report", new_report).inputs(new_inputs).roles(&[]).effects(crate::policy::Effects::NONE).params( r#"{"layer"?: name (default: the selected layer), "name"?, "title"?, "fields"?: [names] (default: all), "where"?: sql, "selectedOnly"?: bool, "groups"?: [fields], "sort"?: field, "descending"?: bool, "page"?: "letter"|"a4"|…, "orientation"?: "portrait"|"landscape"}"#, ), C::new("report.update", "Report Properties", "Report", update).inputs(update_inputs).roles(&[]).effects(crate::policy::Effects::NONE).when(need_report).params( r#"{"report"?: name|index, "name"?, "title"?, "layer"?, "where"?: sql, "selectedOnly"?, "sort"?: field|null, "descending"?, "page"?, "orientation"?, "width"?, "height"?: mm}"#, ), C::new("report.activate", "Open Report", "Browser", |s, p| { let i = index(s, p)?; s.active_report = Some(i); Ok(json!({"report": i})) }).inputs(object_inputs).roles(&[]).effects(crate::policy::Effects::NONE) .view() .when(need_report) .params(r#"{"report": name|index}"#), C::new("report.remove", "Delete Report", "Browser", |s, p| { let i = index(s, p)?; let gone = s.project.reports.remove(i).name; // Other reports' links to it go too. for r in &mut s.project.reports { r.sections.retain(|x| !matches!(x, Section::Related { report } if report.eq_ignore_ascii_case(&gone))); } s.active_report = None; Ok(Value::Null) }).inputs(object_inputs).roles(&[]).effects(crate::policy::Effects::NONE) .when(need_report) .params(r#"{"report"?}"#), // Report sections. C::new("report.supplementalPage", "Supplemental Page", "Publish › Report", |s, p| { let title = super::within("title", "a page's title", ps(p, "title").unwrap_or("About this report"), 500)?; let text = super::within("text", "a page's text", ps(p, "text").unwrap_or(""), 100_000)?; add_section(s, p, Section::Page { title, text, end: b(p, "end").unwrap_or(false) }) }).inputs(object_inputs).roles(&[]).effects(crate::policy::Effects::NONE) .when(need_report) .params(r#"{"report"?, "title"?, "text"?, "end"?: bool (after the records; default before)}"#), C::new("report.newTemplate", "New Template", "Publish › Report", |s, p| { let i = index(s, p)?; let rname = s.project.reports.get(i).map(|r| r.name.clone()).unwrap_or_default(); let before = s.active_layout; let r = super::layout::new_layout( s, &json!({"name": ps(p, "name").map(str::to_string).unwrap_or(format!("{rname} page")), "page": ps(p, "page").unwrap_or("letter"), "orientation": ps(p, "orientation").unwrap_or("portrait"), "template": ps(p, "template").unwrap_or("map")}), )?; s.active_layout = before; let layout = r["name"].as_str().unwrap_or_default().to_string(); add_section(s, &json!({"report": i}), Section::Layout { layout, end: b(p, "end").unwrap_or(false) }) }).inputs(object_inputs).roles(&[]).effects(crate::policy::Effects::NONE) .when(need_report) .params(r#"{"report"?, "name"?, "page"?, "orientation"?, "template"?: "map"|"blank", "end"?: bool}"#), C::new("report.existingTemplate", "Existing Template", "Publish › Report", |s, p| { let i = index(s, p)?; let layout = if let Some(n) = ps(p, "layout") { s.project.layouts.get(template_at(s,n)?).ok_or("no layout")?.name.clone() } else { let text = super::items::read_text(p).map_err(|e| format!("{e} (pass a layout name, or a layout file's path or text)"))?; let before = s.active_layout; let r = super::layout::import(s, &text)?; s.active_layout = before; r["name"].as_str().unwrap_or_default().to_string() }; add_section(s, &json!({"report": i}), Section::Layout { layout, end: b(p, "end").unwrap_or(false) }) }).inputs(template_inputs).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)) .when(need_report) .params(r#"{"report"?, "layout"?: a layout's name, "path"?: "*.rgislayout", "text"?: layout JSON, "data"?: base64 file, "end"?: bool}"#), C::new("report.relatedReport", "Related Report", "Publish › Report", |s, p| { let i = index(s, p)?; let other = ps(p, "related").ok_or("related is required (another report's name)")?; let j = related_report_at(s,other)?; if i == j { return Err("a report can't follow itself".into()); } let report = s.project.reports.get(j).map(|r| r.name.clone()).unwrap_or_default(); add_section(s, &json!({"report": i}), Section::Related { report }) }).inputs(related_report_inputs).roles(&[]).effects(crate::policy::Effects::NONE) .when(need_report) .params(r#"{"report"?, "related": report name}"#), C::new("report.subreport", "Subreport", "Publish › Report", subreport).inputs(subreport_inputs).roles(&[]).effects(crate::policy::Effects::NONE) .when(need_report) .params(r#"{"report"?, "relationship": name (Data Design › Relationships), "fields"?: [related fields]}"#), C::new("report.removeSection", "Remove Section", "Report", |s, p| { let i = index(s, p)?; let k = p.get("index").and_then(Value::as_u64).and_then(|k| usize::try_from(k).ok()).ok_or("index is required")?; let r = s.project.reports.get_mut(i).ok_or("no report")?; if k >= r.sections.len() { return Err(format!("no section {k}")); } r.sections.remove(k); Ok(json!({"sections": r.sections.len()})) }).inputs(object_inputs).roles(&[]).effects(crate::policy::Effects::NONE) .when(need_report) .params(r#"{"report"?, "index": section number}"#), // Data. C::new("report.field", "Field", "Data › Data Design", |s, p| { let remove = b(p, "remove").unwrap_or(false); let (i, field) = if remove { stored(s, p)? } else { field_of(s, p, "field")? }; let r = s.project.reports.get_mut(i).ok_or("no report")?; if !remove && r.fields.len() >= MAX_ITEMS && !r.fields.iter().any(|f| f.eq_ignore_ascii_case(&field)) { return Err(format!("a report lists at most {MAX_ITEMS} fields")); } toggle(&mut r.fields, field, remove, "field")?; Ok(json!({"fields": r.fields})) }).inputs(layer_inputs).roles(&[]).effects(crate::policy::Effects::NONE) .when(need_report) .params(r#"{"report"?, "field": name, "remove"?: bool}"#), C::new("report.autofill", "Autofill Fields", "Data › Data Design", |s, p| { let i = index(s, p)?; let ds = &layer_of(s, i)?.dataset; let FilledIn { names, twins } = filled_in(ds); // The fields a report may take: all but the twins. let all = ds.fields.len() - twins.len(); let full = all > MAX_ITEMS; let r = s.project.reports.get_mut(i).ok_or("no report")?; r.fields = names; let (answer, report) = (json!({"fields": r.fields}), r.name.clone()); // A layer with more fields than a report takes: the first are filled in, and that is // said (#1896), with the count of the layer's fields a report may take (those without // a case twin, which are said apart). if full { let key = if twins.is_empty() { "{report}: the first {count} of the layer's {all} fields are filled in" } else { "{report}: the first {count} of the layer's {all} fields without a case twin are filled in" }; let args = [("report", report.clone().into()), ("count", MAX_ITEMS.to_string().into()), ("all", all.to_string().into())]; s.notify_text(NoticeText::format(key, args), false); } if let Some(note) = twins_left_out(&report, &twins) { s.notify_text(note, false); } Ok(answer) }).inputs(layer_inputs).roles(&[]).effects(crate::policy::Effects::NONE) .when(need_report) .params(r#"{"report"?}"#), C::new("report.statistics", "Statistics", "Data › Data Design", |s, p| { let kind = ps(p, "kind").unwrap_or("sum"); let kind = StatKind::from_code(kind).ok_or_else(|| format!("unknown statistic '{kind}'"))?; let remove = b(p, "remove").unwrap_or(false); let (i, field) = if remove { stored(s, p)? } else { field_of(s, p, "field")? }; let r = s.project.reports.get_mut(i).ok_or("no report")?; // The one named exactly so first, as `toggle` finds a field. let at = r .stats .iter() .position(|x| x.kind == kind && x.field == field) .or_else(|| r.stats.iter().position(|x| x.kind == kind && x.field.eq_ignore_ascii_case(&field))); match (at, remove) { (Some(k), true) => { r.stats.remove(k); } (None, false) if r.stats.len() < MAX_ITEMS => r.stats.push(Stat { field, kind }), (None, false) => return Err("the report has too many statistics".into()), (None, true) => return Err(format!("the report has no {} statistic of '{field}'", kind.label().to_lowercase())), _ => {} } Ok(json!({"stats": r.stats.len()})) }).inputs(layer_inputs).roles(&[]).effects(crate::policy::Effects::NONE) .when(need_report) .params(r#"{"report"?, "field": name, "kind"?: "count"|"sum"|"mean"|"min"|"max", "remove"?: bool}"#), C::new("report.group", "Group", "Data › Data Design", |s, p| { let remove = b(p, "remove").unwrap_or(false); let (i, field) = if remove { stored(s, p)? } else { field_of(s, p, "field")? }; let r = s.project.reports.get_mut(i).ok_or("no report")?; if !remove && r.groups.len() >= MAX_GROUPS && !r.groups.iter().any(|g| g.eq_ignore_ascii_case(&field)) { return Err(format!("reports group by at most {MAX_GROUPS} fields")); } toggle(&mut r.groups, field, remove, "group")?; Ok(json!({"groups": r.groups})) }).inputs(layer_inputs).roles(&[]).effects(crate::policy::Effects::NONE) .when(need_report) .params(r#"{"report"?, "field": name, "remove"?: bool}"#), C::new("report.attachments", "Attachments", "Data › Data Design", |s, p| { let (i, field) = optional_field(s, p, own_attachments)?; let r = s.project.reports.get_mut(i).ok_or("no report")?; r.attachments_field = field; Ok(json!({"field": r.attachments_field})) }).inputs(attachments_inputs).roles(&[]).effects(crate::policy::Effects::NONE) .when(need_report) .params(r#"{"report"?, "field": name of a field listing each record's attachments (paths separated by ';')|null}"#), C::new("report.dynamicPicture", "Dynamic Picture", "Data › Data Design", |s, p| { let (i, field) = optional_field(s, p, own_picture)?; let r = s.project.reports.get_mut(i).ok_or("no report")?; r.picture_field = field; Ok(json!({"field": r.picture_field})) }).inputs(picture_inputs).roles(&[]).effects(crate::policy::Effects::NONE) .when(need_report) .params(r#"{"report"?, "field": name of a field holding a picture path or base64 PNG/JPEG|null}"#), C::new("report.chart", "Chart Frame", "Data › Data Design", |s, p| { let i = index(s, p)?; let chart = match p.get("category") { None | Some(Value::Null) => None, Some(_) => { let (_, category) = own_field_of(s, p, "category", own_category)?; let value = match ps(p, "value") { Some(_) => Some(own_field_of(s, p, "value", own_value)?.1), None => None, }; Some(ReportChart { category, value }) } }; let r = s.project.reports.get_mut(i).ok_or("no report")?; r.chart = chart; Ok(json!({"chart": r.chart.is_some()})) }).inputs(chart_inputs).roles(&[]).effects(crate::policy::Effects::NONE) .when(need_report) .params(r#"{"report"?, "category": field|null (no chart), "value"?: field to sum (default: count records)}"#), // Output. C::new("report.export", "Export Report", "Publish › Output", |s, p| super::exports::write_here(s, p, "report.export")).inputs(export_inputs).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)) .view() .when(need_report) .params(r#"{"report"?, "path": "*.pdf|*.png" (PNG: the first page), "dpi"?: 72..600}"#), C::new("report.save", "Report File", "Publish › Save As", |s, p| save(s, p, false)).inputs(object_inputs).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)) .view() .when(need_report) .params(r#"{"report"?, "path": "*.rgisreport"}"#), C::new("report.saveTemplate", "Report Template", "Publish › Save As", |s, p| save(s, p, true)).inputs(object_inputs).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)) .view() .when(need_report) .params(r#"{"report"?, "path": "*.rgisrpttemplate"}"#), C::new("report.open", "Import Report", "Publish › Report", open).inputs(open_inputs).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)).params( r#"{"path"?: "*.rgisreport|*.rgisrpttemplate", "text"?: report JSON, "data"?: base64 file, "layer"?: the layer a template reports on (default: the selected layer)}"#, ), ] } /// The report named or numbered by `report`, else the active one, else the last one. fn index_input(input: &impl super::InputSource, p: &Value) -> Result { let context = input.input_context(); let s = &context; let rs = &s.project.reports; match p.get("report") { Some(Value::Number(n)) => { n.as_u64().and_then(|n| usize::try_from(n).ok()).filter(|i| *i < rs.len()).ok_or_else(|| format!("no report {n}")) } Some(Value::String(n)) => rs .iter() .position(|r| r.name.eq_ignore_ascii_case(n)) .or_else(|| n.parse::().ok().filter(|i| *i < rs.len())) .ok_or_else(|| format!("no report named '{n}'")), _ => s .active_report .filter(|i| *i < rs.len()) .or(rs.len().checked_sub(1)) .ok_or_else(|| "no reports yet: add one with report.new".to_string()), } } /// A layer by name: the active map's, else any map's. fn find_layer<'a>(input: &'a impl super::InputSource, name: &str) -> Option<&'a Layer> { find_layer_map(input, name).map(|(_, layer)| layer) } /// The layer report `i` lists (the active map's first, else any map's). fn layer_of_input(input: &impl super::InputSource, i: usize) -> Result<&Layer, String> { let context = input.input_context(); let s = &context; let r = s.project.reports.get(i).ok_or("no report")?; find_layer(input, &r.layer).ok_or_else(|| format!("the report's layer '{}' isn't in the project", r.layer)) } /// The report and a field of its layer named by `key` (spelled as the layer spells it). fn field_of(input: &impl super::InputSource, p: &Value, key: &str) -> Result<(usize, String), String> { let i = index_input(input, p)?; let name = ps(p, key).ok_or_else(|| format!("{key} is required"))?; let l = layer_of_input(input, i)?; let fi = super::edit::kept_field(&l.dataset, name, "a report")?.ok_or_else(|| format!("'{}' has no field '{name}'", l.name))?; let field = l.dataset.fields.get(fi).map(|f| f.name.clone()).ok_or("no such field")?; Ok((i, field)) } /// What a report stores already for a command's field ([`own_field_of`]). type Own = fn(&Report) -> Option<&str>; fn own_attachments(r: &Report) -> Option<&str> { r.attachments_field.as_deref() } fn own_picture(r: &Report) -> Option<&str> { r.picture_field.as_deref() } fn own_category(r: &Report) -> Option<&str> { r.chart.as_ref().map(|c| c.category.as_str()) } fn own_value(r: &Report) -> Option<&str> { r.chart.as_ref().and_then(|c| c.value.as_deref()) } /// [`field_of`], except that the name the report stores already for it (`own`), given again as /// stored, is kept as it is: a case twin's name an older project stored can be set again, as /// report.update's sort can; a twin's name given anew is refused (#1955). fn own_field_of(input: &impl super::InputSource, p: &Value, key: &str, own: Own) -> Result<(usize, String), String> { let i = index_input(input, p)?; let name = ps(p, key).ok_or_else(|| format!("{key} is required"))?; let context = input.input_context(); if context.project.reports.get(i).and_then(own) == Some(name) { // Still a field of the layer: only the twin refusal is skipped. let l = layer_of_input(input, i)?; if l.dataset.field_index(name).is_none() { return Err(format!("'{}' has no field '{name}'", l.name)); } return Ok((i, name.to_string())); } field_of(input, p, key) } /// `field` as a field of the report's layer ([`own_field_of`]), or `None` for null/absent. fn optional_field(input: &impl super::InputSource, p: &Value, own: Own) -> Result<(usize, Option), String> { match p.get("field") { None | Some(Value::Null) => Ok((index_input(input, p)?, None)), Some(_) => own_field_of(input, p, "field", own).map(|(i, f)| (i, Some(f))), } } /// A layer's fields to fill a report with ([`filled_in`]). struct FilledIn { /// The first [`MAX_ITEMS`] fields without a case twin. names: Vec, /// The fields with a case twin, all of them, left out as report.field refuses them (#1955). twins: Vec, } /// The most twins a notice names; the rest are counted. const TWINS_NAMED: usize = 20; /// The first [`MAX_ITEMS`] of a layer's fields without a case twin, and every field with one: in /// one pass over the fields to find twins (their names in lower case), and one to pick. fn filled_in(ds: &rustgis_data::Dataset) -> FilledIn { let mut named: HashMap = HashMap::with_capacity(ds.fields.len()); for f in &ds.fields { *named.entry(f.name.to_ascii_lowercase()).or_default() += 1; } let twin = |n: &str| named.get(&n.to_ascii_lowercase()).is_some_and(|c| *c > 1); let (mut names, mut twins) = (Vec::new(), Vec::new()); for f in &ds.fields { if twin(&f.name) { twins.push(f.name.clone()); } else if names.len() < MAX_ITEMS { names.push(f.name.clone()); } } FilledIn { names, twins } } /// The notice for the fields [`filled_in`] left out of report `report`, if any: the first /// [`TWINS_NAMED`] named, the rest counted. fn twins_left_out(report: &str, twins: &[String]) -> Option { (!twins.is_empty()).then(|| { let shown: NoticeText = twins.iter().take(TWINS_NAMED).map(String::as_str).collect::>().join(", ").into(); let more = twins.len().saturating_sub(TWINS_NAMED); let shown = if more > 0 { NoticeText::format("{names}, and {count} more", [("names", shown), ("count", more.to_string().into())]) } else { shown }; NoticeText::format( "{report}: fields named the same but for case are left out ({names}): a report reads its field by name ignoring case, so it would use the first", [("report", report.to_string().into()), ("names", shown)], ) }) } /// The report and the field name given, to remove an entry it stored: matched against what is /// stored, not refused for a case twin, so an entry naming one can still be removed (#1955). fn stored(input: &impl super::InputSource, p: &Value) -> Result<(usize, String), String> { Ok((index_input(input, p)?, ps(p, "field").ok_or("field is required")?.to_string())) } /// Adds `field` to `list`, or removes it: the entry named exactly so, else the first named so /// ignoring case. /// Removing one the list doesn't hold is an error naming it, `what` saying which list. fn toggle(list: &mut Vec, field: String, remove: bool, what: &str) -> Result<(), String> { let at = list.iter().position(|x| *x == field).or_else(|| list.iter().position(|x| x.eq_ignore_ascii_case(&field))); match (at, remove) { (Some(k), true) => { list.remove(k); } (None, true) => return Err(format!("the report has no {what} '{field}'")), (None, false) if list.len() < MAX_ITEMS => list.push(field), _ => {} } Ok(()) } fn add_section(s: &mut Session, p: &Value, sec: Section) -> CmdResult { let i = index(s, p)?; let r = s.project.reports.get_mut(i).ok_or("no report")?; if r.sections.len() >= MAX_ITEMS { return Err("the report has too many sections".into()); } r.sections.push(sec); Ok(json!({"report": i, "sections": r.sections.len()})) } /// Counted changes needed to fit a report to a different layer's schema. #[derive(Default)] struct Fitted { fields: usize, groups: usize, statistics: usize, settings: usize, filter_cleared: bool, columns_replaced: bool, /// The layer's fields, when the default columns are its first `MAX_ITEMS` of more (#1896). layer_fields: usize, subreports: usize, subreport_fields: usize, } impl Fitted { /// Publishes only completed changes, with literal report names and translated operation text. fn notify(self, s: &mut Session, name: &str, when: &'static str) { for (n, key) in [ (self.fields, "{name}: {n} report column(s) missing from the layer removed {when}"), (self.groups, "{name}: {n} report grouping field(s) missing from the layer removed {when}"), (self.statistics, "{name}: {n} report statistic(s) whose fields are missing removed {when}"), (self.settings, "{name}: {n} report sort, picture, attachment or chart setting(s) whose fields are missing removed {when}"), ( usize::from(self.columns_replaced), "{name}: {n} report column set(s) replaced with the layer's default columns because none of the requested columns exist {when}", ), (self.layer_fields, "{name}: the report's columns are the first 64 of the layer's {n} fields {when}"), (self.subreports, "{name}: {n} subreport(s) whose relationship no longer involves the layer removed {when}"), (self.subreport_fields, "{name}: {n} subreport field(s) missing from the related layer removed {when}"), ] { if n > 0 { s.notify_text( NoticeText::format( key, [("name", name.to_string().into()), ("n", n.to_string().into()), ("when", NoticeText::term(when))], ), false, ); } } if self.filter_cleared { s.notify_text(NoticeText::format( "{name}: 1 report filter that could not be read for its layer removed {when}; that filter no longer limits included records", [("name", name.to_string().into()), ("when", NoticeText::term(when))], ), false); } } } /// Drops what a layer lacks so the report draws on that layer, returning every removed binding /// and whether missing columns were replaced with the default field list. fn fit_to_layer(r: &mut Report, ds: &rustgis_data::Dataset) -> Fitted { let has = |n: &str| ds.field_index(n).is_some(); let mut fitted = Fitted { fields: r.fields.len(), groups: r.groups.len(), statistics: r.stats.len(), ..Fitted::default() }; let settings = usize::from(r.sort.is_some()) + usize::from(r.picture_field.is_some()) + usize::from(r.attachments_field.is_some()) + usize::from(r.chart.is_some()); r.fields.retain(|f| has(f)); r.groups.retain(|f| has(f)); r.stats.retain(|x| has(&x.field)); fitted.fields -= r.fields.len(); fitted.groups -= r.groups.len(); fitted.statistics -= r.stats.len(); r.sort = r.sort.take().filter(|f| has(f)); r.picture_field = r.picture_field.take().filter(|f| has(f)); r.attachments_field = r.attachments_field.take().filter(|f| has(f)); r.chart = r.chart.take().filter(|c| has(&c.category) && c.value.as_deref().is_none_or(has)); fitted.settings = settings - usize::from(r.sort.is_some()) - usize::from(r.picture_field.is_some()) - usize::from(r.attachments_field.is_some()) - usize::from(r.chart.is_some()); if r.fields.is_empty() { fitted.columns_replaced = fitted.fields > 0; fitted.layer_fields = if ds.fields.len() > MAX_ITEMS { ds.fields.len() } else { 0 }; r.fields = ds.fields.iter().take(MAX_ITEMS).map(|f| f.name.clone()).collect(); } fitted.filter_cleared = !r.where_clause.is_empty() && rustgis_data::query::parse_fields(&r.where_clause, ds).is_err(); if fitted.filter_cleared { r.where_clause.clear(); } fitted } /// A join key: numbers by value (so 1 and 1.0 match), anything else as text; empty for none. fn join_key(ds: &rustgis_data::Dataset, row: usize, fi: Option) -> String { match fi.and_then(|fi| ds.value(row, fi).as_f64()) { Some(x) => format!("{x}"), None => cell(ds, row, fi), } } /// A report's name told apart by a number (see [`super::numbered_saying`]), with the notice to /// give when the number cuts it. `whose` says where the name came from, as that notice puts it. fn unique_name(s: &Session, command: &str, whose: &str, base: &str, skip: Option) -> (String, Option) { // A name given was already checked by the command. A file's name was held to 200 characters // when the report was read, and that is said then. A name made from a layer's name // ("{layer} Report") may be longer and keeps its first 200 characters; `new_report` says so. let base: String = base.trim().chars().take(200).collect(); let base = if base.is_empty() { "Report".to_string() } else { base }; let taken = |n: &str| s.project.reports.iter().enumerate().any(|(j, r)| Some(j) != skip && r.name.eq_ignore_ascii_case(n)); super::numbered_saying(command, "a report", whose, &base, 200, taken) } /// Page size from `page`/`orientation`/`width`/`height`, portrait by default. fn page(p: &Value, current: Option<(f64, f64)>) -> Result, String> { if ["page", "orientation", "width", "height"].iter().all(|k| p.get(k).is_none()) { return Ok(None); } let mut q = p.clone(); if ps(p, "orientation").is_none() && ps(p, "page").is_some() { q["orientation"] = json!("portrait"); } super::layout::page_size(&q, current).map(Some) } fn new_report(s: &mut Session, p: &Value) -> CmdResult { let given = ps(p, "name").map(|n| super::within("name", "a report's name", n.trim(), 200)).transpose()?; let title = ps(p, "title").map(|t| super::within("title", "a report's title", t, 500)).transpose()?; let layer = new_layer(s, p)?; if layer.is_raster() { return Err("reports list a feature layer's records, not a raster's".into()); } let made = format!("{} Report", layer.name); let whose = if given.is_some() { "the name given" } else { "the name made from the layer's name" }; let (name, mut cut) = unique_name(s, "New Report", whose, given.as_deref().unwrap_or(&made), None); // A name made from a long layer name keeps its first 200 characters. One notice says how many // are kept, with the number when one follows, so two notices don't give two different counts. if given.is_none() && made.trim().chars().count() > 200 { cut = Some(match name.rsplit_once(' ').filter(|_| cut.is_some()) { Some((kept, number)) => format!( "New Report: the report's name is made from the layer's name and keeps its first {} characters before \" {number}\"", kept.chars().count() ), None => "New Report: the report's name is made from the layer's name and keeps its first 200 characters".to_string(), }); } let mut r = Report { name, layer: layer.name.clone(), ..Report::default() }; // A title made from a long layer name keeps its first 500 characters, as a title given must. let title_cut = title.is_none() && layer.name.chars().count() > 500; r.title = title.unwrap_or_else(|| layer.name.chars().take(500).collect()); let ds = &layer.dataset; // A list given is refused past its limit (#1896), not cut. let fields_of = |key: &str, of: &str, limit: usize| -> Result, String> { let given = p.get(key).and_then(Value::as_array).map_or(&[][..], Vec::as_slice); if given.len() > limit { return Err(format!("{key} has {} entries: {of} at most {limit}", given.len())); } let mut out = Vec::new(); for v in given { let n = v.as_str().ok_or_else(|| format!("{key} are field names"))?; // As report.field takes one: a twin's name is refused (#1955). let fi = super::edit::kept_field(ds, n, "a report")?.ok_or_else(|| format!("'{}' has no field '{n}'", layer.name))?; out.extend(ds.fields.get(fi).map(|f| f.name.clone())); } Ok(out) }; r.fields = fields_of("fields", "a report lists", MAX_ITEMS)?; // The layer's fields when none are given: the first of a wide layer, and that is said. let mut twins = Vec::new(); let mut all = ds.fields.len(); let first_of = r.fields.is_empty() && { let filled = filled_in(ds); twins = filled.twins; r.fields = filled.names; // The fields a report may take: all but the twins. all -= twins.len(); all > MAX_ITEMS }; r.groups = fields_of("groups", "a report groups by", MAX_GROUPS)?; if let Some(w) = ps(p, "where").map(str::trim).filter(|w| !w.is_empty()) { rustgis_data::query::parse(w, ds)?; r.where_clause = w.to_string(); } r.selected_only = b(p, "selectedOnly").unwrap_or(false); if let Some(sort) = ps(p, "sort") { let fi = super::edit::kept_field(ds, sort, "a report")?.ok_or_else(|| format!("'{}' has no field '{sort}'", layer.name))?; r.sort = ds.fields.get(fi).map(|f| f.name.clone()); } r.descending = b(p, "descending").unwrap_or(false); if let Some((w, h)) = page(p, None)? { (r.width, r.height) = (w, h); } r.clamp(); let name = r.name.clone(); if s.project.reports.len() >= crate::project::MAX_ITEMS { return Err(format!("a project holds at most {} reports", crate::project::MAX_ITEMS)); } s.project.reports.push(r); let i = s.project.reports.len() - 1; s.active_report = Some(i); if first_of { // Counting the fields a report may take: those without a case twin, said apart. let key = if twins.is_empty() { "{report}: the report lists the first {count} of the layer's {all} fields" } else { "{report}: the report lists the first {count} of the layer's {all} fields without a case twin" }; let args = [("report", name.clone().into()), ("count", MAX_ITEMS.to_string().into()), ("all", all.to_string().into())]; s.notify_text(NoticeText::format(key, args), false); } if let Some(note) = twins_left_out(&name, &twins) { s.notify_text(note, false); } if let Some(cut) = cut { s.notify(cut, false); } if title_cut { s.notify("New Report: the report's title is the layer's name and keeps its first 500 characters".to_string(), false); } Ok(json!({"report": i, "name": name})) } fn update(s: &mut Session, p: &Value) -> CmdResult { let i = index(s, p)?; let new_name = ps(p, "name").map(|n| super::within("name", "a report's name", n.trim(), 200)).transpose()?; let new_title = ps(p, "title").map(|t| super::within("title", "a report's title", t, 500)).transpose()?; let mut r = s.project.reports.get(i).cloned().ok_or("no report")?; let old_name = r.name.clone(); let mut renamed = None; let mut fitted = Fitted::default(); if let Some(n) = ps(p, "layer") { let l = find_layer(s, n).ok_or_else(|| format!("no layer named '{n}'"))?; if l.is_raster() { return Err("reports list a feature layer's records, not a raster's".into()); } r.layer = l.name.clone(); // What the new layer lacks is dropped, so the report doesn't show blank columns, and so // are subreports through relationships that don't involve it. A kept subreport may now // list the other end of its relationship, so its fields are fitted to that layer. fitted = fit_to_layer(&mut r, &l.dataset); let name = l.name.clone(); r.sections.retain_mut(|x| match x { Section::Subreport { relationship, fields } => { let other = update_other(s, relationship, &name); let Some(other) = other else { fitted.subreports += 1; return false; }; let before = fields.len(); match find_layer(s, &other) { Some(o) => fields.retain(|f| o.dataset.field_index(f).is_some()), None => fields.clear(), } fitted.subreport_fields += before - fields.len(); true } _ => true, }); } // Everything is checked against the (possibly new) layer before the report changes. let l = find_layer(s, &r.layer); // A field named anew: a case twin's name is refused, as a report reads the first twin (#1955). let field = |n: &str| -> Result { let ds = &l.ok_or("the report's layer isn't in the project")?.dataset; super::edit::kept_field(ds, n, "a report")? .and_then(|fi| ds.fields.get(fi)) .map(|f| f.name.clone()) .ok_or_else(|| format!("no field '{n}'")) }; let mut cut = None; if let Some(n) = new_name { (r.name, cut) = unique_name(s, "Report Properties", "the name given", &n, Some(i)); renamed = Some(r.name.clone()); } if let Some(t) = new_title { r.title = t; } if let Some(w) = ps(p, "where") { let w = w.trim(); if !w.is_empty() { rustgis_data::query::parse(w, &l.ok_or("the report's layer isn't in the project")?.dataset)?; } r.where_clause = w.to_string(); } if let Some(v) = b(p, "selectedOnly") { r.selected_only = v; } match p.get("sort") { Some(Value::Null) => r.sort = None, // Its own sort set again, as stored, stays, a field of the layer still: only the twin // refusal is skipped. // (Without its layer, as before, it is left as it is.) Some(Value::String(n)) if r.sort.as_deref() == Some(n.as_str()) => { if let Some(layer) = l && layer.dataset.field_index(n).is_none() { return Err(format!("no field '{n}'")); } } Some(Value::String(n)) => r.sort = Some(field(n)?), _ => {} } if let Some(v) = b(p, "descending") { r.descending = v; } if let Some((w, h)) = page(p, Some((r.width, r.height)))? { (r.width, r.height) = (w, h); } r.clamp(); let name = r.name.clone(); if let Some(slot) = s.project.reports.get_mut(i) { *slot = r; } // Related-report links follow a rename. if let Some(new) = renamed { for x in s.project.reports.iter_mut().flat_map(|r| r.sections.iter_mut()) { if let Section::Related { report } = x && report.eq_ignore_ascii_case(&old_name) { *report = new.clone(); } } } if ps(p, "where").is_some() { fitted.filter_cleared = false; } fitted.notify(s, &name, "while changing the report layer"); if let Some(cut) = cut { s.notify(cut, false); } Ok(json!({"report": i, "name": name})) } fn subreport(s: &mut Session, p: &Value) -> CmdResult { let i = index(s, p)?; let rel = ps(p, "relationship").ok_or("relationship is required")?; let layer = s.project.reports.get(i).map(|r| r.layer.clone()).unwrap_or_default(); let r = relationships(s).find(|r| r.name.eq_ignore_ascii_case(rel)).ok_or_else(|| format!("no relationship '{rel}'"))?; let other = opposite(r, &layer).ok_or_else(|| format!("the relationship '{}' doesn't involve '{layer}'", r.name))?.to_string(); let name = r.name.clone(); let ds = &find_layer(s, &other).ok_or_else(|| format!("no layer named '{other}'"))?.dataset; let given = p.get("fields").and_then(Value::as_array).map_or(&[][..], Vec::as_slice); if given.len() > MAX_ITEMS { return Err(format!("fields has {} entries: a subreport lists at most {MAX_ITEMS}", given.len())); } let mut fields = Vec::new(); for v in given { let n = v.as_str().ok_or("fields are field names")?; let fi = super::edit::kept_field(ds, n, "a subreport")?.ok_or_else(|| format!("'{other}' has no field '{n}'"))?; fields.extend(ds.fields.get(fi).map(|f| f.name.clone())); } add_section(s, &json!({"report": i}), Section::Subreport { relationship: name, fields }) } /// Every map's relationships. fn relationships(input: &impl super::InputSource) -> impl Iterator { let context = input.input_context(); let s = &context; s.project.maps.iter().flat_map(|m| m.editing.relationships.iter()) } /// `report.save`, or with `template` `report.saveTemplate`: the report's file at its path. fn save(s: &mut Session, p: &Value, template: bool) -> CmdResult { let command = if template { "report.saveTemplate" } else { "report.save" }; super::exports::file_command(s, command, p, |s, out| { let i = index(s, p)?; Ok(super::exports::Made::new(report_within(s, i, template, out)?, json!({"path": out.path}))) }) } /// A report file (the report as it is) or template (its design without the data source). pub fn report_file(s: &Session, i: usize, template: bool) -> Result, String> { report_within(s, i, template, super::exports::Output::UNLIMITED) } /// [`report_file`] for the file `out` describes. pub(super) fn report_within(s: &Session, i: usize, template: bool, out: super::exports::Output<'_>) -> Result, String> { let r = s.project.reports.get(i).ok_or("no report")?; let r = if template { r.template() } else { r.clone() }; out.json_pretty(&r) } fn open(s: &mut Session, p: &Value) -> CmdResult { let text = super::items::read_text(p)?; crate::project::check_format(&text, "report")?; let mut r: Report = serde_json::from_str(&text).map_err(|e| format!("not a RustGIS report: {e}"))?; let repairs = r.clamp(); import_layer(s, p, &mut r)?; // Drop what the layer lacks, so the report still draws. let fitted = find_layer(s, &r.layer).map(|l| fit_to_layer(&mut r, &l.dataset)).unwrap_or_default(); let (name, cut) = unique_name(s, "Import Report", "the name from the file", &r.name, None); r.name = name; let name = r.name.clone(); if s.project.reports.len() >= crate::project::MAX_ITEMS { return Err(format!("a project holds at most {} reports", crate::project::MAX_ITEMS)); } s.project.reports.push(r); let i = s.project.reports.len() - 1; s.active_report = Some(i); for notice in crate::project::content_notices(repairs, &name) { s.notify_text(notice, false); } fitted.notify(s, &name, "while importing the report"); if let Some(cut) = cut { s.notify(cut, false); } Ok(json!({"report": i, "name": name})) } /// A record's value as report text (nulls blank). fn cell(ds: &rustgis_data::Dataset, row: usize, fi: Option) -> String { match fi.map(|fi| ds.value(row, fi)) { None | Some(rustgis_data::Value::Null) => String::new(), Some(v) => v.to_string(), } } /// Statistic lines for some records. fn stat_lines(r: &Report, ds: &rustgis_data::Dataset, rows: &[usize]) -> Vec { let mut out = vec![format!("{} records", rows.len())]; for st in &r.stats { let fi = ds.field_index(&st.field); let vals: Vec = rows.iter().filter_map(|&row| fi.and_then(|fi| ds.value(row, fi).as_f64())).collect(); let v = st.kind.apply(&vals, rows.len()).map(fmt_num).unwrap_or_else(|| "–".into()); out.push(format!("{} of {}: {v}", st.kind.label(), st.field)); } out } /// A picture field's value as base64 and pixel size: base64 image data, or the path of a PNG or /// JPEG relative to the project's folder. Attribute values come from data files, so paths may /// not be absolute or climb out of that folder (with `..`), and an unsaved project reads none. fn picture_of(text: &str, folder: Option<&Path>) -> Option<(String, u32, u32)> { let t = text.trim(); if t.is_empty() { return None; } let lower = t.to_ascii_lowercase(); let is_path = [".png", ".jpg", ".jpeg"].iter().any(|e| lower.ends_with(e)); let b64 = if is_path { let rel = Path::new(t); let safe = rel.components().all(|c| matches!(c, std::path::Component::Normal(_) | std::path::Component::CurDir)); // `folder` is canonical already; the picture is resolved through links too, so a link // inside the folder can't point outside it. let folder = folder.filter(|_| safe)?; let full = folder.join(rel).canonicalize().ok()?; if !full.starts_with(folder) { return None; } super::graphics::picture(&json!({"path": full.to_str()?})).ok()? } else { super::graphics::picture(&json!({"image": t})).ok()? }; let (w, h) = rustgis_render::picture_size(&b64)?; Some((b64, w, h)) } /// Turns a report into blocks for the renderer. pub fn build(s: &Session, i: usize) -> Result { let r = s.project.reports.get(i).ok_or("no report")?; let mut doc = Doc { title: if r.title.is_empty() { r.name.clone() } else { r.title.clone() }, width: r.width, height: r.height, blocks: Vec::new(), }; let mut seen = vec![i]; blocks(s, i, &mut doc.blocks, &mut seen, 0)?; Ok(doc) } fn blocks(s: &Session, i: usize, out: &mut Vec, seen: &mut Vec, depth: usize) -> Result<(), String> { let r = s.project.reports.get(i).ok_or("no report")?; let layer = layer_of(s, i)?; let ds = &layer.dataset; // Picture paths are read relative to the project's folder (none for an unsaved project). // Canonical once, for checking each picture's resolved path against it. let folder_buf = s.path.as_deref().and_then(Path::parent).and_then(|f| f.canonicalize().ok()); let folder = folder_buf.as_deref(); let layout_index = |name: &str| s.project.layouts.iter().position(|l| l.name.eq_ignore_ascii_case(name)); // Pages before the records. for sec in &r.sections { match sec { Section::Page { title, text, end: false } => { out.push(Block::Heading { level: 0, text: title.clone() }); out.push(Block::Text(text.clone())); out.push(Block::PageBreak); } Section::Layout { layout, end: false } => out.extend(layout_index(layout).map(Block::Layout)), _ => {} } } // The records: matching, selected, sorted by group then sort field. let mut rows: Vec = if r.where_clause.trim().is_empty() { (0..ds.features.len()).collect() } else { rustgis_data::query::select(&r.where_clause, ds)? }; if r.selected_only { let sel: std::collections::HashSet = layer.selection.iter().copied().collect(); rows.retain(|row| sel.contains(row)); } let gi: Vec> = r.groups.iter().map(|g| ds.field_index(g)).collect(); let si = r.sort.as_deref().and_then(|n| ds.field_index(n)); // Sort keys worked out once per record: a total order even for columns mixing numbers and // text (numbers first, by value; then everything else as text). enum Key { Num(f64), Text(String), } let key = |row: usize, fi: Option| -> Key { let v = fi.map(|fi| ds.value(row, fi)); match v.and_then(rustgis_data::Value::as_f64) { Some(x) => Key::Num(x), None => Key::Text(v.map(ToString::to_string).unwrap_or_default()), } }; let cmp = |a: &Key, b: &Key| match (a, b) { (Key::Num(x), Key::Num(y)) => x.total_cmp(y), (Key::Num(_), Key::Text(_)) => std::cmp::Ordering::Less, (Key::Text(_), Key::Num(_)) => std::cmp::Ordering::Greater, (Key::Text(x), Key::Text(y)) => x.cmp(y), }; let mut keyed: Vec<(Vec, Option, usize)> = rows.iter().map(|&row| (gi.iter().map(|g| key(row, *g)).collect(), si.map(|s| key(row, Some(s))), row)).collect(); keyed.sort_by(|a, b| { for (x, y) in a.0.iter().zip(&b.0) { let o = cmp(x, y); if o != std::cmp::Ordering::Equal { return o; } } match (&a.1, &b.1) { (Some(x), Some(y)) => { let o = cmp(x, y); if r.descending { o.reverse() } else { o } } _ => std::cmp::Ordering::Equal, } }); // Sorted first, so a capped report lists the first records in its order. let truncated = keyed.len() > MAX_RECORDS; let rows: Vec = keyed.into_iter().take(MAX_RECORDS).map(|k| k.2).collect(); let title = if r.title.is_empty() { r.name.clone() } else { r.title.clone() }; out.push(Block::Title(title)); let mut about = format!("{} records from {}", rows.len(), layer.name); if !r.where_clause.trim().is_empty() { about.push_str(&format!(" where {}", r.where_clause.trim())); } if r.selected_only { about.push_str(" (selected)"); } if truncated { about.push_str(&format!(" — the first {MAX_RECORDS} are listed")); } // Only a preview draws a layer read in place (an export reads it whole first), here or // under a record through a relationship. let related_in_view = r.sections.iter().any(|sec| { let Section::Subreport { relationship, .. } = sec else { return false }; relationships(s) .find(|x| x.name.eq_ignore_ascii_case(relationship)) .and_then(|rel| opposite(rel, &layer.name)) .and_then(|other| find_layer(s, other)) .is_some_and(Layer::is_streamed) }); if layer.is_streamed() || related_in_view { about.push_str(" — in this preview, only the records in the map's view; the export lists them all"); } out.push(Block::Text(about)); let fi: Vec> = r.fields.iter().map(|n| ds.field_index(n)).collect(); let columns = Block::Columns(r.fields.clone()); let pic = r.picture_field.as_deref().and_then(|n| ds.field_index(n)); let att = r.attachments_field.as_deref().and_then(|n| ds.field_index(n)); let mut pictures = 0usize; let mut picture_bytes = 0usize; // Subreports: related records per key value. let subs: Vec<(String, Vec, Vec>, &rustgis_data::Dataset, HashMap>, Option)> = r .sections .iter() .filter_map(|sec| { let Section::Subreport { relationship, fields } = sec else { return None }; let rel = relationships(s).find(|x| x.name.eq_ignore_ascii_case(relationship))?; // A relationship that doesn't involve this layer is skipped. let (here_key, other, other_key) = relationship_columns(rel, &layer.name)?; let ods = &find_layer(s, other)?.dataset; let ok = ods.field_index(other_key)?; let mut by_key: HashMap> = HashMap::new(); for row in 0..ods.features.len() { let k = join_key(ods, row, Some(ok)); if !k.is_empty() { by_key.entry(k).or_default().push(row); } } let names: Vec = if fields.is_empty() { ods.fields.iter().take(6).map(|f| f.name.clone()).collect() } else { fields.clone() }; let idx = names.iter().map(|n| ods.field_index(n)).collect(); Some((rel.name.clone(), names, idx, ods, by_key, ds.field_index(here_key))) }) .collect(); let mut open: Vec = Vec::new(); // the group values in force, outermost first let mut group_rows: Vec> = vec![Vec::new(); gi.len()]; let close = |out: &mut Vec, level: usize, open: &mut Vec, group_rows: &mut Vec>| { for l in (level..open.len()).rev() { if let Some(g) = group_rows.get_mut(l) { if !g.is_empty() { out.push(Block::Stats(stat_lines(r, ds, g))); } g.clear(); } } open.truncate(level); }; if gi.is_empty() { out.push(columns.clone()); } for &row in &rows { // Group values compare as the sort does: numbers by value (1 and 1.0 are one group). let norm = |g: Option| match g.and_then(|g| ds.value(row, g).as_f64()) { Some(x) => format!("{x}"), None => cell(ds, row, g), }; let vals: Vec = gi.iter().map(|g| norm(*g)).collect(); if let Some(changed) = (0..vals.len()).find(|&l| open.get(l) != vals.get(l)) { close(out, changed, &mut open, &mut group_rows); for (l, v) in vals.iter().enumerate().skip(changed) { let label = r.groups.get(l).cloned().unwrap_or_default(); out.push(Block::Heading { level: l.min(255) as u8, text: format!("{label}: {}", if v.is_empty() { "(blank)" } else { v }), }); open.push(v.clone()); } out.push(columns.clone()); } for g in group_rows.iter_mut() { g.push(row); } out.push(Block::Row(fi.iter().map(|f| cell(ds, row, *f)).collect())); // Once the picture budget is spent, no more picture files are read. if let Some(pf) = pic && pictures < MAX_PICTURES && picture_bytes < PICTURE_BUDGET && let Some((image, width, height)) = picture_of(&cell(ds, row, Some(pf)), folder) && picture_bytes.saturating_add(image.len()) <= PICTURE_BUDGET { pictures += 1; picture_bytes += image.len(); out.push(Block::Picture { image, width, height }); } if let Some(af) = att { let items: Vec = cell(ds, row, Some(af)).split(';').map(str::trim).filter(|x| !x.is_empty()).take(50).map(str::to_string).collect(); if !items.is_empty() { let names = items .iter() .map(|x| Path::new(x).file_name().map_or_else(|| x.clone(), |n| n.to_string_lossy().into_owned())) .collect(); out.push(Block::Attachments(names)); for x in &items { if pictures >= MAX_PICTURES || picture_bytes >= PICTURE_BUDGET { break; } if let Some((image, width, height)) = picture_of(x, folder) && picture_bytes.saturating_add(image.len()) <= PICTURE_BUDGET { pictures += 1; picture_bytes += image.len(); out.push(Block::Picture { image, width, height }); } } } } for (name, names, idx, ods, by_key, key) in &subs { let k = join_key(ds, row, *key); let Some(related) = by_key.get(&k).filter(|_| !k.is_empty()) else { continue }; out.push(Block::SubColumns( names.iter().map(|n| format!("{name}: {n}")).take(1).chain(names.iter().skip(1).cloned()).collect(), )); for &orow in related.iter().take(MAX_RELATED) { out.push(Block::SubRow(idx.iter().map(|f| cell(ods, orow, *f)).collect())); } } } close(out, 0, &mut open, &mut group_rows); // The report's summary and chart. out.push(Block::Heading { level: 0, text: "Summary".into() }); out.push(Block::Stats(stat_lines(r, ds, &rows))); if let Some(c) = &r.chart && let Some(ci) = ds.field_index(&c.category) { let vi = c.value.as_deref().and_then(|v| ds.field_index(v)); let mut sums: BTreeMap = BTreeMap::new(); for &row in &rows { let k = cell(ds, row, Some(ci)); let v = match vi { Some(vi) => ds.value(row, vi).as_f64().unwrap_or(0.0), None => 1.0, }; *sums.entry(if k.is_empty() { "(blank)".into() } else { k }).or_default() += v; } let mut bars: Vec<(String, f64)> = sums.into_iter().collect(); bars.sort_by(|a, b| b.1.total_cmp(&a.1)); bars.truncate(25); let title = match &c.value { Some(v) => format!("{v} by {}", c.category), None => format!("Records by {}", c.category), }; out.push(Block::Chart { title, bars }); } // Pages after the records, then related reports. for sec in &r.sections { match sec { Section::Page { title, text, end: true } => { out.push(Block::PageBreak); out.push(Block::Heading { level: 0, text: title.clone() }); out.push(Block::Text(text.clone())); } Section::Layout { layout, end: true } => out.extend(layout_index(layout).map(Block::Layout)), _ => {} } } for sec in &r.sections { if let Section::Related { report } = sec && depth < MAX_DEPTH && let Some(j) = s.project.reports.iter().position(|x| x.name.eq_ignore_ascii_case(report)) && !seen.contains(&j) { seen.push(j); out.push(Block::PageBreak); // A related report that can't be built (its layer gone, its where clause stale) is // noted in place rather than failing this one. let mut sub = Vec::new(); match blocks(s, j, &mut sub, seen, depth + 1) { Ok(()) => out.extend(sub), Err(e) => out.push(Block::Text(format!("The related report {report} couldn't be included: {e}"))), } } } Ok(()) } /// The report's pages, laid out: for the preview, from the features the layers hold now (a layer /// read in place holds those of the view, and the report says so); an export reads them whole /// ([`report_bytes`]). pub fn pages(s: &Session, i: usize) -> Result<(Doc, Vec), String> { let doc = build(s, i)?; let pages = rustgis_render::report::paginate(&doc); Ok((doc, pages)) } /// The report as a PDF (every page) or a PNG (the first page), and its page count: every record of /// a layer read in place, read whole on a copy of the project ([`whole_copy`]); what reading and /// drawing it said is said in `s`. The browser's export, where the whole read runs here; the /// app's runs on a thread of its own ([`export_work`]). pub fn report_bytes(s: &mut Session, i: usize, dpi: f64, pdf: bool) -> Result<(Vec, usize), String> { let name = s.project.reports.get(i).map(|r| r.name.clone()).unwrap_or_default(); let (bytes, mut copy) = whole_bytes(detached(&s.project), s.path.clone(), i, dpi, pdf, None); super::exports::say_coded(s, super::exports::copy_notices_coded(&mut copy, &name)); bytes } /// What Export Report reads of the session (a copy of the project, its layers' reads left with /// the live ones), and the work that reads the report's layers whole, draws it and writes it. pub(crate) fn export_work(s: &Session, p: &Value) -> Result { let path = ps(p, "path").ok_or("path is required")?.to_string(); let i = index(s, p)?; let dpi = f(p, "dpi").unwrap_or(150.0).clamp(72.0, 600.0); let lower = path.to_ascii_lowercase(); let pdf = lower.ends_with(".pdf"); if !pdf && !lower.ends_with(".png") { return Err("export to a .pdf or .png file".into()); } let (project, folder) = (detached(&s.project), s.path.clone()); let download = s.download_room()?; Ok(Box::new(move || { let (bytes, mut copy) = whole_bytes(project, folder, i, dpi, pdf, download.map(|room| (room, path.as_str()))); let mut held = None; let written = bytes.and_then(|(bytes, pages)| { rustgis_data::progress::check().map_err(|e| e.to_string())?; let len = bytes.len(); held = super::exports::place(download.is_some(), &path, bytes)?; Ok(json!({"path": path, "pages": pages, "bytes": len})) }); // The layers it lists are read whole; a layout drawn in it draws the others' views, and // one whose source fails there is said, as Export Layout says it. // What reading the layers lost keeps its code: said with the export's notes. let wrote = super::exports::drawn_on(&mut copy, written, path)?; Ok(super::exports::Wrote { download: held, ..wrote }) })) } /// `project`, its streamed layers' reads under way left with the live layers. fn detached(project: &crate::project::Project) -> crate::project::Project { let mut project = project.clone(); for m in &mut project.maps { m.detach_reads(); } project } /// Report `i` drawn ([`drawn_bytes`]) from a copy of `project` saved at `path` (pictures are found /// beside it), its layers read whole ([`whole_copy`]), with the copy, which holds what reading and /// drawing it said. fn whole_bytes( project: crate::project::Project, path: Option, i: usize, dpi: f64, pdf: bool, download: Option<(super::exports::Room, &str)>, ) -> (Result<(Vec, usize), String>, Session) { let mut copy = Session::new(project); copy.path = path; let bytes = whole_copy(&mut copy, i).and_then(|()| drawn_bytes(&mut copy, i, dpi, pdf, download)); (bytes, copy) } /// Reads whole, in `copy` (a copy of the project to draw report `i` from), the layers read in /// place that the report lists records of ([`reported_layers`]), so the report lists every /// record of their sources and not only those of the view, their selections kept; the rows a /// source couldn't read are said on the copy. Each read looks at Cancel as Load All Features' /// does: between the chunks of a source read so, not during one read at once. pub(crate) fn whole_copy(copy: &mut Session, i: usize) -> Result<(), String> { for (m, id) in reported_layers(copy, i) { // Cancelled after one layer's read: the next isn't started. rustgis_data::progress::check().map_err(|e| e.to_string())?; let Some(l) = copy.project.maps.get_mut(m).and_then(|map| map.layer_mut(id)) else { continue }; let unread = super::whole::load_into(l)?; unread.say(copy); } rustgis_data::progress::check().map_err(|e| e.to_string()) } /// The layers report `i` lists records of, by map and id: its own, its subreports' related /// layers, and those of the reports it appends, as far as [`blocks`] draws them (the same /// reports, in the same order, to the same depth), so an export reads no layer it doesn't list. pub(crate) fn reported_layers(input: &impl super::InputSource, i: usize) -> Vec<(usize, u32)> { /// Report `i`'s layers at `depth`, then those of the reports it appends, as `blocks` walks /// them (sharing its `seen`). fn walk(input: &impl super::InputSource, i: usize, depth: usize, seen: &mut Vec, out: &mut Vec<(usize, u32)>) { let context = input.input_context(); let s = &context; let place = |name: &str| { let (map, l) = find_layer_map(input, name)?; Some((s.project.maps.iter().position(|m| std::ptr::eq(m, map))?, l.id, l.name.clone())) }; let Some(r) = s.project.reports.get(i) else { return }; // A report whose layer is gone draws nothing (`blocks` fails), nor the reports it appends. let Some((m, id, name)) = place(&r.layer) else { return }; out.push((m, id)); for sec in &r.sections { if let Section::Subreport { relationship, .. } = sec { let other = relationships(input) .find(|x| x.name.eq_ignore_ascii_case(relationship)) .and_then(|rel| opposite(rel, &name).map(str::to_string)); out.extend(other.and_then(|other| place(&other)).map(|(m, id, _)| (m, id))); } } for sec in &r.sections { if let Section::Related { report } = sec && depth < MAX_DEPTH && let Some(j) = s.project.reports.iter().position(|x| x.name.eq_ignore_ascii_case(report)) && !seen.contains(&j) { seen.push(j); walk(input, j, depth + 1, seen, out); } } } let mut out = Vec::new(); walk(input, i, 0, &mut vec![i], &mut out); out.sort_unstable(); out.dedup(); out } /// The report as a PDF (every page) or a PNG (the first page), and its page count, drawn from /// the features `s`'s layers hold. fn drawn_bytes( s: &mut Session, i: usize, dpi: f64, pdf: bool, download: Option<(super::exports::Room, &str)>, ) -> Result<(Vec, usize), String> { let (doc, pages) = pages(s, i)?; let revision = |s: &Session| { pages .iter() .take(if pdf { usize::MAX } else { 1 }) .filter_map(|page| { let rustgis_render::report::Page::Layout(li) = page else { return None }; s.project.layouts.get(*li).map(|layout| rustgis_render::layout::archive_revisions(layout, &s.project.maps)) }) .max() .unwrap_or_default() }; let before = revision(s); let result = super::share::saying_shadows(s, "The report", |s| report_pages_bytes(s, &doc, &pages, dpi, pdf, download)); before.finish_revision(revision(s), Some(result), "could not draw the report")? } fn report_pages_bytes( s: &mut Session, doc: &Doc, pages: &[rustgis_render::report::Page], dpi: f64, pdf: bool, download: Option<(super::exports::Room, &str)>, ) -> Result<(Vec, usize), String> { let n = if pdf { pages.len() } else { 1 }; let mut out = Vec::new(); let mut held = 0usize; let (mut frames_pictured, mut pages_pictured) = (Vec::new(), Vec::new()); // The legends its layouts' pages drew under a fit still cut, and the charts they drew, by // layout ([`keep_drawn`]). let (mut unfitted, mut charts) = (Vec::new(), Vec::new()); // The texts cut off on its layouts' pages, by layout and element. let mut clipped = ClippedTexts::new(); // Counted, and stopped on Cancel, a page at a time. rustgis_data::progress::begin(n); for k in 0..n { rustgis_data::progress::step(1).map_err(|e| e.to_string())?; let page_layout = match pages.get(k) { Some(rustgis_render::report::Page::Layout(li)) => Some(*li), _ => None, }; if let Some(li) = page_layout && let Some(layout) = s.project.layouts.get(li) { // The page's own number, as the page draws it: a text that shows it may only be cut // off on some pages. let info = rustgis_render::layout::PageInfo { page: k + 1, pages: pages.len(), page_name: String::new() }; let cut = rustgis_render::layout::clipped_text(layout, &s.project.maps, dpi, &info); if !cut.is_empty() { clipped.entry(li).or_default().extend(cut); } } if !pdf { let (drawn, listed, shown) = listing(|| rustgis_render::report::draw_page(doc, pages, k, dpi, &s.project.layouts, &mut s.project.maps)); let (img, ..) = drawn?; keep_drawn(&mut unfitted, page_layout, listed); keep_drawn(&mut charts, page_layout, shown); say_report_unfitted(s, &unfitted); say_report_charts(s, &charts); say_report_clipped(s, &clipped); say_sections_not_drawn(s, pages, 1); return Ok((img.encode_png().map_err(|e| e.to_string())?, pages.len())); } let (drawn, listed, shown) = listing(|| rustgis_render::report::pdf_page(doc, pages, k, dpi, &s.project.layouts, &mut s.project.maps)); let (page, pictured) = drawn?; keep_drawn(&mut unfitted, page_layout, listed); keep_drawn(&mut charts, page_layout, shown); if page.overflowed() { // Too large for vectors all the same: the page as one picture. pages_pictured.push(k + 1); let (drawn, listed, shown) = listing(|| rustgis_render::report::draw_page(doc, pages, k, dpi, &s.project.layouts, &mut s.project.maps)); let (img, w, h) = drawn?; keep_drawn(&mut unfitted, page_layout, listed); keep_drawn(&mut charts, page_layout, shown); out.push(rustgis_render::pdf::Page::new(&img, w, h)); } else { frames_pictured.extend(pictured); out.push(page); } held += out.last().map_or(0, rustgis_render::pdf::Page::len); // Where the file is downloaded, its pages are held to the room it has too. if let Some((room, path)) = download && held > room.bytes { return Err(room.refusal(path)); } if held > PDF_BUDGET { return Err(format!("the PDF passed {} MB at page {}; export at a lower dpi", PDF_BUDGET >> 20, k + 1)); } } say_report_unfitted(s, &unfitted); say_report_charts(s, &charts); say_report_clipped(s, &clipped); super::layout::pictured_notices(s, &frames_pictured, &pages_pictured); say_sections_not_drawn(s, pages, n); Ok((rustgis_render::pdf::write(&out, &doc.title), pages.len())) } /// Says the section lines (#2863) the layouts of the first `n` of `pages` don't draw, as the /// layout's own export says them: in one notice however many layouts leave one out, each line named /// with its layout, and a layout on several pages counted once. fn say_sections_not_drawn(s: &mut Session, pages: &[rustgis_render::report::Page], n: usize) { let mut seen = Vec::new(); let mut gaps = std::collections::BTreeMap::new(); for page in pages.iter().take(n) { let rustgis_render::report::Page::Layout(li) = page else { continue }; if seen.contains(li) { continue; } seen.push(*li); let Some(layout) = s.project.layouts.get(*li) else { continue }; for mut gap in rustgis_render::layout::sections_not_drawn(layout, &s.project.maps) { gap.frame_name = format!("{} in layout '{}'", gap.frame_name, layout.name); gaps.insert(gap, 1); } } if let Some(note) = rustgis_render::layout::sections_notice(&gaps, 1) { s.warn(format!("The report: {note}")); } } /// Runs `f`, and returns its result with the legends it drew under a fit still cut and the charts /// it drew. fn listing(f: impl FnOnce() -> T) -> (T, Vec, Vec) { let ((drawn, legends), charts) = rustgis_render::layout::drawn_charts(|| rustgis_render::layout::unfitted_legends(f)); (drawn, legends, charts) } /// Keeps what a page that draws the layout `layout` (by its index) `listed`, a legend cut under /// its fit or a chart drawn, each with that layout: the ids are a layout's own, so two layouts' /// elements sharing one are two. Only an exact repeat (the same element of the same layout, cut /// the same way or drawn with the same data) is dropped: the page drawn again as a picture, or /// the layout on another page of the report. A page that draws no layout lists nothing. fn keep_drawn(kept: &mut Vec<(usize, T)>, layout: Option, listed: Vec) { let Some(li) = layout else { return }; for l in listed { let item = (li, l); if !kept.contains(&item) { kept.push(item); } } } /// Says the legends [`keep_drawn`] kept, each named with its layout, as a layout's export /// says its own ([`super::layout::say_unfitted_legends_in`]). fn say_report_unfitted(s: &mut Session, kept: &[(usize, rustgis_render::layout::UnfittedLegend)]) { let named: Vec<_> = kept.iter().map(|(li, l)| (s.project.layouts.get(*li).map(|layout| (*li, layout.name.clone())), l.clone())).collect(); super::layout::say_unfitted_legends_in(s, &named); } /// Says what the charts [`keep_drawn`] kept leave out, each named with its layout, as a layout's /// export says its own ([`super::layout::chart_notice_lines_in`]). fn say_report_charts(s: &mut Session, kept: &[(usize, rustgis_render::layout::DrawnChart)]) { let drawn = kept.iter().filter_map(|(li, chart)| Some((Some(*li), s.project.layouts.get(*li)?, chart))); for note in super::layout::chart_notice_lines_in(drawn) { s.warn(note); } } /// The texts cut off on a report's layout pages: each layout's (by its index) elements, by id. type ClippedTexts = std::collections::BTreeMap>; /// Says the texts cut off on the report's layout pages as each layout's export says them, one /// notice a layout, which it names: the advice depends on that layout's elements. fn say_report_clipped(s: &mut Session, clipped: &ClippedTexts) { for (li, texts) in clipped { let Some(layout) = s.project.layouts.get(*li) else { continue }; let texts: Vec<_> = texts.clone().into_iter().collect(); let Some(note) = rustgis_render::layout::clipped_text_notice(layout, &texts) else { continue }; let name = crate::project::short_name(&layout.name); s.warn(format!("The report, layout \"{name}\": {note}")); } } /// Resolves the report creator's string-name or active/default layer convention. fn new_layer<'a>(input: &'a impl super::InputSource, p: &Value) -> Result<&'a Layer, String> { let context = input.input_context(); let s = &context; Ok(match ps(p, "layer") { Some(n) => find_layer(input, n).ok_or_else(|| format!("no layer named '{n}'"))?, None => s.active_layer.and_then(|id| s.map().layer(id)).ok_or("select a layer, or pass layer")?, }) } /// Fits an imported report to its explicit or stored existing layer without opening data. fn import_layer(input: &impl super::InputSource, p: &Value, r: &mut Report) -> Result<(), String> { let context = input.input_context(); let s = &context; if let Some(n) = ps(p, "layer") { r.layer = find_layer(input, n).ok_or_else(|| format!("no layer named '{n}'"))?.name.clone(); } else if find_layer(input, &r.layer).is_none() { // A template (or a report on a layer this project lacks) reports on the selected layer. r.layer = s .active_layer .and_then(|id| s.map().layer(id)) .map(|l| l.name.clone()) .ok_or("select the layer to report on, or pass layer")?; } Ok(()) } /// Whether a report naming layer `name` reads layer `id` of the active map, resolved as /// [`find_layer_map`] resolves it: a same-named layer of another map is another layer. pub(super) fn names_layer(s: &Session, name: &str, id: u32) -> bool { find_layer_map(s, name).is_some_and(|(map, layer)| std::ptr::eq(map, s.map()) && layer.id == id) } /// Whether a map other than the active one has a layer named `name`, which a report naming it /// reads while that map is active. pub(super) fn named_elsewhere(s: &Session, name: &str) -> bool { s.project.maps.iter().filter(|m| !std::ptr::eq(*m, s.map())).any(|m| m.layer_by_name(name).is_some()) } /// Finds a literal feature layer active-map first, then in project order. fn find_layer_map<'a>(input: &'a impl super::InputSource, name: &str) -> Option<(&'a rustgis_map::Map, &'a Layer)> { let context = input.input_context(); let s = &context; let in_map = |map: &'a rustgis_map::Map| map.layer_by_name(name).map(|l| (map, l)); in_map(s.map()).or_else(|| s.project.maps.iter().find_map(in_map)) } /// Retains the owning map of the already resolved layer, including repeated ids in other maps. fn owning_inputs(input: &impl super::InputSource, layer: &Layer) -> super::Inputs { let context = input.input_context(); let s = &context; super::Inputs::layers( s.project.maps.iter().flat_map(|map| map.layers.iter().filter(|l| std::ptr::eq(*l, layer)).map(move |l| (map, l))), ) } /// Binds the creator's actual layer without constructing report rows. fn new_inputs(input: &super::InputContext<'_>, p: &Value) -> Result { Ok(owning_inputs(input, new_layer(input, p)?)) } /// Binds the selected report's stored layer through the report executor's lookup. fn layer_inputs(input: &super::InputContext<'_>, p: &Value) -> Result { let mut inputs = owning_inputs(input, layer_of_input(input, index_input(input, p)?)?); inputs.extend(object_inputs(input, p)?); Ok(inputs) } /// [`optional_inputs`] of report.attachments. fn attachments_inputs(input: &super::InputContext<'_>, p: &Value) -> Result { optional_inputs(input, p, own_attachments) } /// [`optional_inputs`] of report.dynamicPicture. fn picture_inputs(input: &super::InputContext<'_>, p: &Value) -> Result { optional_inputs(input, p, own_picture) } /// Binds a field-dependent layer only when an optional field is actually supplied. fn optional_inputs(input: &super::InputContext<'_>, p: &Value, own: Own) -> Result { let (i, field) = optional_field(input, p, own)?; let mut inputs = if field.is_some() { owning_inputs(input, layer_of_input(input, i)?) } else { super::Inputs::layers(std::iter::empty()) }; inputs.extend(object_inputs(input, p)?); Ok(inputs) } /// Clearing a chart consumes no field; a new chart uses the existing field resolver. fn chart_inputs(input: &super::InputContext<'_>, p: &Value) -> Result { if p.get("category").is_none_or(Value::is_null) { return object_inputs(input, p); } let (i, _) = own_field_of(input, p, "category", own_category)?; { let mut inputs = owning_inputs(input, layer_of_input(input, i)?); inputs.extend(object_inputs(input, p)?); Ok(inputs) } } /// Returns the matching relationship direction and both keys for a report's current layer. fn relationship_columns<'a>(rel: &'a rustgis_map::design::Relationship, here: &str) -> Option<(&'a str, &'a str, &'a str)> { if rel.origin.eq_ignore_ascii_case(here) { Some((&rel.origin_key, &rel.destination, &rel.destination_key)) } else if rel.destination.eq_ignore_ascii_case(here) { Some((&rel.destination_key, &rel.origin, &rel.origin_key)) } else { None } } /// Selects the other relationship endpoint using the same direction test as row construction. fn opposite<'a>(rel: &'a rustgis_map::design::Relationship, here: &str) -> Option<&'a str> { relationship_columns(rel, here).map(|(_, other, _)| other) } /// Selects the first named relationship and its active-first other endpoint. fn subreport_input<'a>(input: &'a impl super::InputSource, here: &str, name: &str) -> Option<(&'a rustgis_map::Map, &'a Layer)> { let rel = relationships(input).find(|rel| rel.name.eq_ignore_ascii_case(name))?; find_layer_map(input, opposite(rel, here)?) } /// Collects the layer used to fit a new subreport's fields, without joining rows. fn subreport_inputs(input: &super::InputContext<'_>, p: &Value) -> Result { let s = input; let report = s.project.reports.get(index_input(input, p)?).ok_or("no report")?; let name = ps(p, "relationship").ok_or("relationship is required")?; let mut inputs = super::Inputs::layers(subreport_input(input, &report.layer, name)); inputs.extend(object_inputs(input, p)?); inputs.state["relationship"] = json!(relationships(input).find(|r| r.name.eq_ignore_ascii_case(name))); Ok(inputs) } /// A report layer change retains the first named relationship that involves the new layer. fn update_other(input: &impl super::InputSource, relationship: &str, layer: &str) -> Option { relationships(input).filter(|rel| rel.name.eq_ignore_ascii_case(relationship)).find_map(|rel| opposite(rel, layer)).map(str::to_string) } /// Binds only layer/field and retained-subreport dependencies consumed by this update. fn update_inputs(input: &super::InputContext<'_>, p: &Value) -> Result { let s = input; let report = s.project.reports.get(index_input(input, p)?).ok_or("no report")?; let mut inputs = object_inputs(input, p)?; if let Some(name) = ps(p, "layer") { let (map, layer) = find_layer_map(input, name).ok_or_else(|| format!("no layer named '{name}'"))?; inputs.layers.push((map.instance_id(), layer.id)); for sec in &report.sections { if let Section::Subreport { relationship, .. } = sec && let Some(other) = update_other(input, relationship, &layer.name) && let Some((map, layer)) = find_layer_map(input, &other) { inputs.layers.push((map.instance_id(), layer.id)); } } } else if ps(p, "where").is_some_and(|w| !w.trim().is_empty()) || ps(p, "sort").is_some() { inputs.extend(layer_inputs(input, p)?); } Ok(inputs) } /// Uses inline data/text precedence for pure import planning; path-held selectors remain unresolved. fn open_inputs(input: &super::InputContext<'_>, p: &Value) -> Result { if let Some(name) = ps(p, "layer") { return Ok(super::Inputs::layers([find_layer_map(input, name).ok_or_else(|| format!("no layer named '{name}'"))?])); } let Some(text) = super::items::inline_text(p)? else { return Ok(super::Inputs::unresolved()); }; let mut report: Report = serde_json::from_str(&text).map_err(|e| format!("not a RustGIS report: {e}"))?; report.clamp(); import_layer(input, p, &mut report)?; Ok(super::Inputs::layers(find_layer_map(input, &report.layer))) } /// Walks bounded report references before any row building, picture reading or output write. fn export_inputs(input: &super::InputContext<'_>, p: &Value) -> Result { let root = index_input(input, p)?; let mut inputs = layer_inputs(input, p)?; let mut seen = vec![root]; report_dependencies(input, root, &mut inputs, &mut seen, 0); Ok(inputs) } /// The same bounded related-report graph as blocks, without building rows or reading pictures. fn report_dependencies(input: &impl super::InputSource, i: usize, inputs: &mut super::Inputs, seen: &mut Vec, depth: usize) { let context = input.input_context(); let s = &context; let Some(report) = s.project.reports.get(i) else { return }; if let Some(found) = find_layer_map(input, &report.layer) { inputs.layers.push((found.0.instance_id(), found.1.id)); } else { return; } let mut own = super::Inputs::layers(std::iter::empty()); own.state = json!({"report":i,"name":report.name,"layer":report.layer,"picture":report.picture_field,"attachments":report.attachments_field, "refs":report.sections.iter().filter_map(|section|match section {Section::Layout{layout,..}=>Some(json!({"layout":layout})),Section::Related{report}=>Some(json!({"report":report})),Section::Subreport{relationship,fields}=>Some(json!({"relationship":relationship,"fields":fields})),_=>None}).collect::>()}); inputs.extend(own); for sec in &report.sections { match sec { Section::Subreport { relationship, .. } => { if let Some((map, layer)) = subreport_input(input, &report.layer, relationship) { inputs.layers.push((map.instance_id(), layer.id)); } } Section::Layout { layout, .. } => { if let Some(li) = s.project.layouts.iter().position(|l| l.name.eq_ignore_ascii_case(layout)) { inputs.extend(super::layout::render_inputs_at(input, li)); } } Section::Related { report } if depth < MAX_DEPTH => { if let Some(j) = s.project.reports.iter().position(|r| r.name.eq_ignore_ascii_case(report)) && !seen.contains(&j) { seen.push(j); report_dependencies(input, j, inputs, seen, depth + 1); } } _ => {} } } } /// Reference-only edits bind the actual report without requiring its stored layer to still exist. fn object_inputs(input: &super::InputContext<'_>, p: &Value) -> Result { let s = input; let i = index_input(input, p)?; let mut inputs = super::Inputs::layers(std::iter::empty()); inputs.state = json!({"report":i,"name":s.project.reports.get(i).map(|r|&r.name)}); Ok(inputs) } /// A related report is the first literal name match used by section insertion. fn related_report_at(input: &impl super::InputSource, name: &str) -> Result { let context = input.input_context(); let s = &context; s.project.reports.iter().position(|r| r.name.eq_ignore_ascii_case(name)).ok_or_else(|| format!("no report '{name}'")) } /// Binds both existing report objects, without building either one's rows. fn related_report_inputs(input: &super::InputContext<'_>, p: &Value) -> Result { let s = input; let mut inputs = object_inputs(input, p)?; let i = related_report_at(input, ps(p, "related").ok_or("related is required (another report's name)")?)?; inputs.state["related"] = json!((i, &s.project.reports.get(i).map(|r| &r.name))); Ok(inputs) } /// An existing layout is a stored reference here; an imported document stays frozen as raw params. fn template_inputs(input: &super::InputContext<'_>, p: &Value) -> Result { let mut inputs = object_inputs(input, p)?; if let Some(name) = ps(p, "layout") { inputs.state["layout"] = json!(template_at(input, name)?); } Ok(inputs) } /// Shares the exact first-name layout lookup with the reference-only template insertion. fn template_at(input: &impl super::InputSource, name: &str) -> Result { let context = input.input_context(); let s = &context; s.project.layouts.iter().position(|l| l.name.eq_ignore_ascii_case(name)).ok_or_else(|| format!("no layout '{name}'")) } /// The presentation/report selected by the live session, using the same pure resolver as approval. pub fn index(s: &Session, p: &Value) -> Result { index_input(s, p) } /// The live report layer, using the same active-first resolver as approval. pub fn layer_of(s: &Session, i: usize) -> Result<&Layer, String> { layer_of_input(s, i) } #[cfg(test)] mod tests { use rustgis_map::layout::LegendFit; use rustgis_render::layout::UnfittedLegend; /// A page drawn again as a picture, or a layout shown on two pages, lists its legends again: /// kept once. Two layouts' legends sharing an id, or one legend cut otherwise, are kept apart; /// a page that draws no layout keeps nothing. #[test] fn a_reports_cut_legends_are_kept_once_by_layout() { let cut = |columns: usize| UnfittedLegend { id: 3, name: "Key".into(), fit: LegendFit::AdjustColumns, columns, too_wide: false, too_tall: true, }; let mut kept = Vec::new(); super::keep_drawn(&mut kept, Some(0), vec![cut(1)]); // The vector page, then the same page drawn as a picture. super::keep_drawn(&mut kept, Some(0), vec![cut(1)]); super::keep_drawn(&mut kept, Some(1), vec![cut(1)]); super::keep_drawn(&mut kept, Some(0), vec![cut(2)]); super::keep_drawn(&mut kept, None, vec![cut(3)]); assert_eq!(kept, [(0, cut(1)), (1, cut(1)), (0, cut(2))]); } }