git clone https://garage44.eu/jeroen/cartopolis.git.. /crates/geo/src/formats/feeds.rs
//! `feeds/index.json` and `feeds/{id}/14/{x}/{y}.json` — data layers: records
//! from a source outside Cartopolis (TripleXL's saved queries first), placed on
//! the map (`docs/notes/data-layers.md`).
//!
//! Written by atlas's `feeds.rs`; read by the client's `systems::feeds`. The
//! index lists every layer with what its card and key need; a layer's cells sit
//! under a stamped `coverage.json` of their own (`atlas::stamped`), so a client
//! refetches only the cells a refresh changed.
//!
//! A record's colour is an index into its layer's `values`, not a colour: the
//! palette is the client's, and a layer that grows an eighth value must not
//! repaint the seven before it.
use serde::{Deserialize, Serialize};
/// `feeds/index.json`.
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct Index {
#[serde(default)]
pub feeds: Vec<Feed>,
}
/// One layer, as the Layers panel, the card and the key need it.
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct Feed {
/// When the records last changed, ms since the epoch — not when they were
/// last asked for, which changes nothing a reader shows.
#[serde(default)]
pub changed_ms: u64,
/// The column the colours follow, `""` when the layer has one colour.
#[serde(default)]
pub colour_by: String,
/// The credit line, as the source asks for it.
#[serde(default)]
pub credit: String,
/// The columns a record's `fields` hold, in the order the card shows them.
#[serde(default)]
pub fields: Vec<String>,
/// The publisher it is grouped under in Layers ▸ Data.
#[serde(default)]
pub group: String,
pub id: String,
#[serde(default)]
pub licence: String,
pub name: String,
/// Records placed on the map, in all cells.
#[serde(default)]
pub records: u64,
/// The values of `colour_by`, most common first, at most
/// [`MAX_VALUES`]; a record whose value is not among them has the index
/// [`MAX_VALUES`], "other" — the same index whatever `values` holds, so
/// "other" keeps its colour when a layer gains a value.
#[serde(default)]
pub values: Vec<String>,
/// Where the data comes from, for "via …" — `"TripleXL"`.
#[serde(default)]
pub via: String,
}
/// How many values of a colour column get a colour of their own; the rest are
/// "other". Eight is what a categorical palette keeps apart at marker size.
pub const MAX_VALUES: usize = 7;
/// One record: `[lat, lng, title, value, fields, link]`. `value` indexes the
/// layer's `values` (−1 when it has no colour column); `fields` follow the
/// layer's `fields`; `link` is the record's own address, `""` when it has none.
pub type Record = (f64, f64, String, i64, Vec<String>, String);
/// `feeds/{id}/14/{x}/{y}.json`.
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct Cell {
#[serde(default)]
pub records: Vec<Record>,
}
/// Parse the index.
///
/// # Errors
///
/// When the text is not JSON or not this document.
pub fn parse_index(text: &str) -> Result<Index, String> {
super::from_str(text)
}
/// Parse a cell file.
///
/// # Errors
///
/// When the text is not JSON or not this document.
pub fn parse_cell(text: &str) -> Result<Cell, String> {
super::from_str(text)
}
#[cfg(test)]
mod tests {
use super::*;
const CELL: &str = concat!(
r#"{"records":[[53.2229,6.5571,"Hoge Noorderplantsoenstuw",0,"#,
r#"["KST6211","overlaat"],"https://www.triplexl.nl/id/noorderzijlvest/stuw/KST6211"]]}"#
);
#[test]
fn a_cell_round_trips_byte_for_byte() {
let cell = parse_cell(CELL).unwrap();
assert_eq!(cell.records[0].2, "Hoge Noorderplantsoenstuw");
assert_eq!(cell.records[0].4, vec!["KST6211", "overlaat"]);
assert_eq!(
String::from_utf8(super::super::to_vec(&cell).unwrap()).unwrap(),
CELL
);
}
#[test]
fn an_index_from_before_a_field_still_reads() {
let index = parse_index(r#"{"feeds":[{"id":"nzv-stuwen","name":"Stuwen"}]}"#).unwrap();
assert_eq!(index.feeds[0].name, "Stuwen");
assert!(index.feeds[0].values.is_empty());
}
}