maven_rs/settings/
servers.rs

1use derive_builder::Builder;
2use serde::{Deserialize, Serialize};
3
4use crate::editor::{
5    utils::{add_if_present, create_basic_text_element, from_element_using_builder},
6    ChildOfListElement, ElementConverter, HasElementName, UpdatableElement,
7};
8
9#[derive(Debug, Clone, Serialize, Deserialize, Default)]
10pub struct Servers {
11    #[serde(default, rename = "server")]
12    pub servers: Vec<Server>,
13}
14
15#[derive(Debug, Clone, Serialize, Deserialize, Default, Builder, PartialEq)]
16pub struct Server {
17    pub id: String,
18    #[builder(setter(into, strip_option), default)]
19    pub username: Option<String>,
20    #[builder(setter(into, strip_option), default)]
21    pub password: Option<String>,
22    #[builder(setter(into, strip_option), default)]
23    pub private_key: Option<String>,
24    #[builder(setter(into, strip_option), default)]
25    pub passphrase: Option<String>,
26    #[builder(setter(into, strip_option), default)]
27    pub file_permissions: Option<String>,
28    #[builder(setter(into, strip_option), default)]
29    pub directory_permissions: Option<String>,
30    // TODO: configuration elements
31}
32
33impl Server {
34    pub fn username_and_password(&self) -> Option<(&str, &str)> {
35        match (&self.username, &self.password) {
36            (Some(username), Some(password)) => Some((username.as_str(), password.as_str())),
37            _ => None,
38        }
39    }
40
41    pub fn private_key_and_passphrase(&self) -> Option<(&str, &str)> {
42        match (&self.private_key, &self.passphrase) {
43            (Some(private_key), Some(passphrase)) => {
44                Some((private_key.as_str(), passphrase.as_str()))
45            }
46            _ => None,
47        }
48    }
49}
50impl HasElementName for Server {
51    fn element_name() -> &'static str {
52        "server"
53    }
54}
55impl ElementConverter for Server {
56    from_element_using_builder!(
57        ServerBuilder,
58        element,
59        document,
60        "id" => id,
61        "username" => username,
62        "password" => password,
63        "privateKey" => private_key,
64        "passphrase" => passphrase,
65        "filePermissions" => file_permissions,
66        "directoryPermissions" => directory_permissions
67    );
68
69    fn into_children(
70        self,
71        document: &mut edit_xml::Document,
72    ) -> Result<Vec<edit_xml::Element>, crate::editor::XMLEditorError> {
73        let Self {
74            id,
75            username,
76            password,
77            private_key,
78            passphrase,
79            file_permissions,
80            directory_permissions,
81        } = self;
82        let mut children = vec![create_basic_text_element(document, "id", id)];
83
84        add_if_present!(document, children, username, "username");
85        add_if_present!(document, children, password, "password");
86        add_if_present!(document, children, private_key, "privateKey");
87        add_if_present!(document, children, passphrase, "passphrase");
88        add_if_present!(document, children, file_permissions, "filePermissions");
89        add_if_present!(
90            document,
91            children,
92            directory_permissions,
93            "directoryPermissions"
94        );
95
96        Ok(children)
97    }
98}
99
100impl ChildOfListElement for Server {
101    fn parent_element_name() -> &'static str {
102        "servers"
103    }
104}
105impl UpdatableElement for Server {
106    /// Will rewrite the entire element with the current element. Because it might be a change from a password to a private key.
107    fn update_element(
108        &self,
109        element: edit_xml::Element,
110        document: &mut edit_xml::Document,
111    ) -> Result<(), crate::editor::XMLEditorError> {
112        element.clear_children(document);
113        for child in self.clone().into_children(document)? {
114            element.push_child(document, child)?;
115        }
116        Ok(())
117    }
118}