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/Cargo.toml b/rust/Cargo.toml index 8e9894b..0347a87 100644 --- a/rust/Cargo.toml +++ b/rust/Cargo.toml @@ -6,8 +6,11 @@ edition = "2021" [features] default = ["const"] const = [] +serde_derive = ["dep:serde", "dep:serde_json"] [dependencies] +serde = { version = "1.0.194", features = ["derive"], optional = true } +serde_json = { version = "1.0.110", optional = true } [build-dependencies] serde = { version = "1.0.194", features = ["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 b545565..55b57cc 100644 --- a/rust/src/node/model.rs +++ b/rust/src/node/model.rs @@ -1,4 +1,8 @@ +#[cfg(feature = "serde_derive")] +use serde::Serialize; + #[derive(Debug, PartialEq)] +#[cfg_attr(feature = "serde_derive", derive(Serialize))] pub struct NodeName { #[cfg(feature = "const")] pub ar: &'static str, @@ -9,6 +13,7 @@ pub struct NodeName { } #[derive(Debug, PartialEq)] +#[cfg_attr(feature = "serde_derive", derive(Serialize))] pub enum NodeType { University, Academy, @@ -21,6 +26,7 @@ pub enum NodeType { } #[derive(Debug, PartialEq, Clone)] +#[cfg_attr(feature = "serde_derive", derive(Serialize))] pub struct NodeTerms { pub per_year: usize, #[cfg(feature = "const")] @@ -28,6 +34,7 @@ pub struct NodeTerms { } #[derive(Debug, PartialEq)] +#[cfg_attr(feature = "serde_derive", derive(Serialize))] pub struct Node { pub name: NodeName, pub r#type: NodeType,