node_launchpad/
connection_mode.rs

1// Copyright 2025 MaidSafe.net limited.
2//
3// This SAFE Network Software is licensed to you under The General Public License (GPL), version 3.
4// Unless required by applicable law or agreed to in writing, the SAFE Network Software distributed
5// under the GPL Licence is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
6// KIND, either express or implied. Please review the Licences for the specific language governing
7// permissions and limitations relating to use of the SAFE Network Software.
8
9use ant_service_management::NodeServiceData;
10use serde::{Deserialize, Serialize};
11use std::fmt::{Display, Formatter, Result};
12use strum::{Display, EnumIter};
13
14#[derive(Clone, Copy, Debug, Default, EnumIter, Eq, Hash, PartialEq)]
15pub enum ConnectionMode {
16    #[default]
17    Automatic,
18    HomeNetwork,
19    UPnP,
20    CustomPorts,
21}
22
23impl Display for ConnectionMode {
24    fn fmt(&self, f: &mut Formatter) -> Result {
25        match self {
26            ConnectionMode::HomeNetwork => write!(f, "Home Network"),
27            ConnectionMode::UPnP => write!(f, "UPnP"),
28            ConnectionMode::CustomPorts => write!(f, "Custom Ports"),
29            ConnectionMode::Automatic => write!(f, "Automatic"),
30        }
31    }
32}
33
34impl<'de> Deserialize<'de> for ConnectionMode {
35    fn deserialize<D>(deserializer: D) -> std::result::Result<ConnectionMode, D::Error>
36    where
37        D: serde::Deserializer<'de>,
38    {
39        let s = String::deserialize(deserializer)?;
40        match s.as_str() {
41            "Home Network" => Ok(ConnectionMode::HomeNetwork),
42            "UPnP" => Ok(ConnectionMode::UPnP),
43            "Custom Ports" => Ok(ConnectionMode::CustomPorts),
44            "Automatic" => Ok(ConnectionMode::Automatic),
45            _ => Err(serde::de::Error::custom(format!(
46                "Invalid ConnectionMode: {s:?}"
47            ))),
48        }
49    }
50}
51
52impl Serialize for ConnectionMode {
53    fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
54    where
55        S: serde::Serializer,
56    {
57        let s = match self {
58            ConnectionMode::HomeNetwork => "Home Network",
59            ConnectionMode::UPnP => "UPnP",
60            ConnectionMode::CustomPorts => "Custom Ports",
61            ConnectionMode::Automatic => "Automatic",
62        };
63        serializer.serialize_str(s)
64    }
65}
66
67#[derive(Default, Debug, Clone, Serialize, Display)]
68pub enum NodeConnectionMode {
69    UPnP,
70    Relay,
71    Manual,
72    #[default]
73    Unknown,
74}
75
76impl From<&NodeServiceData> for NodeConnectionMode {
77    fn from(nsd: &NodeServiceData) -> Self {
78        match (nsd.no_upnp, nsd.relay) {
79            (true, false) => Self::UPnP,
80            (false, true) => Self::Relay,
81            (false, false) => Self::Manual,
82            _ => Self::Unknown,
83        }
84    }
85}