feat: new country command

This commit is contained in:
Artemy 2024-04-24 19:51:02 +03:00
parent ff8e88b39b
commit b1f5e42a46
7 changed files with 147 additions and 20 deletions

1
.gitignore vendored
View file

@ -1 +1,2 @@
/target /target
/map

27
Cargo.lock generated
View file

@ -113,6 +113,7 @@ dependencies = [
"geojson", "geojson",
"serde", "serde",
"serde_json", "serde_json",
"toml",
"toml_edit", "toml_edit",
"wax", "wax",
] ]
@ -491,6 +492,15 @@ dependencies = [
"serde", "serde",
] ]
[[package]]
name = "serde_spanned"
version = "0.6.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eb3622f419d1296904700073ea6cc23ad690adbd66f13ea683df73298736f0c1"
dependencies = [
"serde",
]
[[package]] [[package]]
name = "smallvec" name = "smallvec"
version = "1.13.2" version = "1.13.2"
@ -552,11 +562,26 @@ dependencies = [
"syn", "syn",
] ]
[[package]]
name = "toml"
version = "0.8.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e9dd1545e8208b4a5af1aa9bbd0b4cf7e9ea08fabc5d0a5c67fcaafa17433aa3"
dependencies = [
"serde",
"serde_spanned",
"toml_datetime",
"toml_edit",
]
[[package]] [[package]]
name = "toml_datetime" name = "toml_datetime"
version = "0.6.5" version = "0.6.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3550f4e9685620ac18a50ed434eb3aec30db8ba93b0287467bca5826ea25baf1" checksum = "3550f4e9685620ac18a50ed434eb3aec30db8ba93b0287467bca5826ea25baf1"
dependencies = [
"serde",
]
[[package]] [[package]]
name = "toml_edit" name = "toml_edit"
@ -565,6 +590,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d3328d4f68a705b2a4498da1d580585d39a6510f98318a2cec3018a7ec61ddef" checksum = "d3328d4f68a705b2a4498da1d580585d39a6510f98318a2cec3018a7ec61ddef"
dependencies = [ dependencies = [
"indexmap", "indexmap",
"serde",
"serde_spanned",
"toml_datetime", "toml_datetime",
"winnow", "winnow",
] ]

View file

@ -11,6 +11,7 @@ geo = "0.28.0"
geojson = "0.24.1" geojson = "0.24.1"
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0" serde_json = "1.0"
toml = "0.8.12"
toml_edit = "0.22.12" toml_edit = "0.22.12"
wax = "0.6.0" wax = "0.6.0"

View file

@ -2,6 +2,7 @@ use clap::Parser;
mod build; mod build;
mod init; mod init;
mod new;
mod types; mod types;
use types::Commands; use types::Commands;
@ -12,6 +13,7 @@ fn main() {
match args.cmd { match args.cmd {
Commands::Build => build::build(), Commands::Build => build::build(),
Commands::Init { name } => init::init(name), Commands::Init { name } => init::init(name),
Commands::New { cmd } => new::new(cmd),
_ => unimplemented!(), _ => unimplemented!(),
} }
} }

75
src/new.rs Normal file
View file

@ -0,0 +1,75 @@
use std::{fs, path::Path};
use toml_edit::{value, DocumentMut, Value};
use crate::types::{Config, Country, NewCommands};
pub fn new(cmd: NewCommands) {
match cmd {
NewCommands::Country {
name,
id,
description,
foundation_date,
flag,
about,
} => {
let name = name.unwrap_or_default();
let description = description.unwrap_or_default();
let foundation_date = foundation_date.unwrap_or_default();
let flag = flag.unwrap_or_default();
let about = about;
let country = Country {
name: name.clone(),
description,
foundation_date,
flag,
about,
tags: Some(vec![]),
};
let config = fs::read_to_string("config.toml").unwrap();
// Validate config
let c = toml::from_str::<Config>(&config);
match c {
Ok(c) => c,
Err(err) => {
panic!("Invalid config: {}", err);
}
};
// Get actual config
let mut config = config.parse::<DocumentMut>().unwrap();
// Add country to layers
let layers = config["country"]["layers"].clone().into_value().unwrap();
let layers = if let Value::Array(mut layers) = layers {
layers.push(&id);
layers
} else {
panic!("layers is not an array");
};
config["country"]["layers"] = value(layers);
fs::write("config.toml", config.to_string()).unwrap();
// Add country to countries
let country_folder = Path::new(".").join("countries").join(&id);
fs::create_dir_all(&country_folder).unwrap();
fs::write(
country_folder.join("country.toml"),
toml::to_string_pretty(&country).unwrap(),
)
.unwrap();
fs::write(
country_folder.join("country.geojson"),
include_str!("./templates/sample.geojson"),
)
.unwrap();
}
}
}

View file

@ -10,22 +10,23 @@ geo_file = "geo.geojson"
# generate_colors = false # generate_colors = false
# show_markers = false # show_markers = false
# Include or exclude countries based on tags
# Glob syntax is supported
[[processing.tags]]
include = [] # [] = not filtered
# exclude = ["test", "test2"]
# [[processing.countries_rewrite]]
# name = "name"
# color = "#000000"
# Information for public repository in cimengine. See: https://github.com/CIMEngine/MapList # Information for public repository in cimengine. See: https://github.com/CIMEngine/MapList
# [[processing.public]] # [processing.public]
# name = "Sample Map" # name = "Sample Map"
# geo = "https://example.com/geo.geojson" # geo = "https://example.com/geo.geojson"
# countries = "https://example.com/countries.json" # countries = "https://example.com/countries.json"
# Include or exclude countries based on tags
# Glob syntax is supported
# [processing.tags]
# include = [] # [] = not filtered
# exclude = ["test", "test2"]
# [processing.countries_rewrite]
# name = "name"
# color = "#000000"
# Nature layers
[[nature]] [[nature]]
id = "water" id = "water"
color = "#75cff0" color = "#75cff0"

View file

@ -35,7 +35,19 @@ pub enum Commands {
#[derive(Debug, Subcommand)] #[derive(Debug, Subcommand)]
pub enum NewCommands { pub enum NewCommands {
/// Create new country /// Create new country
Country { name: String }, Country {
id: String,
#[clap(short, long)]
name: Option<String>,
#[clap(short, long)]
description: Option<String>,
#[clap(short, long)]
foundation_date: Option<String>,
#[clap(long)]
flag: Option<String>,
#[clap(short, long)]
about: Option<String>,
},
} }
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]
@ -59,12 +71,13 @@ pub struct ProcessingConfig {
tags: Option<ProcessingTagsConfig>, tags: Option<ProcessingTagsConfig>,
countries_rewrite: Option<CountryRewriteConfig>, countries_rewrite: Option<CountryRewriteConfig>,
public: Option<PublicConfig>,
} }
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]
pub struct ProcessingTagsConfig { pub struct ProcessingTagsConfig {
include: Vec<String>, include: Option<Vec<String>>,
exclude: Vec<String>, exclude: Option<Vec<String>>,
} }
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]
@ -77,11 +90,18 @@ pub struct CountryRewriteConfig {
} }
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Country { pub struct PublicConfig {
name: String, name: String,
description: String, geo: String,
foundation_date: String, countries: String,
flag: String, }
about: Option<String>,
tags: Option<Vec<String>>, #[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Country {
pub name: String,
pub description: String,
pub foundation_date: String,
pub flag: String,
pub about: Option<String>,
pub tags: Option<Vec<String>>,
} }