Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/rust-checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
- run: cargo build --verbose
working-directory: ${{ env.CWD }}
- name: "is docs updated and generated files are committed"
run: exit $(git status --porcelain | wc -l)
run: git diff --exit-code
- run: cargo test --verbose
working-directory: ${{ env.CWD }}
- run: cargo test --verbose --lib --bins --tests --features serde_derive
Expand Down
9 changes: 9 additions & 0 deletions _data/ubma/info.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"$schema": "https://raw.githubusercontent.com/dzcode-io/kuliya/main/_schemas/l1.json",
"name": {
"ar": "جامعة باجي مختار عنابة",
"en": "University of Badji Mokhtar Annaba",
"fr": "Université Badji Mokhtar Annaba"
},
"type": "UNIVERSITY"
}
67 changes: 37 additions & 30 deletions rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions rust/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "kuliya"
description = "A library for querying Algerian education dataset."
version = "0.1.2"
version = "0.1.3"
edition = "2021"
license = "MIT"

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

[dependencies]
nest_struct = "0.5.2"
serde = { version = "1.0.194", features = ["derive"], optional = true }
serde_json = { version = "1.0.110", optional = true }
nest_struct = "0.5"
serde = { version = "1", features = ["derive"], optional = true }
serde_json = { version = "1", optional = true }

[build-dependencies]
serde = { version = "1.0.194", features = ["derive"] }
serde_json = { version = "1.0.110", features = ["preserve_order"] }
serde = { version = "1", features = ["derive"] }
serde_json = { version = "1", features = ["preserve_order"] }
78 changes: 61 additions & 17 deletions rust/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,33 @@ mod r#static {
use serde_json::Value;
use std::{fs, io, path::Path};

fn dir_tree_to_list(dir: impl AsRef<Path>) -> (String, String) {
fn dir_tree_to_list(dir: impl AsRef<Path>) -> (String, String, String) {
let info_path = dir.as_ref().join("info.json");
let info_dot_json = match info_path.exists() {
let node_dir = 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))

let path = dir.as_ref().display().to_string();
Some((
path.split("_data/").last().unwrap_or(&path).to_string(),
Value::Object(info),
))
}
false => None,
};
let this_node = match &info_dot_json {
Some(info) => {

let this_node = match &node_dir {
Some((_, info)) => {
let path = dir.as_ref().display().to_string();
let path = path.split("_data/").last().unwrap_or(&path);
let code = path.split('/').next_back().unwrap_or(path).to_lowercase();

format!(
r#"const {}: Node = Node {{
code: "{}",
name: NodeName {{
en: {},
ar: {},
Expand All @@ -31,6 +39,7 @@ mod r#static {
}};
"#,
path.replace('/', "_").to_uppercase(),
code,
info.get("name").unwrap().get("en").unwrap(),
info.get("name").unwrap().get("ar").unwrap(),
info.get("name").unwrap().get("fr").unwrap(),
Expand All @@ -50,7 +59,7 @@ mod r#static {

match ty.as_str() {
"Specialty" | "Sector" => format!(
r#"NodeType::{}{{
r#"NodeType::{} {{
terms: NodeTerms {{
per_year: 2,
slots: &[7, 8, 9, 10],
Expand All @@ -69,10 +78,8 @@ mod r#static {
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);
let this_match = match &node_dir {
Some((path, _)) => {
format!(
" \"{}\" => Some(&{}),\n",
path,
Expand All @@ -83,12 +90,22 @@ mod r#static {
};

let sub_dirs = fs::read_dir(&dir).unwrap();
let mut children: Vec<(String, String)> = sub_dirs
let mut children_names = Vec::new();
let mut children: Vec<(String, String, String)> = 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()))
let path = entry.path();
children_names.push(
path.display()
.to_string()
.split("_data/")
.last()
.map(|s| s.replace('/', "_").to_uppercase())
.unwrap(),
);
Some(dir_tree_to_list(path))
} else {
None
}
Expand All @@ -97,16 +114,36 @@ mod r#static {
// to ensure deterministic output on different platforms
children.sort();

let mut children_names = children_names
.iter()
.map(|name| format!("&{}", name))
.collect::<Vec<String>>();
// to ensure deterministic output on different platforms
children_names.sort();

let this_children_match = format!(
r#" "{}" => vec![{}],
"#,
match &node_dir {
Some((path, _)) => path,
None => "",
},
children_names.join(", ")
);

let mut constants = String::new();
let mut matches = String::new();
for (c, m) in children {
constants.push_str(&c);
matches.push_str(&m);
let mut children_matches = String::new();
for (c, m, chm) in &children {
constants.push_str(c);
matches.push_str(m);
children_matches.push_str(chm);
}

(
format!("{}{}", this_node, constants),
format!("{}{}", this_match, matches),
format!("{}{}", this_children_match, children_matches),
)
}

Expand All @@ -123,8 +160,15 @@ pub fn get_node_by_path(path: &str) -> Option<&Node> {{
match path {{
{} _ => None,
}}
}}"##,
string_tree.0, string_tree.1
}}

pub fn get_node_children_by_path(path: &str) -> Vec<&Node> {{
match path {{
{} _ => vec![],
}}
}}
"##,
string_tree.0, string_tree.1, string_tree.2
);
fs::create_dir_all("./src/static/_auto_generated")?;
fs::write("./src/static/_auto_generated/data.rs", data)?;
Expand Down
Loading