From 124f435c123b278d4161ff92df678879ec67a0cd Mon Sep 17 00:00:00 2001 From: omdxp Date: Thu, 11 Jan 2024 21:31:52 +0100 Subject: [PATCH] chore: address PR comments --- rust/src/api/get_node_by_path.rs | 66 ++++++++------------------------ rust/src/api/storage.rs | 5 ++- rust/src/lib.rs | 1 - rust/src/node/model.rs | 8 +++- 4 files changed, 24 insertions(+), 56 deletions(-) diff --git a/rust/src/api/get_node_by_path.rs b/rust/src/api/get_node_by_path.rs index 6fd0c6a..9ed9060 100644 --- a/rust/src/api/get_node_by_path.rs +++ b/rust/src/api/get_node_by_path.rs @@ -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 { - storage::get_node_by_path(data_path, path) +pub fn get_node_by_path(path: &str) -> Result { + storage::get_node_by_path(path) } #[cfg(any(feature = "const", feature = "storage"))] @@ -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, @@ -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, @@ -185,19 +149,19 @@ mod test { let actual = get_node_by_path(path); assert_eq!(actual, expected); } - #[cfg(feature = "storage")] - { - let actual = get_node_by_path("../_data", path).ok(); - assert_eq!(actual, expected); - } - #[cfg(feature = "serde_derive")] + #[cfg(any(feature = "storage", 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 + ); + } } } } @@ -211,7 +175,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()); } } diff --git a/rust/src/api/storage.rs b/rust/src/api/storage.rs index e14b0dc..591799e 100644 --- a/rust/src/api/storage.rs +++ b/rust/src/api/storage.rs @@ -2,8 +2,9 @@ use crate::Node; #[cfg(feature = "storage")] -pub fn get_node_by_path(data_path: &'static str, path: &str) -> Result { - let fs_path = format!("{}/{}/info.json", data_path, path); +pub fn get_node_by_path(path: impl AsRef) -> Result { + 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::(info.as_str()) else { return Err(std::io::Error::new( diff --git a/rust/src/lib.rs b/rust/src/lib.rs index 3ee6ef9..71ad07c 100644 --- a/rust/src/lib.rs +++ b/rust/src/lib.rs @@ -1,6 +1,5 @@ #[cfg(feature = "const")] mod _auto_generated; - mod api; mod node; diff --git a/rust/src/node/model.rs b/rust/src/node/model.rs index d616c0d..ec717bd 100644 --- a/rust/src/node/model.rs +++ b/rust/src/node/model.rs @@ -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)] @@ -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))]