node_launchpad/
connection_mode.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
use std::fmt::{Display, Formatter, Result};

use serde::{Deserialize, Serialize};
use strum::EnumIter;

#[derive(Clone, Copy, Debug, Default, EnumIter, Eq, Hash, PartialEq)]
pub enum ConnectionMode {
    #[default]
    Automatic,
    HomeNetwork,
    UPnP,
    CustomPorts,
}

impl Display for ConnectionMode {
    fn fmt(&self, f: &mut Formatter) -> Result {
        match self {
            ConnectionMode::HomeNetwork => write!(f, "Home Network"),
            ConnectionMode::UPnP => write!(f, "UPnP"),
            ConnectionMode::CustomPorts => write!(f, "Custom Ports"),
            ConnectionMode::Automatic => write!(f, "Automatic"),
        }
    }
}

impl<'de> Deserialize<'de> for ConnectionMode {
    fn deserialize<D>(deserializer: D) -> std::result::Result<ConnectionMode, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let s = String::deserialize(deserializer)?;
        match s.as_str() {
            "Home Network" => Ok(ConnectionMode::HomeNetwork),
            "UPnP" => Ok(ConnectionMode::UPnP),
            "Custom Ports" => Ok(ConnectionMode::CustomPorts),
            "Automatic" => Ok(ConnectionMode::Automatic),
            _ => Err(serde::de::Error::custom(format!(
                "Invalid ConnectionMode: {s:?}"
            ))),
        }
    }
}

impl Serialize for ConnectionMode {
    fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        let s = match self {
            ConnectionMode::HomeNetwork => "Home Network",
            ConnectionMode::UPnP => "UPnP",
            ConnectionMode::CustomPorts => "Custom Ports",
            ConnectionMode::Automatic => "Automatic",
        };
        serializer.serialize_str(s)
    }
}