1use derive_builder::Builder;
2use edit_xml::Element;
3use serde::{Deserialize, Serialize};
4
5use crate::editor::{
6 utils::{add_if_present, find_or_create_then_set_text_content, sync_element},
7 ComparableElement, ElementConverter, HasElementName, PomValue, UpdatableElement,
8};
9
10use super::{ChecksumPolicy, UpdatePolicy};
11
12#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize, Builder)]
13#[serde(rename_all = "camelCase")]
14pub struct DistributionRepository {
15 #[builder(setter(into, strip_option), default)]
16 pub id: Option<String>,
17 #[builder(setter(into, strip_option), default)]
18 pub name: Option<String>,
19 pub url: String,
20 #[builder(setter(into, strip_option), default)]
21 pub layout: Option<String>,
22 #[builder(setter(into, strip_option), default)]
23 pub update_policy: Option<UpdatePolicy>,
24 #[builder(setter(into, strip_option), default)]
25 pub checksum_policy: Option<ChecksumPolicy>,
26}
27impl DistributionRepository {
28 pub fn repository(self) -> DistributionRepositoryRepository {
29 DistributionRepositoryRepository::new(self)
30 }
31 pub fn snapshot_repository(self) -> DistributionRepositorySnapshotRepository {
32 DistributionRepositorySnapshotRepository::new(self)
33 }
34}
35
36impl ComparableElement for DistributionRepository {
37 fn is_same_item(&self, other: &Self) -> bool {
38 if self.name.is_none() {
39 return false;
40 }
41 self.name == other.name
42 }
43}
44impl UpdatableElement for DistributionRepository {
45 fn update_element(
46 &self,
47 element: Element,
48 document: &mut edit_xml::Document,
49 ) -> Result<(), crate::editor::XMLEditorError> {
50 sync_element(document, element, "id", self.id.as_deref());
51 sync_element(document, element, "name", self.name.as_deref());
52 find_or_create_then_set_text_content(document, element, "url", self.url.as_str());
53 Ok(())
55 }
56}
57impl ElementConverter for DistributionRepository {
58 fn from_element(
59 element: edit_xml::Element,
60 document: &edit_xml::Document,
61 ) -> Result<Self, crate::editor::XMLEditorError> {
62 let mut builder = DistributionRepositoryBuilder::default();
63 for child in element.child_elements(document) {
64 match child.name(document) {
65 "id" => {
66 builder.id(String::from_element(child, document)?);
67 }
68 "name" => {
69 builder.name(String::from_element(child, document)?);
70 }
71 "url" => {
72 builder.url(String::from_element(child, document)?);
73 }
74 "layout" => {
75 builder.layout(String::from_element(child, document)?);
76 }
77 "updatePolicy" => {
78 builder.update_policy(UpdatePolicy::from_element(child, document)?);
79 }
80 "checksumPolicy" => {
81 builder.checksum_policy(ChecksumPolicy::from_element(child, document)?);
82 }
83 _ => {}
84 }
85 }
86 let result = builder.build()?;
87 Ok(result)
88 }
89 fn into_children(
92 self,
93 document: &mut edit_xml::Document,
94 ) -> Result<Vec<edit_xml::Element>, crate::editor::XMLEditorError> {
95 let Self {
96 id,
97 name,
98 url,
99 layout,
100 update_policy,
101 checksum_policy,
102 } = self;
103 let mut children = vec![];
104 add_if_present!(document, children, id, "id");
105 add_if_present!(document, children, name, "name");
106 children.push(crate::editor::utils::create_basic_text_element(
107 document, "url", url,
108 ));
109 add_if_present!(document, children, layout, "layout");
110 add_if_present!(document, children, update_policy, "updatePolicy");
111 add_if_present!(document, children, checksum_policy, "checksumPolicy");
112
113 Ok(children)
114 }
115}
116
117pub struct DistributionRepositoryRepository(DistributionRepository);
118impl DistributionRepositoryRepository {
119 pub fn new(repository: DistributionRepository) -> Self {
120 Self(repository)
121 }
122}
123impl From<DistributionRepository> for DistributionRepositoryRepository {
124 fn from(repository: DistributionRepository) -> Self {
125 Self(repository)
126 }
127}
128impl HasElementName for DistributionRepositoryRepository {
129 fn element_name() -> &'static str {
130 "repository"
131 }
132}
133impl ComparableElement for DistributionRepositoryRepository {
134 fn is_same_item(&self, other: &Self) -> bool {
135 self.0.is_same_item(&other.0)
136 }
137}
138impl UpdatableElement for DistributionRepositoryRepository {
139 fn update_element(
140 &self,
141 element: Element,
142 document: &mut edit_xml::Document,
143 ) -> Result<(), crate::editor::XMLEditorError> {
144 self.0.update_element(element, document)
145 }
146}
147impl ElementConverter for DistributionRepositoryRepository {
148 fn from_element(
149 element: edit_xml::Element,
150 document: &edit_xml::Document,
151 ) -> Result<Self, crate::editor::XMLEditorError> {
152 let repository = DistributionRepository::from_element(element, document)?;
153 Ok(Self(repository))
154 }
155 fn into_children(
158 self,
159 document: &mut edit_xml::Document,
160 ) -> Result<Vec<edit_xml::Element>, crate::editor::XMLEditorError> {
161 self.0.into_children(document)
162 }
163}
164
165pub struct DistributionRepositorySnapshotRepository(DistributionRepository);
166impl DistributionRepositorySnapshotRepository {
167 pub fn new(repository: DistributionRepository) -> Self {
168 Self(repository)
169 }
170}
171impl From<DistributionRepository> for DistributionRepositorySnapshotRepository {
172 fn from(repository: DistributionRepository) -> Self {
173 Self(repository)
174 }
175}
176impl HasElementName for DistributionRepositorySnapshotRepository {
177 fn element_name() -> &'static str {
178 "snapshotRepository"
179 }
180}
181impl ComparableElement for DistributionRepositorySnapshotRepository {
182 fn is_same_item(&self, other: &Self) -> bool {
183 self.0.is_same_item(&other.0)
184 }
185}
186impl UpdatableElement for DistributionRepositorySnapshotRepository {
187 fn update_element(
188 &self,
189 element: Element,
190 document: &mut edit_xml::Document,
191 ) -> Result<(), crate::editor::XMLEditorError> {
192 self.0.update_element(element, document)
193 }
194}
195impl ElementConverter for DistributionRepositorySnapshotRepository {
196 fn from_element(
197 element: edit_xml::Element,
198 document: &edit_xml::Document,
199 ) -> Result<Self, crate::editor::XMLEditorError> {
200 let repository = DistributionRepository::from_element(element, document)?;
201 Ok(Self(repository))
202 }
203 fn into_children(
206 self,
207 document: &mut edit_xml::Document,
208 ) -> Result<Vec<edit_xml::Element>, crate::editor::XMLEditorError> {
209 self.0.into_children(document)
210 }
211}