From b1f5e42a4673822fa1b9dd2de1ce700e50d0ccf0 Mon Sep 17 00:00:00 2001 From: Artemy Date: Wed, 24 Apr 2024 19:51:02 +0300 Subject: [PATCH] feat: new country command --- .gitignore | 1 + Cargo.lock | 27 ++++++++++++++ Cargo.toml | 1 + src/main.rs | 2 ++ src/new.rs | 75 +++++++++++++++++++++++++++++++++++++++ src/templates/config.toml | 23 ++++++------ src/types.rs | 38 +++++++++++++++----- 7 files changed, 147 insertions(+), 20 deletions(-) create mode 100644 src/new.rs diff --git a/.gitignore b/.gitignore index ea8c4bf..da5c49f 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /target +/map \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock index cc5f8f8..1734c68 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -113,6 +113,7 @@ dependencies = [ "geojson", "serde", "serde_json", + "toml", "toml_edit", "wax", ] @@ -491,6 +492,15 @@ dependencies = [ "serde", ] +[[package]] +name = "serde_spanned" +version = "0.6.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eb3622f419d1296904700073ea6cc23ad690adbd66f13ea683df73298736f0c1" +dependencies = [ + "serde", +] + [[package]] name = "smallvec" version = "1.13.2" @@ -552,11 +562,26 @@ dependencies = [ "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]] name = "toml_datetime" version = "0.6.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3550f4e9685620ac18a50ed434eb3aec30db8ba93b0287467bca5826ea25baf1" +dependencies = [ + "serde", +] [[package]] name = "toml_edit" @@ -565,6 +590,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d3328d4f68a705b2a4498da1d580585d39a6510f98318a2cec3018a7ec61ddef" dependencies = [ "indexmap", + "serde", + "serde_spanned", "toml_datetime", "winnow", ] diff --git a/Cargo.toml b/Cargo.toml index 2b61381..ad675ef 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,6 +11,7 @@ geo = "0.28.0" geojson = "0.24.1" serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" +toml = "0.8.12" toml_edit = "0.22.12" wax = "0.6.0" diff --git a/src/main.rs b/src/main.rs index 5d70dfd..eecd53e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,6 +2,7 @@ use clap::Parser; mod build; mod init; +mod new; mod types; use types::Commands; @@ -12,6 +13,7 @@ fn main() { match args.cmd { Commands::Build => build::build(), Commands::Init { name } => init::init(name), + Commands::New { cmd } => new::new(cmd), _ => unimplemented!(), } } diff --git a/src/new.rs b/src/new.rs new file mode 100644 index 0000000..6cef4f9 --- /dev/null +++ b/src/new.rs @@ -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); + + match c { + Ok(c) => c, + Err(err) => { + panic!("Invalid config: {}", err); + } + }; + + // Get actual config + let mut config = config.parse::().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(); + } + } +} diff --git a/src/templates/config.toml b/src/templates/config.toml index 8ac93bd..76da029 100644 --- a/src/templates/config.toml +++ b/src/templates/config.toml @@ -10,22 +10,23 @@ geo_file = "geo.geojson" # generate_colors = 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 -# [[processing.public]] +# [processing.public] # name = "Sample Map" # geo = "https://example.com/geo.geojson" # 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]] id = "water" color = "#75cff0" diff --git a/src/types.rs b/src/types.rs index e43e11d..3f07025 100644 --- a/src/types.rs +++ b/src/types.rs @@ -35,7 +35,19 @@ pub enum Commands { #[derive(Debug, Subcommand)] pub enum NewCommands { /// Create new country - Country { name: String }, + Country { + id: String, + #[clap(short, long)] + name: Option, + #[clap(short, long)] + description: Option, + #[clap(short, long)] + foundation_date: Option, + #[clap(long)] + flag: Option, + #[clap(short, long)] + about: Option, + }, } #[derive(Debug, Serialize, Deserialize, Clone)] @@ -59,12 +71,13 @@ pub struct ProcessingConfig { tags: Option, countries_rewrite: Option, + public: Option, } #[derive(Debug, Serialize, Deserialize, Clone)] pub struct ProcessingTagsConfig { - include: Vec, - exclude: Vec, + include: Option>, + exclude: Option>, } #[derive(Debug, Serialize, Deserialize, Clone)] @@ -77,11 +90,18 @@ pub struct CountryRewriteConfig { } #[derive(Debug, Serialize, Deserialize, Clone)] -pub struct Country { +pub struct PublicConfig { name: String, - description: String, - foundation_date: String, - flag: String, - about: Option, - tags: Option>, + geo: String, + countries: 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, + pub tags: Option>, }