Skip to content

Commit

Permalink
Add storage feature
Browse files Browse the repository at this point in the history
  • Loading branch information
ZibanPirate committed Jan 11, 2024
1 parent 1c4b2b7 commit ad4fc90
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 40 deletions.
1 change: 1 addition & 0 deletions .github/workflows/rust-checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ jobs:
- run: cd rust && cargo build --verbose
- 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
1 change: 1 addition & 0 deletions rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ edition = "2021"
default = ["const"]
const = []
serde_derive = ["dep:serde", "dep:serde_json"]
storage = ["serde_derive"]

[dependencies]
serde = { version = "1.0.194", features = ["derive"], optional = true }
Expand Down
124 changes: 89 additions & 35 deletions rust/src/api/get_node_by_path.rs
Original file line number Diff line number Diff line change
@@ -1,62 +1,110 @@
use crate::{_auto_generated, node::model::Node};
use crate::node::model::Node;

#[cfg(feature = "const")]
use crate::_auto_generated;

#[cfg(feature = "storage")]
use crate::api::storage;
#[cfg(feature = "storage")]
use std::path::Path;

#[cfg(feature = "const")]
pub fn get_node_by_path(path: &str) -> Option<&Node> {
_auto_generated::data::get_node_by_path(path)
}

#[cfg(feature = "storage")]
pub fn get_node_by_path(path: impl AsRef<Path>) -> Result<Node, storage::StorageError> {
storage::get_node_by_path(path)
}

#[cfg(test)]
mod test {
use crate::node::model::{Node, NodeName, NodeTerms, NodeType};

use super::get_node_by_path;
use crate::node::model::{Node, NodeName, NodeTerms, NodeType};
#[cfg(feature = "storage")]
use std::path::Path;

#[test]
fn check_three_schemas_and_non_existent() {
let umkb = Node {
name: NodeName {
#[cfg(feature = "const")]
ar: "جامعة محمد خيضر بسكرة",
#[cfg(feature = "storage")]
ar: "جامعة محمد خيضر بسكرة".to_string(),
#[cfg(feature = "const")]
en: "University of Mohamed Khider Biskra",
#[cfg(feature = "storage")]
en: "University of Mohamed Khider Biskra".to_string(),
#[cfg(feature = "const")]
fr: "Université Mohamed Khider Biskra",
#[cfg(feature = "storage")]
fr: "Université Mohamed Khider Biskra".to_string(),
},
r#type: NodeType::University,
};
let sec = Node {
name: NodeName {
#[cfg(feature = "const")]
ar: "تخصص التحكم الكهربائي",
#[cfg(feature = "storage")]
ar: "تخصص التحكم الكهربائي".to_string(),
#[cfg(feature = "const")]
en: "Specialy of Electrical Control",
#[cfg(feature = "storage")]
en: "Specialy of Electrical Control".to_string(),
#[cfg(feature = "const")]
fr: "Spécialité de commande électrique",
#[cfg(feature = "storage")]
fr: "Spécialité de commande électrique".to_string(),
},
r#type: NodeType::Specialty {
terms: NodeTerms {
per_year: 2,
#[cfg(feature = "const")]
slots: &[7, 8, 9, 10],
#[cfg(feature = "storage")]
slots: vec![7, 8, 9, 10],
},
},
};
let fst = Node {
name: NodeName {
#[cfg(feature = "const")]
ar: "كلية العلوم والتكنلوجيا",
#[cfg(feature = "storage")]
ar: "كلية العلوم والتكنلوجيا".to_string(),
#[cfg(feature = "const")]
en: "Faculty of Science and Technology",
#[cfg(feature = "storage")]
en: "Faculty of Science and Technology".to_string(),
#[cfg(feature = "const")]
fr: "Faculté des Sciences et de la Technologie",
#[cfg(feature = "storage")]
fr: "Faculté des Sciences et de la Technologie".to_string(),
},
r#type: NodeType::Faculty,
};

let tests = vec![
(
"umkb",
Some(&Node {
name: NodeName {
ar: "جامعة محمد خيضر بسكرة",
en: "University of Mohamed Khider Biskra",
fr: "Université Mohamed Khider Biskra",
},
r#type: NodeType::University,
}),
Some(&umkb),
#[cfg(feature = "serde_derive")]
"{\"name\":{\"ar\":\"جامعة محمد خيضر بسكرة\",\"en\":\"University of Mohamed Khider Biskra\",\"fr\":\"Université Mohamed Khider Biskra\"},\"type\":\"University\"}",
"{\"name\":{\"ar\":\"جامعة محمد خيضر بسكرة\",\"en\":\"University of Mohamed Khider Biskra\",\"fr\":\"Université Mohamed Khider Biskra\"},\"type\":\"UNIVERSITY\"}",
),
(
"umkb/fst",
Some(&Node {
name: NodeName {
ar: "كلية العلوم والتكنلوجيا",
en: "Faculty of Science and Technology",
fr: "Faculté des Sciences et de la Technologie",
},
r#type: NodeType::Faculty,
}),
Some(&fst),
#[cfg(feature = "serde_derive")]
"{\"name\":{\"ar\":\"كلية العلوم والتكنلوجيا\",\"en\":\"Faculty of Science and Technology\",\"fr\":\"Faculté des Sciences et de la Technologie\"},\"type\":\"Faculty\"}",
"{\"name\":{\"ar\":\"كلية العلوم والتكنلوجيا\",\"en\":\"Faculty of Science and Technology\",\"fr\":\"Faculté des Sciences et de la Technologie\"},\"type\":\"FACULTY\"}",
),
(
"umkb/fst/dee/sec",
Some(&Node {
name: NodeName {
ar: "تخصص التحكم الكهربائي",
en: "Specialy of Electrical Control",
fr: "Spécialité de commande électrique",
},
r#type: NodeType::Specialty {
terms: NodeTerms {
per_year: 2,
slots: &[7, 8, 9, 10],
},
},
}),
Some(&sec),
#[cfg(feature = "serde_derive")]
"{\"name\":{\"ar\":\"تخصص التحكم الكهربائي\",\"en\":\"Specialy of Electrical Control\",\"fr\":\"Spécialité de commande électrique\"},\"type\":{\"Specialty\":{\"terms\":{\"per_year\":2,\"slots\":[7,8,9,10]}}}}",
"{\"name\":{\"ar\":\"تخصص التحكم الكهربائي\",\"en\":\"Specialy of Electrical Control\",\"fr\":\"Spécialité de commande électrique\"},\"type\":\"SPECIALTY\",\"terms\":{\"perYear\":2,\"slots\":[7,8,9,10]}}",
),
(
"does/not/exist", None,
Expand All @@ -68,7 +116,13 @@ mod test {
for test_case in tests {
let path = test_case.0;
let expected = test_case.1;
#[cfg(feature = "const")]
let actual = get_node_by_path(path);
#[cfg(feature = "storage")]
let actual = get_node_by_path(Path::new("../_data").join(path)).ok();
#[cfg(feature = "storage")]
let actual: Option<&Node> = actual.as_ref();

assert_eq!(actual, expected);
#[cfg(feature = "serde_derive")]
{
Expand Down
2 changes: 2 additions & 0 deletions rust/src/api/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
pub mod get_node_by_path;
#[cfg(feature = "storage")]
pub mod storage;
15 changes: 15 additions & 0 deletions rust/src/api/storage.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#![cfg(feature = "storage")]

use crate::Node;
use std::path::Path;

pub enum StorageError {
Io(std::io::Error),
Json(serde_json::Error),
}

pub fn get_node_by_path(path: impl AsRef<Path>) -> Result<Node, StorageError> {
let json_file_path = path.as_ref().join("info.json");
let json_file_content = std::fs::read_to_string(json_file_path).map_err(StorageError::Io)?;
serde_json::from_str::<Node>(json_file_content.as_str()).map_err(StorageError::Json)
}
3 changes: 3 additions & 0 deletions rust/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#[cfg(feature = "const")]
mod _auto_generated;
mod api;
mod node;

pub use api::get_node_by_path::get_node_by_path;
#[cfg(feature = "storage")]
pub use api::storage::StorageError;
pub use node::model::{Node, NodeName, NodeTerms, NodeType};
36 changes: 31 additions & 5 deletions rust/src/node/model.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,35 @@
#[cfg(feature = "storage")]
use serde::Deserialize;
#[cfg(feature = "serde_derive")]
use serde::Serialize;

#[derive(Debug, PartialEq)]
#[cfg_attr(feature = "serde_derive", derive(Serialize))]
#[cfg(feature = "const")]
pub struct NodeName {
#[cfg(feature = "const")]
pub ar: &'static str,
#[cfg(feature = "const")]
pub en: &'static str,
#[cfg(feature = "const")]
pub fr: &'static str,
}

#[derive(Debug, PartialEq)]
#[cfg_attr(feature = "serde_derive", derive(Serialize))]
#[cfg_attr(feature = "storage", derive(Serialize))]
#[cfg_attr(feature = "storage", derive(Deserialize))]
#[cfg(feature = "storage")]
pub struct NodeName {
pub ar: String,
pub en: String,
pub fr: String,
}

#[derive(Debug, PartialEq)]
#[cfg_attr(
feature = "serde_derive",
derive(Serialize),
serde(tag = "type"),
serde(rename_all = "UPPERCASE")
)]
#[cfg_attr(feature = "storage", derive(Deserialize))]
pub enum NodeType {
University,
Academy,
Expand All @@ -26,16 +42,26 @@ pub enum NodeType {
}

#[derive(Debug, PartialEq, Clone)]
#[cfg_attr(feature = "serde_derive", derive(Serialize))]
#[cfg_attr(
feature = "serde_derive",
derive(Serialize),
serde(rename_all = "camelCase")
)]
#[cfg_attr(feature = "storage", derive(Deserialize))]
pub struct NodeTerms {
pub per_year: usize,
#[cfg(feature = "const")]
pub slots: &'static [i32],
#[cfg(feature = "storage")]
pub slots: Vec<i32>,
}

#[derive(Debug, PartialEq)]
#[cfg_attr(feature = "serde_derive", derive(Serialize))]
#[cfg_attr(feature = "storage", derive(Deserialize))]

pub struct Node {
pub name: NodeName,
#[cfg_attr(feature = "serde_derive", serde(flatten))]
pub r#type: NodeType,
}

0 comments on commit ad4fc90

Please sign in to comment.