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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
//! Provides [`Expose`] for the `expose` field of [`Service`](super::Service).

use std::{
    fmt::{self, Display, Formatter},
    str::FromStr,
};

use serde::{Deserialize, Deserializer, Serialize, Serializer};

use crate::serde::FromStrOrU16Visitor;

use super::ports::{ParseRangeError, Protocol, Range};

/// Incoming port or range of ports which are exposed from the [`Service`](super::Service) container
/// to the host.
///
/// (De)serializes from/to an integer or string in the format `{start}[-{end}][/{protocol}]`.
///
/// [compose-spec](https://github.com/compose-spec/compose-spec/blob/master/05-services.md#expose)
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Expose {
    /// Port or port range.
    pub range: Range,

    /// Port protocol.
    pub protocol: Option<Protocol>,
}

impl From<u16> for Expose {
    fn from(start: u16) -> Self {
        Self {
            range: start.into(),
            protocol: None,
        }
    }
}

impl From<Range> for Expose {
    fn from(range: Range) -> Self {
        Self {
            range,
            protocol: None,
        }
    }
}

impl FromStr for Expose {
    type Err = ParseRangeError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        // Format is "{range}[/{protocol}]".

        let (range, protocol) = s.split_once('/').map_or((s, None), |(range, protocol)| {
            (range, Some(protocol.into()))
        });

        Ok(Self {
            range: range.parse()?,
            protocol,
        })
    }
}

impl TryFrom<&str> for Expose {
    type Error = ParseRangeError;

    fn try_from(value: &str) -> Result<Self, Self::Error> {
        value.parse()
    }
}

impl Display for Expose {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        let Self { range, protocol } = self;

        // Format is "{range}[/{protocol}]".

        Display::fmt(range, f)?;

        if let Some(protocol) = protocol {
            write!(f, "/{protocol}")?;
        }

        Ok(())
    }
}

impl Serialize for Expose {
    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        if self.range.end().is_none() && self.protocol.is_none() {
            self.range.start().serialize(serializer)
        } else {
            serializer.collect_str(self)
        }
    }
}

impl<'de> Deserialize<'de> for Expose {
    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        FromStrOrU16Visitor::new("an integer or string representing a port or port range")
            .deserialize(deserializer)
    }
}

#[cfg(test)]
mod tests {
    use proptest::{option, prop_assert_eq, prop_compose, proptest};

    use crate::service::ports::tests::{protocol, range};

    use super::*;

    proptest! {
        #[test]
        fn parse_no_panic(string: String) {
            let _ = string.parse::<Expose>();
        }

        #[test]
        fn to_string_no_panic(expose in expose()) {
            expose.to_string();
        }

        #[test]
        fn round_trip(expose in expose()) {
            prop_assert_eq!(&expose, &expose.to_string().parse()?);
        }
    }

    prop_compose! {
        fn expose()(range in range(), protocol in option::of(protocol())) -> Expose {
            Expose {
                range,
                protocol,
            }
        }
    }
}