From c75551d3637abd5295138bb0cf4e621c19bd6cd6 Mon Sep 17 00:00:00 2001 From: Zakaria Mansouri Date: Thu, 21 Nov 2024 14:04:59 +0100 Subject: [PATCH] fix: set default feature to `static` and skipped build.rs if no `static` is selected --- .github/workflows/rust-checks.yml | 1 + rust/Cargo.toml | 2 +- rust/build.rs | 201 ++++++++++++++++-------------- rust/src/static/node/mod.rs | 1 + 4 files changed, 107 insertions(+), 98 deletions(-) diff --git a/.github/workflows/rust-checks.yml b/.github/workflows/rust-checks.yml index 70966c2..3467e89 100644 --- a/.github/workflows/rust-checks.yml +++ b/.github/workflows/rust-checks.yml @@ -29,3 +29,4 @@ jobs: - run: cd rust && cargo test --verbose - run: cd rust && cargo test --verbose --features serde_derive - run: cd rust && cargo test --verbose --no-default-features --features storage + - run: cd rust && cargo test --verbose --all-features diff --git a/rust/Cargo.toml b/rust/Cargo.toml index 06f6183..b1a9725 100644 --- a/rust/Cargo.toml +++ b/rust/Cargo.toml @@ -4,7 +4,7 @@ version = "0.1.0" edition = "2021" [features] -default = ["static", "serde_derive", "storage"] +default = ["static"] static = [] serde_derive = ["dep:serde", "dep:serde_json"] storage = ["serde_derive"] diff --git a/rust/build.rs b/rust/build.rs index 2451401..aaa3695 100644 --- a/rust/build.rs +++ b/rust/build.rs @@ -1,26 +1,27 @@ -#![cfg(feature = "static")] -use serde_json::Value; -use std::{fs, io, path::Path}; +#[cfg(feature = "static")] +mod r#static { + use serde_json::Value; + use std::{fs, io, path::Path}; -fn dir_tree_to_list(dir: impl AsRef) -> (String, String) { - let info_path = dir.as_ref().join("info.json"); - let info_dot_json = match info_path.exists() { - true => { - let info = fs::read_to_string(&info_path).unwrap(); - let info: Value = serde_json::from_str(info.as_str()).unwrap(); - let mut info = info.as_object().unwrap().clone(); - info.remove("$schema"); - Some(Value::Object(info)) - } - false => None, - }; - let this_node = match &info_dot_json { - Some(info) => { - let path = dir.as_ref().display().to_string(); - let path = path.split("_data/").last().unwrap_or(&path); + fn dir_tree_to_list(dir: impl AsRef) -> (String, String) { + let info_path = dir.as_ref().join("info.json"); + let info_dot_json = match info_path.exists() { + true => { + let info = fs::read_to_string(&info_path).unwrap(); + let info: Value = serde_json::from_str(info.as_str()).unwrap(); + let mut info = info.as_object().unwrap().clone(); + info.remove("$schema"); + Some(Value::Object(info)) + } + false => None, + }; + let this_node = match &info_dot_json { + Some(info) => { + let path = dir.as_ref().display().to_string(); + let path = path.split("_data/").last().unwrap_or(&path); - format!( - r#"const {}: Node = Node {{ + format!( + r#"const {}: Node = Node {{ name: NodeName {{ en: {}, ar: {}, @@ -29,87 +30,87 @@ fn dir_tree_to_list(dir: impl AsRef) -> (String, String) { r#type: {}, }}; "#, - path.replace('/', "_").to_uppercase(), - info.get("name").unwrap().get("en").unwrap(), - info.get("name").unwrap().get("ar").unwrap(), - info.get("name").unwrap().get("fr").unwrap(), - match &info.get("type").unwrap() { - Value::String(s) => { - let ty = s - .chars() - .enumerate() - .map(|(i, c)| { - if i == 0 { - c.to_uppercase().to_string() - } else { - c.to_lowercase().to_string() - } - }) - .collect::(); + path.replace('/', "_").to_uppercase(), + info.get("name").unwrap().get("en").unwrap(), + info.get("name").unwrap().get("ar").unwrap(), + info.get("name").unwrap().get("fr").unwrap(), + match &info.get("type").unwrap() { + Value::String(s) => { + let ty = s + .chars() + .enumerate() + .map(|(i, c)| { + if i == 0 { + c.to_uppercase().to_string() + } else { + c.to_lowercase().to_string() + } + }) + .collect::(); - match ty.as_str() { - "Specialty" | "Sector" => format!( - r#"NodeType::{}{{ + match ty.as_str() { + "Specialty" | "Sector" => format!( + r#"NodeType::{}{{ terms: NodeTerms {{ per_year: 2, slots: &[7, 8, 9, 10], }}, }}"#, - ty - ), + ty + ), - _ => format!("NodeType::{}", ty), + _ => format!("NodeType::{}", ty), + } } + _ => "".to_string(), } - _ => "".to_string(), - } - ) - } - None => String::new(), - }; + ) + } + None => String::new(), + }; - let this_match = match &info_dot_json { - Some(_) => { - let path = dir.as_ref().display().to_string(); - let path = path.split("_data/").last().unwrap_or(&path); - format!( - " \"{}\" => Some(&{}),\n", - path, - path.replace('/', "_").to_uppercase() - ) - } - None => String::new(), - }; + let this_match = match &info_dot_json { + Some(_) => { + let path = dir.as_ref().display().to_string(); + let path = path.split("_data/").last().unwrap_or(&path); + format!( + " \"{}\" => Some(&{}),\n", + path, + path.replace('/', "_").to_uppercase() + ) + } + None => String::new(), + }; - let sub_dirs = fs::read_dir(&dir).unwrap(); - let children = sub_dirs.filter_map(|entry| { - let entry = entry.unwrap(); - let ty = entry.file_type().unwrap(); - if ty.is_dir() { - Some(dir_tree_to_list(entry.path())) - } else { - None - } - }); + let sub_dirs = fs::read_dir(&dir).unwrap(); + let children = sub_dirs.filter_map(|entry| { + let entry = entry.unwrap(); + let ty = entry.file_type().unwrap(); + if ty.is_dir() { + Some(dir_tree_to_list(entry.path())) + } else { + None + } + }); - let mut constants = String::new(); - let mut matches = String::new(); - children.for_each(|(c, m)| { - constants.push_str(&c); - matches.push_str(&m); - }); + let mut constants = String::new(); + let mut matches = String::new(); + children.for_each(|(c, m)| { + constants.push_str(&c); + matches.push_str(&m); + }); - ( - format!("{}{}", this_node, constants), - format!("{}{}", this_match, matches), - ) -} + ( + format!("{}{}", this_node, constants), + format!("{}{}", this_match, matches), + ) + } -fn generate_data_file() -> Result<(), io::Error> { - let string_tree = dir_tree_to_list("../_data"); + fn generate_data_file() -> Result<(), io::Error> { + let string_tree = dir_tree_to_list("../_data"); - let data = format!( - r##"// This is auto-generated. Do not edit manually. + let data = format!( + r##"// This is auto-generated. Do not edit manually. use super::super::node::{{Node, NodeName, NodeType, NodeTerms}}; @@ -119,19 +120,25 @@ pub fn get_node_by_path(path: &str) -> Option<&Node> {{ {} _ => None, }} }}"##, - string_tree.0, string_tree.1 - ); - fs::create_dir_all("./src/static/_auto_generated")?; - fs::write("./src/static/_auto_generated/data.rs", data)?; - fs::write( - "./src/static/_auto_generated/mod.rs", - r#"// This is auto-generated. Do not edit manually + string_tree.0, string_tree.1 + ); + fs::create_dir_all("./src/static/_auto_generated")?; + fs::write("./src/static/_auto_generated/data.rs", data)?; + fs::write( + "./src/static/_auto_generated/mod.rs", + r#"// This is auto-generated. Do not edit manually pub mod data; "#, - )?; - Ok(()) + )?; + Ok(()) + } + + pub fn main() { + generate_data_file().unwrap(); + } } fn main() { - generate_data_file().unwrap(); + #[cfg(feature = "static")] + r#static::main(); } diff --git a/rust/src/static/node/mod.rs b/rust/src/static/node/mod.rs index e909a39..20d53d3 100644 --- a/rust/src/static/node/mod.rs +++ b/rust/src/static/node/mod.rs @@ -1,3 +1,4 @@ +#[cfg(feature = "serde_derive")] use serde::Serialize; #[derive(Debug, PartialEq)]