From 0e83ee914d596f6e0d59e331d48fa4ee68362c99 Mon Sep 17 00:00:00 2001 From: Shautvast Date: Thu, 24 Jul 2025 09:34:45 +0200 Subject: [PATCH] added properties (cost me 3 days to make this a 15 minute task) --- src/maven/mod.rs | 2 +- src/maven/pom.rs | 43 ++++++++++++---------- src/maven/{pom_reader.rs => pom_parser.rs} | 22 ++++++++++- tests/{pom => maven}/mod.rs | 0 tests/maven/pom_parser_test.rs | 17 +++++++++ tests/{pom => maven}/resources/pom.xml | 6 +++ tests/mod.rs | 2 +- tests/pom/pom_parser_test.rs | 9 ----- tests/xml/dom_parser_test.rs | 2 +- 9 files changed, 69 insertions(+), 34 deletions(-) rename src/maven/{pom_reader.rs => pom_parser.rs} (84%) rename tests/{pom => maven}/mod.rs (100%) create mode 100644 tests/maven/pom_parser_test.rs rename tests/{pom => maven}/resources/pom.xml (90%) delete mode 100644 tests/pom/pom_parser_test.rs diff --git a/src/maven/mod.rs b/src/maven/mod.rs index e6b3626..c5c8ef4 100644 --- a/src/maven/mod.rs +++ b/src/maven/mod.rs @@ -1,4 +1,4 @@ pub mod metadata; pub mod pom; pub mod pom_view; -pub mod pom_reader; +pub mod pom_parser; diff --git a/src/maven/pom.rs b/src/maven/pom.rs index 8aa137b..7cad7fd 100644 --- a/src/maven/pom.rs +++ b/src/maven/pom.rs @@ -1,43 +1,46 @@ +use std::collections::HashMap; + /// The Maven variant to parse poms /// These structs is directly modelled after the XML because that is what strong-xml plugin requires #[derive(PartialEq, Debug)] pub struct Pom { - pub(crate) parent: Option, - pub(crate) group_id: Option, - pub(crate) artifact_id: String, - pub(crate) version: Option, - pub(crate) name: String, - pub(crate) packaging: Option, - pub(crate) url: Option, - pub(crate) dependencies: Vec, - pub(crate) dependency_management: Vec, + pub parent: Option, + pub group_id: Option, + pub artifact_id: String, + pub version: Option, + pub name: String, + pub packaging: Option, + pub url: Option, + pub dependencies: Vec, + pub dependency_management: Vec, + pub properties: HashMap, } #[derive(PartialEq, Debug)] pub struct License { - pub(crate) name: String, - pub(crate) url: String, - pub(crate) distribution: Option, + pub name: String, + pub url: String, + pub distribution: Option, } #[derive(PartialEq, Debug)] pub struct Parent { - pub(crate) group_id: String, - pub(crate) artifact_id: String, - pub(crate) version: String, + pub group_id: String, + pub artifact_id: String, + pub version: String, } #[derive(PartialEq, Debug)] pub struct Developer { - pub(crate) id: Option, - pub(crate) name: String, + pub id: Option, + pub name: String, } #[derive(PartialEq, Debug, Clone)] pub struct Dependency { - pub(crate) group_id: String, - pub(crate) artifact_id: String, - pub(crate) version: Option, + pub group_id: String, + pub artifact_id: String, + pub version: Option, } #[cfg(test)] diff --git a/src/maven/pom_reader.rs b/src/maven/pom_parser.rs similarity index 84% rename from src/maven/pom_reader.rs rename to src/maven/pom_parser.rs index 0054408..1c8ccee 100644 --- a/src/maven/pom_reader.rs +++ b/src/maven/pom_parser.rs @@ -1,6 +1,7 @@ use crate::maven::pom::{Dependency, Developer, Parent, Pom}; use crate::xml::SaxError; use crate::xml::dom_parser::{Node, get_document}; +use std::collections::HashMap; pub fn get_pom(xml: &str) -> Result { let mut group_id = None; @@ -12,6 +13,7 @@ pub fn get_pom(xml: &str) -> Result { let mut url = None; let mut dependencies = vec![]; let mut dependency_management = vec![]; + let mut properties = HashMap::new(); //useless assignment... for child in get_document(xml)?.root.children { match child.name.as_str() { @@ -24,6 +26,7 @@ pub fn get_pom(xml: &str) -> Result { "url" => url = child.text, "dependencies" => dependencies = get_dependencies(child), "dependencyManagement" => dependency_management = get_dependency_mgmt(child), + "properties" => properties = get_properties(child), _ => {} } } @@ -36,12 +39,27 @@ pub fn get_pom(xml: &str) -> Result { packaging, url, dependencies, - dependency_management + dependency_management, + properties, }) } +fn get_properties(element: Node) -> HashMap { + let mut properties = HashMap::new(); + for property in element.children { + properties.insert( + property.name.clone(), + property + .text + .expect(format!("Cannot read property '{}'", property.name).as_str()) + .to_string(), + ); + } + properties +} + fn get_dependency_mgmt(element: Node) -> Vec { - if !element.children.is_empty(){ + if !element.children.is_empty() { get_dependencies(element.children.first().unwrap().clone()) } else { vec![] diff --git a/tests/pom/mod.rs b/tests/maven/mod.rs similarity index 100% rename from tests/pom/mod.rs rename to tests/maven/mod.rs diff --git a/tests/maven/pom_parser_test.rs b/tests/maven/pom_parser_test.rs new file mode 100644 index 0000000..7864811 --- /dev/null +++ b/tests/maven/pom_parser_test.rs @@ -0,0 +1,17 @@ +use undeepend::maven::pom_parser::get_pom; +use undeepend::xml::dom_parser::get_document; + +#[test] +fn test_pom_parser() { + let test_xml = include_str!("../maven/resources/pom.xml"); + let pom = get_pom(test_xml).expect("failed to get document"); + assert_eq!("Mockito",pom.name); + assert_eq!(Some("org.mockito".to_string()),pom.group_id); + assert_eq!("mockito-core",pom.artifact_id); + assert_eq!(Some("1.9.5".to_string()),pom.version); + assert_eq!(Some("jar".to_string()),pom.packaging); + assert_eq!(Some("http://www.mockito.org".to_string()),pom.url); + assert_eq!(2, pom.properties.len()); + assert_eq!("17", pom.properties["maven.compiler.source"]); + assert_eq!("21", pom.properties["maven.compiler.target"]); +} diff --git a/tests/pom/resources/pom.xml b/tests/maven/resources/pom.xml similarity index 90% rename from tests/pom/resources/pom.xml rename to tests/maven/resources/pom.xml index 5894332..e4c7a55 100644 --- a/tests/pom/resources/pom.xml +++ b/tests/maven/resources/pom.xml @@ -7,6 +7,12 @@ 1.9.5 Mockito jar + + + 17 + 21 + + http://www.mockito.org Mock objects library for java diff --git a/tests/mod.rs b/tests/mod.rs index 534dd8c..565ad22 100644 --- a/tests/mod.rs +++ b/tests/mod.rs @@ -1,2 +1,2 @@ mod xml; -mod pom; \ No newline at end of file +mod maven; \ No newline at end of file diff --git a/tests/pom/pom_parser_test.rs b/tests/pom/pom_parser_test.rs deleted file mode 100644 index d30c7c9..0000000 --- a/tests/pom/pom_parser_test.rs +++ /dev/null @@ -1,9 +0,0 @@ -use undeepend::maven::pom_reader::get_pom; -use undeepend::xml::dom_parser::get_document; - -#[test] -fn test_pom_parser() { - let test_xml = include_str!("../pom/resources/pom.xml"); - let pom = get_pom(test_xml).expect("failed to get document"); - println!("{:?}", pom); -} diff --git a/tests/xml/dom_parser_test.rs b/tests/xml/dom_parser_test.rs index cda589f..981d488 100644 --- a/tests/xml/dom_parser_test.rs +++ b/tests/xml/dom_parser_test.rs @@ -2,7 +2,7 @@ use undeepend::xml::dom_parser::get_document; #[test] fn test_dom_parser() { - let test_xml = include_str!("../pom/resources/pom.xml"); + let test_xml = include_str!("../maven/resources/pom.xml"); let doc = get_document(test_xml).expect("failed to get document"); println!("{:?}",doc); } \ No newline at end of file