From a7286fa4712754e8ce80249502ae3ff9bdf72ef0 Mon Sep 17 00:00:00 2001 From: Zakaria Mansouri Date: Sun, 7 Jan 2024 23:00:18 +0100 Subject: [PATCH 1/3] removed un-needed deserialization --- rust/src/node/model.rs | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/rust/src/node/model.rs b/rust/src/node/model.rs index 22d1d90..55b57cc 100644 --- a/rust/src/node/model.rs +++ b/rust/src/node/model.rs @@ -1,19 +1,19 @@ #[cfg(feature = "serde_derive")] -use serde::{Deserialize, Serialize}; +use serde::Serialize; #[derive(Debug, PartialEq)] -#[cfg_attr(feature = "serde_derive", derive(Serialize, Deserialize))] -pub struct NodeName<'a> { +#[cfg_attr(feature = "serde_derive", derive(Serialize))] +pub struct NodeName { #[cfg(feature = "const")] - pub ar: &'a str, + pub ar: &'static str, #[cfg(feature = "const")] - pub en: &'a str, + pub en: &'static str, #[cfg(feature = "const")] - pub fr: &'a str, + pub fr: &'static str, } #[derive(Debug, PartialEq)] -#[cfg_attr(feature = "serde_derive", derive(Serialize, Deserialize))] +#[cfg_attr(feature = "serde_derive", derive(Serialize))] pub enum NodeType { University, Academy, @@ -26,18 +26,16 @@ pub enum NodeType { } #[derive(Debug, PartialEq, Clone)] -#[cfg_attr(feature = "serde_derive", derive(Serialize, Deserialize))] +#[cfg_attr(feature = "serde_derive", derive(Serialize))] pub struct NodeTerms { pub per_year: usize, #[cfg(feature = "const")] - #[cfg_attr(feature = "serde_derive", serde(skip_deserializing))] pub slots: &'static [i32], } #[derive(Debug, PartialEq)] -#[cfg_attr(feature = "serde_derive", derive(Serialize, Deserialize))] +#[cfg_attr(feature = "serde_derive", derive(Serialize))] pub struct Node { - #[cfg_attr(feature = "serde_derive", serde(borrow))] - pub name: NodeName<'static>, + pub name: NodeName, pub r#type: NodeType, } From 42d683094435e34966d0ff2fdefbdfa963166a3d Mon Sep 17 00:00:00 2001 From: Zakaria Mansouri Date: Sun, 7 Jan 2024 23:00:33 +0100 Subject: [PATCH 2/3] refactored tests to cater for serde_derive feature --- .github/workflows/rust-checks.yml | 1 + rust/src/api/get_node_by_path.rs | 74 +++++++++++++++---------------- 2 files changed, 36 insertions(+), 39 deletions(-) diff --git a/.github/workflows/rust-checks.yml b/.github/workflows/rust-checks.yml index a6c038a..a6a24c1 100644 --- a/.github/workflows/rust-checks.yml +++ b/.github/workflows/rust-checks.yml @@ -24,3 +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 --features serde_derive diff --git a/rust/src/api/get_node_by_path.rs b/rust/src/api/get_node_by_path.rs index f8681c8..3893a22 100644 --- a/rust/src/api/get_node_by_path.rs +++ b/rust/src/api/get_node_by_path.rs @@ -11,45 +11,38 @@ mod test { use super::get_node_by_path; - struct TestCase<'a> { - path: &'a str, - expected: Node, - } - - impl<'a> TestCase<'a> { - pub fn new(path: &'a str, expected: Node) -> Self { - Self { path, expected } - } - } - #[test] - fn should_get_expected_info() { - let tests: Vec = vec![ - TestCase::new( + fn check_three_schemas_and_non_extant() { + let tests = vec![ + ( "umkb", - Node { + Some(&Node { name: NodeName { ar: "جامعة محمد خيضر بسكرة", en: "University of Mohamed Khider Biskra", fr: "Université Mohamed Khider Biskra", }, r#type: NodeType::University, - }, + }), + #[cfg(feature = "serde_derive")] + "{\"name\":{\"ar\":\"جامعة محمد خيضر بسكرة\",\"en\":\"University of Mohamed Khider Biskra\",\"fr\":\"Université Mohamed Khider Biskra\"},\"type\":\"University\"}", ), - TestCase::new( + ( "umkb/fst", - Node { + Some(&Node { name: NodeName { ar: "كلية العلوم والتكنلوجيا", en: "Faculty of Science and Technology", fr: "Faculté des Sciences et de la Technologie", }, r#type: NodeType::Faculty, - }, + }), + #[cfg(feature = "serde_derive")] + "{\"name\":{\"ar\":\"كلية العلوم والتكنلوجيا\",\"en\":\"Faculty of Science and Technology\",\"fr\":\"Faculté des Sciences et de la Technologie\"},\"type\":\"Faculty\"}", ), - TestCase::new( + ( "umkb/fst/dee/sec", - Node { + Some(&Node { name: NodeName { ar: "تخصص التحكم الكهربائي", en: "Specialy of Electrical Control", @@ -61,27 +54,30 @@ mod test { slots: &[7, 8, 9, 10], }, }, - }, + }), + #[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]}}}}", + ), + ( + "does/not/exist", None, + #[cfg(feature = "serde_derive")] + "null" ), ]; - for tc in tests { - let actual = get_node_by_path(tc.path).unwrap(); - assert_node(&tc.expected, &actual); + for test_case in tests { + let path = test_case.0; + let expected = test_case.1; + let actual = get_node_by_path(path); + assert_eq!(actual, expected); + #[cfg(feature = "serde_derive")] + { + 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() { - let res = get_node_by_path("does/not/exist"); - assert!(res.is_none()); - } - - fn assert_node(expected: &Node, actual: &Node) { - assert_eq!( - expected, actual, - "Expected node to be '{:?}', but got '{:?}'", - expected, actual - ); - } } From b231059869791d7eb6d00ced2fa54a36e9f2c8e0 Mon Sep 17 00:00:00 2001 From: Zakaria Mansouri Date: Sun, 7 Jan 2024 23:13:55 +0100 Subject: [PATCH 3/3] Fix typo in test function name --- rust/src/api/get_node_by_path.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rust/src/api/get_node_by_path.rs b/rust/src/api/get_node_by_path.rs index 3893a22..7076b38 100644 --- a/rust/src/api/get_node_by_path.rs +++ b/rust/src/api/get_node_by_path.rs @@ -12,7 +12,7 @@ mod test { use super::get_node_by_path; #[test] - fn check_three_schemas_and_non_extant() { + fn check_three_schemas_and_non_existent() { let tests = vec![ ( "umkb",