maven_rs/pom/
parent.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
use derive_builder::Builder;
use serde::{Deserialize, Serialize};

use crate::editor::{
    utils::{add_if_present, from_element_using_builder, sync_element},
    ElementConverter, HasElementName, UpdatableElement,
};

#[derive(Debug, Clone, Default, PartialEq, Eq, Hash, Serialize, Deserialize, Builder)]
#[serde(rename_all = "camelCase")]
pub struct Parent {
    #[builder(setter(into, strip_option), default)]
    pub group_id: Option<String>,
    #[builder(setter(into, strip_option), default)]
    pub artifact_id: Option<String>,
    #[builder(setter(into, strip_option), default)]
    pub version: Option<String>,
    #[builder(setter(into, strip_option), default)]
    pub relative_path: Option<String>,
}
impl HasElementName for Parent {
    fn element_name() -> &'static str {
        "parent"
    }
}
impl ElementConverter for Parent {
    from_element_using_builder!(
        ParentBuilder,
        element,
        document,
        "groupId" => group_id,
        "artifactId" => artifact_id,
        "version" => version,
        "relativePath" => relative_path
    );

    fn into_children(
        self,
        document: &mut edit_xml::Document,
    ) -> Result<Vec<edit_xml::Element>, crate::editor::XMLEditorError> {
        let Self {
            group_id,
            artifact_id,
            version,
            relative_path,
        } = self;
        let mut children = vec![];
        add_if_present!(document, children, group_id, "groupId");
        add_if_present!(document, children, artifact_id, "artifactId");
        add_if_present!(document, children, version, "version");
        add_if_present!(document, children, relative_path, "relativePath");
        Ok(children)
    }
}

impl UpdatableElement for Parent {
    fn update_element(
        &self,
        element: edit_xml::Element,
        document: &mut edit_xml::Document,
    ) -> Result<(), crate::editor::XMLEditorError> {
        sync_element(document, element, "version", self.version.as_deref());
        sync_element(
            document,
            element,
            "relativePath",
            self.relative_path.as_deref(),
        );
        Ok(())
    }
}
#[cfg(test)]
mod tests {
    use pretty_assertions::assert_eq;

    use crate::editor::utils::test_utils;

    pub use super::*;

    #[test]
    pub fn test_element_parse() -> anyhow::Result<()> {
        let test_value = r#"
            <parent>
                <groupId>dev.wyatt-herkamp</groupId>
                <artifactId>test</artifactId>
                <version>1.0.0</version>
            </parent>
        "#;
        let dep = test_utils::create_xml_to_element::<Parent>(test_value)?;
        assert_eq!(
            dep,
            Parent {
                group_id: Some("dev.wyatt-herkamp".to_string()),
                artifact_id: Some("test".to_string()),
                version: Some("1.0.0".to_string()),
                ..Default::default()
            }
        );
        println!("{:#?}", dep);
        Ok(())
    }
}