geolith 0.2.1

Shared library of the geolith builders: source readers, conflation, admin areas, tile encoding and the PMTiles and MBTiles writers
Documentation
//! The credit every geolith output carries.
//!
//! Each builder writes [`CREDIT`] into the metadata of what it makes: the
//! `attribution` of a `PMTiles` archive (and so of its `TileJSON`), and the
//! `META` part of a `.glx` file. The credit comes after the attribution of
//! the data the output holds ([`data_credit`]): the OpenStreetMap credit the
//! ODbL needs, the Overture credit, and the `DataMeet` credit that CC BY 4.0
//! needs for the Survey of India outline. A user can append text, never remove
//! the credit or the data attribution.

/// The default credit.
pub const CREDIT: &str = "© geolith";

/// The OpenStreetMap credit (the `ODbL` asks for it).
pub const OSM_CREDIT: &str = "© OpenStreetMap contributors (ODbL)";

/// The Overture credit (the line the Overture attribution page gives).
pub const OVERTURE_CREDIT: &str = "Overture Maps Foundation";

/// The Natural Earth credit (public domain; the credit is a courtesy).
pub const NATURAL_EARTH_CREDIT: &str = "Natural Earth (public domain)";

/// The credit of the Survey of India outline (`DataMeet`, CC BY 4.0).
pub const DATAMEET_CREDIT: &str = "India boundary: DataMeet India community (CC BY 4.0)";

/// The data a build read, for [`data_credit`].
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct DataSources {
    /// `OpenStreetMap` data (a PBF, or Overture records that carry it).
    pub osm: bool,
    /// Overture Maps data.
    pub overture: bool,
    /// The Survey of India outline of `DataMeet`.
    pub datameet: bool,
    /// Natural Earth data.
    pub natural_earth: bool,
}

impl DataSources {
    /// The sources of a build.
    #[must_use]
    pub const fn new(osm: bool, overture: bool, datameet: bool) -> Self {
        Self {
            osm,
            overture,
            datameet,
            natural_earth: false,
        }
    }

    /// These sources and Natural Earth data.
    #[must_use]
    pub const fn with_natural_earth(mut self) -> Self {
        self.natural_earth = true;
        self
    }
}

/// The credit of the data an output holds, without [`CREDIT`]: the sources
/// in the order OSM, Overture, Natural Earth, `DataMeet`; OSM, Overture and
/// Natural Earth on one line the way the Overture attribution page writes it.
/// Empty when no source is set.
#[must_use]
pub fn data_credit(sources: DataSources) -> String {
    data_credit_with_osm(sources, OSM_CREDIT)
}

/// [`data_credit`] with `osm` as the OSM credit text, for a `TileJSON`
/// attribution that links it.
#[must_use]
pub fn data_credit_with_osm(sources: DataSources, osm: &str) -> String {
    let mut line = Vec::new();
    if sources.osm {
        line.push(osm);
    }
    if sources.overture {
        line.push(OVERTURE_CREDIT);
    }
    if sources.natural_earth {
        line.push(NATURAL_EARTH_CREDIT);
    }
    let mut parts = Vec::new();
    if !line.is_empty() {
        parts.push(line.join(", "));
    }
    if sources.datameet {
        parts.push(DATAMEET_CREDIT.to_owned());
    }
    parts.join(" | ")
}

/// The tag of the `.glx` part that carries the credit.
pub const META_PART_TAG: [u8; 4] = *b"META";

/// `existing` with the credit after it; the credit alone when `existing`
/// is empty. A text that already ends with the credit is returned as it is.
#[must_use]
pub fn with_credit(existing: &str) -> String {
    let existing = existing.trim();
    if existing.is_empty() {
        CREDIT.to_owned()
    } else if existing.ends_with(CREDIT) {
        existing.to_owned()
    } else {
        format!("{existing} | {CREDIT}")
    }
}

/// The JSON of a `.glx` `META` part: the credit of the data and of geolith,
/// the builder and its version.
#[must_use]
pub fn meta_json(builder: &str, version: &str, sources: DataSources) -> Vec<u8> {
    let meta = serde_json::json!({
        "attribution": with_credit(&data_credit(sources)),
        "builder": format!("{builder} {version}"),
    });
    meta.to_string().into_bytes()
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn the_credit_follows_the_data_attribution() {
        assert_eq!(with_credit(""), "© geolith");
        assert_eq!(with_credit("  "), "© geolith");
        assert_eq!(
            with_credit("© OpenStreetMap"),
            "© OpenStreetMap | © geolith"
        );
    }

    #[test]
    fn the_credit_is_added_once() {
        let once = with_credit("© OpenStreetMap");
        assert_eq!(with_credit(&once), once);
    }

    /// The credit text is the one the docs give to users
    /// (docs.geolith.app `search/hosted/attribution`, `tiles/hosted`): the
    /// strings are written out here, not built from the constants.
    #[test]
    fn the_data_credit_is_the_line_the_docs_promise() {
        assert_eq!(
            data_credit(DataSources::new(true, true, false)),
            "© OpenStreetMap contributors (ODbL), Overture Maps Foundation"
        );
        assert_eq!(
            data_credit(DataSources::new(true, false, false)),
            "© OpenStreetMap contributors (ODbL)"
        );
        assert_eq!(
            data_credit(DataSources::new(false, true, false)),
            "Overture Maps Foundation"
        );
        assert_eq!(
            data_credit(DataSources::new(true, true, true)),
            "© OpenStreetMap contributors (ODbL), Overture Maps Foundation | \
             India boundary: DataMeet India community (CC BY 4.0)"
        );
        assert_eq!(data_credit(DataSources::default()), "");
        assert_eq!(
            data_credit(DataSources::new(true, true, true).with_natural_earth()),
            "© OpenStreetMap contributors (ODbL), Overture Maps Foundation, \
             Natural Earth (public domain) | \
             India boundary: DataMeet India community (CC BY 4.0)"
        );
        assert_eq!(
            data_credit(DataSources::new(true, false, false).with_natural_earth()),
            "© OpenStreetMap contributors (ODbL), Natural Earth (public domain)"
        );
        assert_eq!(
            data_credit_with_osm(DataSources::new(true, true, false), "<a>OSM</a>"),
            "<a>OSM</a>, Overture Maps Foundation"
        );
    }

    #[test]
    fn the_meta_part_is_json_with_the_credit_and_the_builder() {
        let json: serde_json::Value = serde_json::from_slice(&meta_json(
            "geolith-search-builder",
            "0.1.0",
            DataSources::new(true, true, true),
        ))
        .expect("json");
        assert_eq!(
            json["attribution"],
            "© OpenStreetMap contributors (ODbL), Overture Maps Foundation | \
             India boundary: DataMeet India community (CC BY 4.0) | © geolith"
        );
        assert_eq!(json["builder"], "geolith-search-builder 0.1.0");
        assert_eq!(META_PART_TAG, *b"META");
    }

    #[test]
    fn a_meta_part_without_data_credits_holds_the_geolith_credit_alone() {
        let json: serde_json::Value = serde_json::from_slice(&meta_json(
            "geolith-search-builder",
            "0.1.0",
            DataSources::default(),
        ))
        .expect("json");
        assert_eq!(json["attribution"], "© geolith");
    }
}