Skip to content

Commit

Permalink
chore: address PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
omdxp committed Jan 12, 2024
1 parent 43aaaf7 commit 811736f
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 208 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/rust-checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ jobs:
- beta
- nightly
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- run: rustup update ${{ matrix.toolchain }} && rustup default ${{ matrix.toolchain }}
- run: cd rust && cargo build --verbose
- run: cd rust && cargo test --verbose
- run: cd rust && cargo test --verbose --no-default-features --features const
- run: cd rust && cargo test --verbose --features serde_derive storage
- run: cd rust && cargo test --verbose --no-default-features --features storage
243 changes: 81 additions & 162 deletions rust/src/api/get_node_by_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,168 +5,104 @@ 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(data_path: &'static str, path: &str) -> Result<Node, std::io::Error> {
storage::get_node_by_path(data_path, path)
pub fn get_node_by_path(path: impl AsRef<Path>) -> Result<Node, storage::StorageError> {
storage::get_node_by_path(path)
}

#[cfg(any(feature = "const", feature = "storage"))]
#[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(
#[cfg(feature = "const")]&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,
},
#[cfg(feature = "storage")]
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,
}
),
Some(&umkb),
#[cfg(feature = "serde_derive")]
"{\"name\":{\"ar\":\"جامعة محمد خيضر بسكرة\",\"en\":\"University of Mohamed Khider Biskra\",\"fr\":\"Université Mohamed Khider Biskra\"},\"type\":\"UNIVERSITY\"}",
),
(
"umkb/fst",
Some(
#[cfg(feature = "const")]
&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,
},
#[cfg(feature = "storage")]
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,
}
),
Some(&fst),
#[cfg(feature = "serde_derive")]
"{\"name\":{\"ar\":\"كلية العلوم والتكنلوجيا\",\"en\":\"Faculty of Science and Technology\",\"fr\":\"Faculté des Sciences et de la Technologie\"},\"type\":\"FACULTY\"}",
),
(
"umkb/fst/dee/sec",
Some(
#[cfg(feature = "const")]
&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],
},
},
},
#[cfg(feature = "storage")]
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],
},
},
}
),
Some(&sec),
#[cfg(feature = "serde_derive")]
"{\"name\":{\"ar\":\"تخصص التحكم الكهربائي\",\"en\":\"Specialy of Electrical Control\",\"fr\":\"Spécialité de commande électrique\"},\"type\":\"SPECIALTY\",\"terms\":{\"perYear\":2,\"slots\":[7,8,9,10]}}",
),
Expand All @@ -177,42 +113,25 @@ mod test {
),
];

for tc in tests {
let path = tc.0;
let expected = tc.1;
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);
assert_eq!(actual, expected);
}
let actual = get_node_by_path(path);
#[cfg(feature = "storage")]
{
let actual = get_node_by_path("../_data", path).ok();
assert_eq!(actual, expected);
}
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")]
{
let expected_stringified = tc.2;
let actual = get_node_by_path("../_data", path).ok();
let expected_stringified = test_case.2;
assert_eq!(
serde_json::to_string(&actual).unwrap(),
expected_stringified
);
}
}
}

#[test]
fn should_get_none_when_path_does_not_exist() {
#[cfg(feature = "const")]
{
let res = get_node_by_path("does/not/exist");
assert!(res.is_none());
}
#[cfg(feature = "storage")]
{
let res = get_node_by_path("../_data", "does/not/exist");
assert!(res.is_err());
}
}
}
}
3 changes: 2 additions & 1 deletion rust/src/api/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod get_node_by_path;
mod storage;
#[cfg(feature = "storage")]
pub mod storage;
24 changes: 12 additions & 12 deletions rust/src/api/storage.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
#[cfg(feature = "storage")]
#![cfg(feature = "storage")]

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

#[cfg(feature = "storage")]
pub fn get_node_by_path(data_path: &'static str, path: &str) -> Result<Node, std::io::Error> {
let fs_path = format!("{}/{}/info.json", data_path, path);
let info = std::fs::read_to_string(fs_path)?;
let Ok(node) = serde_json::from_str::<Node>(info.as_str()) else {
return Err(std::io::Error::new(
std::io::ErrorKind::Other,
"json was not deserialized",
));
};
Ok(node)
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)
}
5 changes: 3 additions & 2 deletions rust/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#[cfg(feature = "const")]
mod _auto_generated;

mod api;
mod node;

pub use api::get_node_by_path::get_node_by_path;
pub use node::model::{Node, NodeName, NodeTerms, NodeType};
#[cfg(feature = "storage")]
pub use api::storage::StorageError;
pub use node::model::{Node, NodeName, NodeTerms, NodeType};
2 changes: 1 addition & 1 deletion rust/src/node/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
pub mod model;
pub mod model;
Loading

0 comments on commit 811736f

Please sign in to comment.