pub const CREDIT: &str = "© geolith";
pub const OSM_CREDIT: &str = "© OpenStreetMap contributors (ODbL)";
pub const OVERTURE_CREDIT: &str = "Overture Maps Foundation";
pub const NATURAL_EARTH_CREDIT: &str = "Natural Earth (public domain)";
pub const DATAMEET_CREDIT: &str = "India boundary: DataMeet India community (CC BY 4.0)";
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct DataSources {
pub osm: bool,
pub overture: bool,
pub datameet: bool,
pub natural_earth: bool,
}
impl DataSources {
#[must_use]
pub const fn new(osm: bool, overture: bool, datameet: bool) -> Self {
Self {
osm,
overture,
datameet,
natural_earth: false,
}
}
#[must_use]
pub const fn with_natural_earth(mut self) -> Self {
self.natural_earth = true;
self
}
}
#[must_use]
pub fn data_credit(sources: DataSources) -> String {
data_credit_with_osm(sources, OSM_CREDIT)
}
#[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(" | ")
}
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, 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);
}
#[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");
}
}