1use serde::{Deserialize, Serialize};
3mod build;
4mod depend;
5mod developers;
6mod distribution_management;
7pub mod editor;
8mod parent;
9mod properties;
10mod repositories;
11mod scm;
12pub use build::*;
13pub use depend::*;
14pub use developers::*;
15pub use distribution_management::*;
16pub use parent::*;
17pub use properties::*;
18pub use repositories::*;
19pub use scm::*;
20
21#[derive(Debug, Serialize, Deserialize, Clone)]
46pub struct Pom {
47 #[serde(rename = "groupId")]
48 pub group_id: Option<String>,
49 #[serde(rename = "artifactId")]
50 pub artifact_id: String,
51 pub parent: Option<Parent>,
52 pub version: Option<String>,
53 pub name: Option<String>,
54 pub description: Option<String>,
55 pub url: Option<String>,
56 pub scm: Option<Scm>,
57}
58impl Pom {
59 pub fn get_group_id(&self) -> Option<&str> {
63 self.group_id
64 .as_deref()
65 .or(self.parent.as_ref().and_then(|x| x.group_id.as_deref()))
66 }
67 pub fn get_version(&self) -> Option<&str> {
68 self.version
69 .as_deref()
70 .or(self.parent.as_ref().and_then(|x| x.version.as_deref()))
71 }
72}
73
74#[cfg(test)]
75pub mod tests {
76 use anyhow::Context;
77
78 use crate::pom::Pom;
79
80 #[test]
81 pub fn test_version_or_group_id_in_parent() -> anyhow::Result<()> {
82 const EXAMPLE_POM: &str = r#"
83 <?xml version="1.0" encoding="UTF-8"?>
84 <project>
85 <modelVersion>4.0.0</modelVersion>
86 <parent>
87 <groupId>com.google.code.gson</groupId>
88 <artifactId>gson-parent</artifactId>
89 <version>2.11.0</version>
90 </parent>
91
92 <artifactId>gson</artifactId>
93 <name>Gson</name>
94 </project>
95 "#;
96 let pom: Pom = quick_xml::de::from_str(EXAMPLE_POM).context("Unable to Parse Test Pom")?;
97 assert_eq!(pom.get_group_id(), Some("com.google.code.gson"));
98 assert_eq!(pom.get_version(), Some("2.11.0"));
99 assert_eq!(pom.artifact_id, "gson");
100 Ok(())
101 }
102}