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 11, 2024
1 parent 43aaaf7 commit 44625c8
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 75 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
84 changes: 15 additions & 69 deletions rust/src/api/get_node_by_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ pub fn get_node_by_path(path: &str) -> Option<&Node> {
}

#[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: &str) -> Result<Node, std::io::Error> {
storage::get_node_by_path(path)
}

#[cfg(any(feature = "const", feature = "storage"))]
Expand All @@ -31,35 +31,17 @@ mod test {
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,
Expand All @@ -74,35 +56,17 @@ mod test {
#[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,
Expand All @@ -117,18 +81,9 @@ mod test {
#[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 {
Expand All @@ -143,17 +98,8 @@ mod test {
#[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 {
Expand Down Expand Up @@ -185,19 +131,19 @@ mod test {
let actual = get_node_by_path(path);
assert_eq!(actual, expected);
}
#[cfg(feature = "storage")]
#[cfg(any(feature = "storage", feature = "serde_derive"))]
{
let actual = get_node_by_path("../_data", path).ok();
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
);
let actual = get_node_by_path(format!("../_data/{}", path).as_str()).ok();
if cfg!(feature = "storage") {
assert_eq!(actual, expected);
}
if cfg!(feature = "serde_derive") {
let expected_stringified = tc.2;
assert_eq!(
serde_json::to_string(&actual).unwrap(),
expected_stringified
);
}
}
}
}
Expand All @@ -211,7 +157,7 @@ mod test {
}
#[cfg(feature = "storage")]
{
let res = get_node_by_path("../_data", "does/not/exist");
let res = get_node_by_path("does/not/exist");
assert!(res.is_err());
}
}
Expand Down
5 changes: 3 additions & 2 deletions rust/src/api/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
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);
pub fn get_node_by_path(path: impl AsRef<std::path::Path>) -> Result<Node, std::io::Error> {
let fs_path = format!("{}/info.json", path.as_ref().to_str().unwrap());
dbg!(&fs_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(
Expand Down
1 change: 0 additions & 1 deletion rust/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#[cfg(feature = "const")]
mod _auto_generated;

mod api;
mod node;

Expand Down
8 changes: 6 additions & 2 deletions rust/src/node/model.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#[cfg(feature = "serde_derive")]
use serde::{Deserialize, Serialize};
use serde::Serialize;

#[cfg(feature = "storage")]
use serde::Deserialize;

#[cfg(feature = "const")]
#[derive(Debug, PartialEq)]
Expand Down Expand Up @@ -61,7 +64,8 @@ pub struct NodeTerms {

#[cfg(any(feature = "const", feature = "storage"))]
#[derive(Debug, PartialEq)]
#[cfg_attr(feature = "serde_derive", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde_derive", derive(Serialize))]
#[cfg_attr(feature = "storage", derive(Deserialize))]
pub struct Node {
#[cfg(feature = "const")]
#[cfg_attr(feature = "serde_derive", serde(borrow))]
Expand Down

0 comments on commit 44625c8

Please sign in to comment.