outpost/
lib.rs

1use anyhow::Result;
2
3#[derive(Debug, Clone)]
4pub struct PortMapping {
5    pub local: u16,
6    pub public: u16,
7}
8
9impl PortMapping {
10    pub fn from_vec(p: Vec<String>) -> Result<Vec<Self>> {
11        let mut ports = Vec::new();
12
13        for mapping in p.into_iter() {
14            let mapping = mapping.try_into()?;
15
16            // Check for duplicates
17            // TODO
18
19            ports.push(mapping);
20        }
21
22        Ok(ports)
23    }
24}
25
26impl TryFrom<String> for PortMapping {
27    type Error = anyhow::Error;
28
29    fn try_from(value: String) -> Result<Self> {
30        if let Some((local, public)) = value.split_once(":") {
31            Ok(PortMapping {
32                local: local.parse::<u16>()?,
33                public: public.parse::<u16>()?,
34            })
35        } else {
36            Ok(PortMapping {
37                local: value.parse::<u16>()?,
38                public: value.parse::<u16>()?,
39            })
40        }
41    }
42}