maven_rs/
pom.rs

1//! The structure and functions to work with a pom file.
2use 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/// Represents a pom file.
22///
23/// This structure is used with Serde to deserialize a pom file.
24///
25/// # Example
26/// ```
27/// use maven_rs::pom::Pom;
28/// const EXAMPLE_POM: &str = r#"
29///    <?xml version="1.0" encoding="UTF-8"?>
30///    <project>
31///         <modelVersion>4.0.0</modelVersion>
32///         <groupId>org.apache.maven</groupId>
33///         <artifactId>maven-artifact</artifactId>
34///         <version>3.0</version>
35///         <name>Apache Maven Artifact</name>
36///    </project>
37/// "#;
38///
39/// let x: Pom = maven_rs::quick_xml::de::from_str(EXAMPLE_POM).unwrap();
40/// println!("{:#?}", x);
41/// assert_eq!(x.group_id, Some("org.apache.maven".to_string()));
42/// assert_eq!(x.artifact_id, "maven-artifact".to_string());
43/// assert_eq!(x.version, Some("3.0".to_string()));
44/// ```
45#[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    /// Gets the group id of the pom.
60    /// If the group id is not present, it will attempt to get the group id from the parent.
61    /// If the parent does not have a group id, it will return None.
62    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}