maven_rs/pom/editor/
distribution_management.rs1use edit_xml::Element;
2
3use super::PomEditor;
4use crate::editor::ElementConverter;
5use crate::editor::{UpdatableElement, XMLEditorError};
6use crate::pom::{DistributionRepositoryRepository, DistributionRepositorySnapshotRepository};
7impl PomEditor {
8 pub fn get_or_create_distribution_management_element(
15 &mut self,
16 ) -> DistributionManagementEditor<'_> {
17 DistributionManagementEditor::new(self)
18 }
19 pub fn get_distribution_management_element_or_none(
23 &mut self,
24 ) -> Option<DistributionManagementEditor<'_>> {
25 if self.has_distribution_management() {
26 return Some(DistributionManagementEditor::new(self));
27 }
28 None
29 }
30 pub fn has_distribution_management(&self) -> bool {
31 let root = self.root();
32 root.find(&self.document, "distributionManagement")
33 .is_some()
34 }
35 pub fn delete_distribution_management(&mut self) -> Result<bool, XMLEditorError> {
36 let root = self.root();
37 let element = root.find(&self.document, "distributionManagement");
38 if let Some(element) = element {
39 element.detach(&mut self.document)?;
40 Ok(true)
41 } else {
42 Ok(false)
43 }
44 }
45}
46#[derive(Debug)]
48pub struct DistributionManagementEditor<'a> {
49 parent: &'a mut PomEditor,
50 element: Element,
51}
52macro_rules! top_level_structured_type {
53 (
54 $(#[$set_docs:meta])*
55 set: $set:ident,
56 $(#[$get_docs:meta])*
57 get: $get:ident,
58 $element_name:literal => $structured_type:ident,
59 ) => {
60 $(#[$get_docs])*
61 pub fn $get(&self) -> Result<Option<$structured_type>, XMLEditorError> {
62 self.element.find(&self.parent.document, $element_name)
63 .map(|x| $structured_type::from_element(x, &self.parent.document))
64 .transpose()
65 }
66 $(#[$set_docs])*
67 pub fn $set<U>(&mut self, value: U) -> Result<(), XMLEditorError>
68 where
69 U: Into<Option<$structured_type>> {
70 let value: Option<$structured_type> = value.into();
71 let root = self.element;
72 let existing_element = root.find(&self.parent.document, $element_name);
73 if let Some(value) = value{
74 if let Some(element) = existing_element {
75 value.update_element(element, &mut self.parent.document)?;
76 return Ok(());
77 }
78 let new_element = value.into_element(&mut self.parent.document)?;
79 root.push_child(&mut self.parent.document, new_element)?;
80 }else{
81 if let Some(element) = existing_element {
82 element.detach(&mut self.parent.document)?;
83 }
84 }
85
86 Ok(())
87 }
88 };
89}
90
91impl<'a> DistributionManagementEditor<'a> {
92 pub(super) fn new(parent: &'a mut PomEditor) -> Self {
93 let root = parent.root();
94 let element: Element = crate::editor::utils::get_or_create_top_level_element(
95 "distributionManagement",
96 &mut parent.document,
97 root,
98 );
99 Self { parent, element }
100 }
101 top_level_structured_type!(
102 set: set_repository,
103 get: get_repository,
104 "repository" => DistributionRepositoryRepository,
105 );
106 top_level_structured_type!(
107 set: set_snapshot_repository,
108 get: get_snapshot_repository,
109 "snapshotRepository" => DistributionRepositorySnapshotRepository,
110 );
111}
112
113#[cfg(test)]
114mod tests {
115 use crate::pom::{distribution_management, editor::PomEditor};
116
117 #[test]
118 pub fn test_dm() -> anyhow::Result<()> {
119 let mut editor = PomEditor::new_with_group_and_artifact("dev.wyatt-herkamp", "test");
120 {
121 let mut distribution_management =
122 editor.get_or_create_distribution_management_element();
123 let repository = distribution_management.get_repository()?;
124 assert!(repository.is_none());
125
126 let repository = distribution_management.get_snapshot_repository()?;
127 assert!(repository.is_none());
128
129 distribution_management.set_repository(Some(
130 distribution_management::DistributionRepository {
131 id: Some("test".to_string()),
132 name: Some("test".to_string()),
133 url: "https://test.com".to_string(),
134 layout: Some("default".to_string()),
135 ..Default::default()
136 }
137 .repository(),
138 ))?;
139
140 distribution_management.set_repository(Some(
141 distribution_management::DistributionRepository {
142 id: Some("test".to_string()),
143 name: Some("test".to_string()),
144 url: "https://test.com".to_string(),
145 layout: Some("default".to_string()),
146 ..Default::default()
147 }
148 .repository(),
149 ))?;
150
151 distribution_management.set_snapshot_repository(
152 distribution_management::DistributionRepository {
153 id: Some("test-snapshot".to_string()),
154 name: Some("test".to_string()),
155 url: "https://test.com".to_string(),
156 layout: Some("default".to_string()),
157 ..Default::default()
158 }
159 .snapshot_repository(),
160 )?;
161 }
162 let value = editor.write_to_str()?;
163 println!("{}", value);
164
165 Ok(())
166 }
167
168 #[test]
169 pub fn test_create_and_delete() -> anyhow::Result<()> {
170 let mut editor = PomEditor::new_with_group_and_artifact("dev.wyatt-herkamp", "test");
171
172 assert!(
173 editor
174 .get_distribution_management_element_or_none()
175 .is_none()
176 );
177
178 assert!(!editor.has_distribution_management());
179
180 assert!(!editor.delete_distribution_management()?);
181
182 {
183 let mut dm = editor.get_or_create_distribution_management_element();
184
185 dm.set_repository(Some(
186 distribution_management::DistributionRepository {
187 id: Some("test".to_string()),
188 name: Some("test".to_string()),
189 url: "https://test.com".to_string(),
190 layout: Some("default".to_string()),
191 ..Default::default()
192 }
193 .repository(),
194 ))?;
195 }
196
197 let value = editor.write_to_str()?;
198
199 let mut editor = PomEditor::load_from_str(&value)?;
200
201 assert!(editor.has_distribution_management());
202 {
203 let dm = editor.get_distribution_management_element_or_none();
204
205 assert!(dm.is_some());
206 }
207 editor.delete_distribution_management()?;
208
209 assert!(!editor.has_distribution_management());
210
211 Ok(())
212 }
213}