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
use std::{
    convert::TryInto,
    net::{Ipv4Addr, SocketAddr},
};

use super::*;

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ListenerOptions {
    pub socket: SocketOptions,
}

impl ListenerOptions {
    pub fn new(local: impl TryInto<SocketAddress>) -> Result<Valid<Self>, OptionsError> {
        Self::with(local, Default::default())
    }

    pub fn with(
        local: impl TryInto<SocketAddress>,
        socket: SocketOptions,
    ) -> Result<Valid<ListenerOptions>, OptionsError> {
        let local_address = local
            .try_into()
            .map_err(|_| OptionsError::InvalidLocalAddress)?;

        use SocketHost::*;
        let local = match local_address.host {
            Ipv4(ipv4) => SocketAddr::new(ipv4.into(), local_address.port),
            Ipv6(ipv6) => SocketAddr::new(ipv6.into(), local_address.port),
            Domain(_) => return Err(OptionsError::InvalidLocalAddress),
        };

        let mut options = Self { socket };
        options.socket.connect.local.set_port(local.port());
        if local.ip() != Ipv4Addr::UNSPECIFIED {
            options.socket.connect.local.set_ip(local.ip());
        }

        options.try_validate()
    }
}

impl Validation for ListenerOptions {
    type Error = OptionsError;

    fn is_valid(&self) -> Result<(), Self::Error> {
        self.socket.is_valid()?;
        if self.socket.connect.local.port() == 0 {
            Err(OptionsError::LocalPortRequiredToListen)
        } else {
            self.is_valid_composite()
        }
    }
}

impl CompositeValidation for ListenerOptions {
    fn is_valid_composite(&self) -> Result<(), <Self as Validation>::Error> {
        Ok(())
    }
}

impl OptionsOf<SocketOptions> for ListenerOptions {
    fn set_options(&mut self, value: SocketOptions) {
        self.socket = value;
    }
}

impl OptionsOf<Connect> for ListenerOptions {
    fn set_options(&mut self, value: Connect) {
        self.socket.connect = value;
    }
}

impl OptionsOf<Session> for ListenerOptions {
    fn set_options(&mut self, value: Session) {
        self.socket.session = value;
    }
}

impl OptionsOf<Encryption> for ListenerOptions {
    fn set_options(&mut self, value: Encryption) {
        self.socket.encryption = value;
    }
}

impl OptionsOf<Sender> for ListenerOptions {
    fn set_options(&mut self, value: Sender) {
        self.socket.sender = value;
    }
}

impl OptionsOf<Receiver> for ListenerOptions {
    fn set_options(&mut self, value: Receiver) {
        self.socket.receiver = value;
    }
}