maven_rs/pom/
properties.rs

1use ahash::{HashMap, HashMapExt};
2
3use crate::editor::{ElementConverter, HasElementName, UpdatableElement};
4//TODO: Do the values need to be something other than strings?
5//TODO: Ordering will be lost if we use a HashMap
6/// Represents the properties of a pom file.
7#[derive(Debug, Clone, Default, PartialEq, Eq)]
8pub struct Properties(pub HashMap<String, String>);
9impl HasElementName for Properties {
10    fn element_name() -> &'static str {
11        "properties"
12    }
13}
14impl ElementConverter for Properties {
15    fn from_element(
16        element: edit_xml::Element,
17        document: &edit_xml::Document,
18    ) -> Result<Self, crate::editor::XMLEditorError> {
19        let mut properties = HashMap::new();
20        for child in element.child_elements(document) {
21            let name = child.name(document).to_owned();
22            let value = child.text_content(document);
23            properties.insert(name, value);
24        }
25        Ok(Properties(properties))
26    }
27
28    fn into_children(
29        self,
30        document: &mut edit_xml::Document,
31    ) -> Result<Vec<edit_xml::Element>, crate::editor::XMLEditorError> {
32        let mut children = vec![];
33        for (name, value) in self.0 {
34            let element = edit_xml::Element::new(document, name);
35            element.set_text_content(document, value);
36            children.push(element);
37        }
38        Ok(children)
39    }
40}
41
42impl UpdatableElement for Properties {
43    /// Updating a Properties element means all of the original children are removed and replaced with the new children.
44    fn update_element(
45        &self,
46        element: edit_xml::Element,
47        document: &mut edit_xml::Document,
48    ) -> Result<(), crate::editor::XMLEditorError> {
49        element.clear_children(document);
50        for (key, value) in self.0.iter() {
51            let child = edit_xml::Element::new(document, key);
52            child.set_text_content(document, value);
53            element.push_child(document, child)?;
54        }
55        Ok(())
56    }
57}