pub const CREDIT: &str = "© geolith";
pub const META_PART_TAG: [u8; 4] = *b"META";
#[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}")
}
}
#[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");
}
}