Skip to content

Commit 15f1247

Browse files
authored
Merge pull request #110 from dzcode-io/feat/get_children_by_path
2 parents 6ca5f1a + f4225c6 commit 15f1247

File tree

8 files changed

+565
-60
lines changed

8 files changed

+565
-60
lines changed

.github/workflows/rust-checks.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ jobs:
3030
- run: cargo build --verbose
3131
working-directory: ${{ env.CWD }}
3232
- name: "is docs updated and generated files are committed"
33-
run: exit $(git status --porcelain | wc -l)
33+
run: git diff --exit-code
3434
- run: cargo test --verbose
3535
working-directory: ${{ env.CWD }}
3636
- run: cargo test --verbose --lib --bins --tests --features serde_derive

_data/ubma/info.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"$schema": "https://raw.githubusercontent.com/dzcode-io/kuliya/main/_schemas/l1.json",
3+
"name": {
4+
"ar": "جامعة باجي مختار عنابة",
5+
"en": "University of Badji Mokhtar Annaba",
6+
"fr": "Université Badji Mokhtar Annaba"
7+
},
8+
"type": "UNIVERSITY"
9+
}

rust/Cargo.lock

Lines changed: 37 additions & 30 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust/Cargo.toml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "kuliya"
33
description = "A library for querying Algerian education dataset."
4-
version = "0.1.2"
4+
version = "0.1.3"
55
edition = "2021"
66
license = "MIT"
77

@@ -15,10 +15,10 @@ serde_derive = ["dep:serde", "dep:serde_json"]
1515
storage = ["serde_derive"]
1616

1717
[dependencies]
18-
nest_struct = "0.5.2"
19-
serde = { version = "1.0.194", features = ["derive"], optional = true }
20-
serde_json = { version = "1.0.110", optional = true }
18+
nest_struct = "0.5"
19+
serde = { version = "1", features = ["derive"], optional = true }
20+
serde_json = { version = "1", optional = true }
2121

2222
[build-dependencies]
23-
serde = { version = "1.0.194", features = ["derive"] }
24-
serde_json = { version = "1.0.110", features = ["preserve_order"] }
23+
serde = { version = "1", features = ["derive"] }
24+
serde_json = { version = "1", features = ["preserve_order"] }

rust/build.rs

Lines changed: 61 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,33 @@ mod r#static {
33
use serde_json::Value;
44
use std::{fs, io, path::Path};
55

6-
fn dir_tree_to_list(dir: impl AsRef<Path>) -> (String, String) {
6+
fn dir_tree_to_list(dir: impl AsRef<Path>) -> (String, String, String) {
77
let info_path = dir.as_ref().join("info.json");
8-
let info_dot_json = match info_path.exists() {
8+
let node_dir = match info_path.exists() {
99
true => {
1010
let info = fs::read_to_string(&info_path).unwrap();
1111
let info: Value = serde_json::from_str(info.as_str()).unwrap();
1212
let mut info = info.as_object().unwrap().clone();
1313
info.remove("$schema");
14-
Some(Value::Object(info))
14+
15+
let path = dir.as_ref().display().to_string();
16+
Some((
17+
path.split("_data/").last().unwrap_or(&path).to_string(),
18+
Value::Object(info),
19+
))
1520
}
1621
false => None,
1722
};
18-
let this_node = match &info_dot_json {
19-
Some(info) => {
23+
24+
let this_node = match &node_dir {
25+
Some((_, info)) => {
2026
let path = dir.as_ref().display().to_string();
2127
let path = path.split("_data/").last().unwrap_or(&path);
28+
let code = path.split('/').next_back().unwrap_or(path).to_lowercase();
2229

2330
format!(
2431
r#"const {}: Node = Node {{
32+
code: "{}",
2533
name: NodeName {{
2634
en: {},
2735
ar: {},
@@ -31,6 +39,7 @@ mod r#static {
3139
}};
3240
"#,
3341
path.replace('/', "_").to_uppercase(),
42+
code,
3443
info.get("name").unwrap().get("en").unwrap(),
3544
info.get("name").unwrap().get("ar").unwrap(),
3645
info.get("name").unwrap().get("fr").unwrap(),
@@ -50,7 +59,7 @@ mod r#static {
5059

5160
match ty.as_str() {
5261
"Specialty" | "Sector" => format!(
53-
r#"NodeType::{}{{
62+
r#"NodeType::{} {{
5463
terms: NodeTerms {{
5564
per_year: 2,
5665
slots: &[7, 8, 9, 10],
@@ -69,10 +78,8 @@ mod r#static {
6978
None => String::new(),
7079
};
7180

72-
let this_match = match &info_dot_json {
73-
Some(_) => {
74-
let path = dir.as_ref().display().to_string();
75-
let path = path.split("_data/").last().unwrap_or(&path);
81+
let this_match = match &node_dir {
82+
Some((path, _)) => {
7683
format!(
7784
" \"{}\" => Some(&{}),\n",
7885
path,
@@ -83,12 +90,22 @@ mod r#static {
8390
};
8491

8592
let sub_dirs = fs::read_dir(&dir).unwrap();
86-
let mut children: Vec<(String, String)> = sub_dirs
93+
let mut children_names = Vec::new();
94+
let mut children: Vec<(String, String, String)> = sub_dirs
8795
.filter_map(|entry| {
8896
let entry = entry.unwrap();
8997
let ty = entry.file_type().unwrap();
9098
if ty.is_dir() {
91-
Some(dir_tree_to_list(entry.path()))
99+
let path = entry.path();
100+
children_names.push(
101+
path.display()
102+
.to_string()
103+
.split("_data/")
104+
.last()
105+
.map(|s| s.replace('/', "_").to_uppercase())
106+
.unwrap(),
107+
);
108+
Some(dir_tree_to_list(path))
92109
} else {
93110
None
94111
}
@@ -97,16 +114,36 @@ mod r#static {
97114
// to ensure deterministic output on different platforms
98115
children.sort();
99116

117+
let mut children_names = children_names
118+
.iter()
119+
.map(|name| format!("&{}", name))
120+
.collect::<Vec<String>>();
121+
// to ensure deterministic output on different platforms
122+
children_names.sort();
123+
124+
let this_children_match = format!(
125+
r#" "{}" => vec![{}],
126+
"#,
127+
match &node_dir {
128+
Some((path, _)) => path,
129+
None => "",
130+
},
131+
children_names.join(", ")
132+
);
133+
100134
let mut constants = String::new();
101135
let mut matches = String::new();
102-
for (c, m) in children {
103-
constants.push_str(&c);
104-
matches.push_str(&m);
136+
let mut children_matches = String::new();
137+
for (c, m, chm) in &children {
138+
constants.push_str(c);
139+
matches.push_str(m);
140+
children_matches.push_str(chm);
105141
}
106142

107143
(
108144
format!("{}{}", this_node, constants),
109145
format!("{}{}", this_match, matches),
146+
format!("{}{}", this_children_match, children_matches),
110147
)
111148
}
112149

@@ -123,8 +160,15 @@ pub fn get_node_by_path(path: &str) -> Option<&Node> {{
123160
match path {{
124161
{} _ => None,
125162
}}
126-
}}"##,
127-
string_tree.0, string_tree.1
163+
}}
164+
165+
pub fn get_node_children_by_path(path: &str) -> Vec<&Node> {{
166+
match path {{
167+
{} _ => vec![],
168+
}}
169+
}}
170+
"##,
171+
string_tree.0, string_tree.1, string_tree.2
128172
);
129173
fs::create_dir_all("./src/static/_auto_generated")?;
130174
fs::write("./src/static/_auto_generated/data.rs", data)?;

0 commit comments

Comments
 (0)