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 34d4f56
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 200 deletions.
1 change: 0 additions & 1 deletion .github/workflows/rust-checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,4 @@ jobs:
- 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 --no-default-features --features storage
2 changes: 1 addition & 1 deletion rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ version = "0.1.0"
edition = "2021"

[features]
default = ["const"]
default = ["storage"]
const = []
serde_derive = ["dep:serde", "dep:serde_json"]
storage = ["serde_derive"]
Expand Down
236 changes: 79 additions & 157 deletions rust/src/api/get_node_by_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,167 +6,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 @@ -179,40 +116,25 @@ mod test {

for tc in tests {
let path = tc.0;
#[cfg(any(feature = "const", feature = "storage"))]
let expected = tc.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 = actual.as_ref();

#[cfg(any(feature = "const", feature = "storage"))]
assert_eq!(actual, expected);
#[cfg(feature = "serde_derive")]
{
let expected_stringified = tc.2;
let actual = get_node_by_path("../_data", path).ok();
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());
}
}
}
2 changes: 1 addition & 1 deletion rust/src/api/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
pub mod get_node_by_path;
mod storage;
pub mod storage;
22 changes: 10 additions & 12 deletions rust/src/api/storage.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
#[cfg(feature = "storage")]
#![cfg(feature = "storage")]
use crate::Node;

#[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<std::path::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: 2 additions & 1 deletion 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;
#[cfg(feature = "storage")]
pub use api::storage::StorageError;
pub use node::model::{Node, NodeName, NodeTerms, NodeType};
Loading

0 comments on commit 34d4f56

Please sign in to comment.