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..7076b38 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_existent() { + 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 - ); - } } 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, }