Skip to content

Commit d028617

Browse files
committed
feat: country rewrite
1 parent 0acf4ec commit d028617

4 files changed

Lines changed: 98 additions & 39 deletions

File tree

src/build.rs

Lines changed: 42 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
use std::{fs, path::Path, time};
22

33
use serde_json::json;
4-
use wax::{Glob, Pattern};
54

65
use crate::{
76
types::{CountryData, ToCollection},
8-
utils::{diff_countries, get_country, read_config},
7+
utils::{
8+
diff_countries, get_country, is_match, read_config, rewrite_if_some, rewrite_if_some_option,
9+
},
910
};
11+
use wax::Glob;
1012

1113
pub fn build() {
1214
let config = read_config();
@@ -28,50 +30,55 @@ pub fn build() {
2830
{
2931
let dissolved_time = time::Instant::now();
3032

31-
if tags.len() == 0 {
32-
for country_id in &config.main.layers {
33-
countries.push(get_country(country_id.to_owned()));
34-
}
35-
} else {
36-
for country_id in &config.main.layers {
37-
let country = get_country(country_id.to_owned());
38-
39-
match &country.config.tags {
40-
Some(tags) => {
41-
let mut matches = false;
42-
for glob in &globs {
43-
for tag in tags {
44-
if glob.is_match(tag.as_str()) {
45-
matches = true;
46-
}
47-
}
48-
}
49-
50-
if !matches {
51-
continue;
52-
}
53-
54-
countries.push(country);
55-
}
56-
None => {
57-
continue;
58-
}
59-
}
33+
for country_id in &config.main.layers {
34+
let country = get_country(country_id.to_owned());
35+
36+
if is_match(&country.config.tags, &globs) {
37+
countries.push(country);
6038
}
6139
}
6240

6341
println!("Dissolved in {:?}", dissolved_time.elapsed());
6442
}
6543

66-
// TODO: Add country_rewrite support
67-
68-
let countries = {
44+
let countries: Vec<CountryData> = {
6945
let diff_time = time::Instant::now();
7046

71-
let countries = diff_countries(countries);
47+
let mut countries = diff_countries(countries);
7248

7349
println!("Diffed in {:?}", diff_time.elapsed());
7450

51+
countries.iter_mut().for_each(|c| {
52+
if !processing_item.show_markers.unwrap_or(true) {
53+
c.markers = vec![];
54+
}
55+
56+
for country_rewrite in processing_item.countries_rewrite.clone().unwrap_or(vec![]) {
57+
let tags = country_rewrite.tags.unwrap_or(vec![]);
58+
let globs: Vec<Glob> = tags.iter().map(|tag| Glob::new(tag).unwrap()).collect();
59+
60+
if is_match(&c.config.tags, &globs) {
61+
rewrite_if_some(country_rewrite.properties.name, &mut c.config.name);
62+
rewrite_if_some(
63+
country_rewrite.properties.description,
64+
&mut c.config.description,
65+
);
66+
rewrite_if_some(
67+
country_rewrite.properties.foundation_date,
68+
&mut c.config.foundation_date,
69+
);
70+
rewrite_if_some(country_rewrite.properties.flag, &mut c.config.flag);
71+
rewrite_if_some_option(
72+
country_rewrite.properties.about,
73+
&mut c.config.about,
74+
);
75+
rewrite_if_some(country_rewrite.properties.fill, &mut c.config.fill);
76+
rewrite_if_some(country_rewrite.properties.stroke, &mut c.config.stroke);
77+
rewrite_if_some_option(country_rewrite.properties.tags, &mut c.config.tags);
78+
}
79+
}
80+
});
81+
7582
countries
7683
};
7784

src/templates/config.toml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,18 @@ output_folder = "./out/map"
2020
# Glob syntax is supported
2121
# tags = ["test-[!2]"]
2222

23-
# Rewrite properties of all countries. All fields are optional.
24-
# [processing.countries_rewrite]
23+
# Rewrite properties of countries by tags. All fields except `tags` are optional.
24+
# [[processing.countries_rewrite]]
25+
# tags = ["test"]
26+
# [processing.countries_rewrite.properties]
2527
# name = "name"
2628
# color = "#000000"
2729
# description = "description"
2830
# foundation_date = "2024-01-01"
2931
# flag = "https://example.com/flag.png"
3032
# about = "https://example.com/about.html"
3133

32-
# Nature layers
34+
# Additional nature layers
3335
[[nature]]
3436
id = "water"
3537
color = "#75cff0"

src/types.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,17 +75,26 @@ pub struct ProcessingConfig {
7575
pub output_folder: String,
7676

7777
pub tags: Option<Vec<String>>,
78-
pub countries_rewrite: Option<CountryRewriteConfig>,
78+
pub countries_rewrite: Option<Vec<CountryRewriteConfig>>,
7979
pub public: Option<PublicConfig>,
8080
}
8181

8282
#[derive(Debug, Serialize, Deserialize, Clone)]
8383
pub struct CountryRewriteConfig {
84+
pub tags: Option<Vec<String>>,
85+
pub properties: CountryRewriteConfigProps,
86+
}
87+
88+
#[derive(Debug, Serialize, Deserialize, Clone)]
89+
pub struct CountryRewriteConfigProps {
8490
pub name: Option<String>,
8591
pub description: Option<String>,
8692
pub foundation_date: Option<String>,
8793
pub flag: Option<String>,
94+
pub fill: Option<String>,
95+
pub stroke: Option<String>,
8896
pub about: Option<String>,
97+
pub tags: Option<Vec<String>>,
8998
}
9099

91100
#[derive(Debug, Serialize, Deserialize, Clone)]

src/utils.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::{fs, path::Path};
22

33
use geo::{BooleanOps, MultiPolygon};
44
use geojson::GeoJson;
5+
use wax::{Glob, Pattern};
56

67
use crate::types::{Config, CountryConfig, CountryData, Territory, ToMultiPolygon, ToSplitGeo};
78

@@ -77,3 +78,43 @@ pub fn hash_hex_color(s: String) -> String {
7778

7879
format!("#{}", hex_str.chars().take(6).collect::<String>())
7980
}
81+
82+
pub fn is_match(tags: &Option<Vec<String>>, globs: &Vec<Glob>) -> bool {
83+
if globs.len() == 0 {
84+
return true;
85+
}
86+
87+
match tags {
88+
Some(tags) => {
89+
let mut matches = false;
90+
for glob in globs {
91+
for tag in tags {
92+
if glob.is_match(tag.as_str()) {
93+
matches = true;
94+
}
95+
}
96+
}
97+
98+
matches
99+
}
100+
None => true,
101+
}
102+
}
103+
104+
pub fn rewrite_if_some<T>(value: Option<T>, rewrite: &mut T) {
105+
match value {
106+
Some(value) => {
107+
*rewrite = value;
108+
}
109+
None => {}
110+
}
111+
}
112+
113+
pub fn rewrite_if_some_option<T>(value: Option<T>, rewrite: &mut Option<T>) {
114+
match value {
115+
Some(value) => {
116+
*rewrite = Some(value);
117+
}
118+
None => {}
119+
}
120+
}

0 commit comments

Comments
 (0)