geolith 0.1.5

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. A user can append text, never remove the
//! credit or the data attribution (OpenStreetMap's ODbL and the Overture
//! licences need theirs).

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

/// 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, the builder and its
/// version.
#[must_use]
pub fn meta_json(builder: &str, version: &str) -> Vec<u8> {
    let meta = serde_json::json!({
        "attribution": CREDIT,
        "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);
    }

    #[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")).expect("json");
        assert_eq!(json["attribution"], "© geolith");
        assert_eq!(json["builder"], "geolith-search-builder 0.1.0");
        assert_eq!(META_PART_TAG, *b"META");
    }
}