maven_rs/pom/
build.rs

1use derive_builder::Builder;
2use serde::{Deserialize, Serialize};
3
4use crate::{
5    editor::{
6        utils::{add_if_present, create_basic_text_element, find_element_or_err, sync_element},
7        ChildOfListElement, ComparableElement, ElementConverter, HasElementName, PomValue,
8        UpdatableElement, XMLEditorError,
9    },
10    types::Property,
11};
12
13#[derive(Debug, Serialize, Deserialize, Clone, Default, Builder)]
14pub struct Build {
15    #[serde(rename = "sourceDirectory")]
16    pub source_directory: Option<String>,
17    #[serde(default)]
18    pub plugins: Plugins,
19}
20#[derive(Debug, Serialize, Deserialize, Clone, Default)]
21pub struct Plugins {
22    #[serde(default, rename = "plugin")]
23    pub plugins: Vec<Plugin>,
24}
25#[derive(Debug, Serialize, Deserialize, Clone, Default, PartialEq, Eq, Builder)]
26pub struct Plugin {
27    #[serde(rename = "groupId")]
28    pub group_id: Option<String>,
29    #[serde(rename = "artifactId")]
30    pub artifact_id: String,
31    pub version: Option<Property>,
32    // TODO Add configuration
33}
34impl Plugin {
35    /// Checks if the plugin is the same as the other plugin.
36    ///
37    /// Basically, it checks if the group id and artifact id are the same.
38    pub fn is_same_plugin(&self, other: &Plugin) -> bool {
39        self.group_id == other.group_id && self.artifact_id == other.artifact_id
40    }
41}
42impl HasElementName for Plugin {
43    fn element_name() -> &'static str {
44        "plugin"
45    }
46}
47impl ElementConverter for Plugin {
48    fn from_element(
49        element: edit_xml::Element,
50        document: &edit_xml::Document,
51    ) -> Result<Self, XMLEditorError> {
52        let group_id = element
53            .find(document, "groupId")
54            .map(|group_id| String::from_element(group_id, document))
55            .transpose()?;
56        let artifact_id =
57            find_element_or_err(element, "artifactId", document)?.text_content(document);
58        let version = element
59            .find(document, "version")
60            .map(|element| Property::from_element(element, document))
61            .transpose()?;
62
63        Ok(Self {
64            group_id,
65            artifact_id,
66            version,
67        })
68    }
69
70    fn into_children(
71        self,
72        document: &mut edit_xml::Document,
73    ) -> Result<Vec<edit_xml::Element>, XMLEditorError> {
74        let Self {
75            group_id,
76            artifact_id,
77            version,
78        } = self;
79        let mut result = vec![];
80        add_if_present!(document, result, group_id, "groupId");
81        result.push(create_basic_text_element(
82            document,
83            "artifactId",
84            artifact_id,
85        ));
86        add_if_present!(document, result, version, "version");
87
88        Ok(result)
89    }
90}
91impl ChildOfListElement for Plugin {
92    fn parent_element_name() -> &'static str {
93        "plugins"
94    }
95}
96impl ComparableElement for Plugin {
97    fn is_same_item(&self, other: &Self) -> bool {
98        self.is_same_plugin(other)
99    }
100}
101impl UpdatableElement for Plugin {
102    fn update_element(
103        &self,
104        element: edit_xml::Element,
105        document: &mut edit_xml::Document,
106    ) -> Result<(), XMLEditorError> {
107        sync_element(
108            document,
109            element,
110            "version",
111            self.version.as_ref().map(|v| v.to_string()),
112        );
113        Ok(())
114    }
115}