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
#![allow(rustdoc::broken_intra_doc_links)]

use clap::Parser;
/// Clap helper to require listener address as a required positional argument `listen_address`,
/// for `clap(flatten)`-ing into your primary options struct.
///
/// Also invides a number of additional optional options to adjust the way it listens.
///
/// Provides documentation about how to specify listening addresses into `--help`.
///
/// Example:
///
/// ```,no_run
/// # use clap::Parser;
/// #[derive(Parser)]
/// /// Doc comment here is highly adviced
/// struct Args {
///    #[clap(flatten)]
///    listener: tokio_listener::ListenerAddressPositional,
/// }
/// ```
#[derive(Parser)]
pub struct ListenerAddressPositional {
    /// Socket address to listen for incoming connections.  
    ///
    /// Various types of addresses are supported:
    ///
    /// * TCP socket address and port, like 127.0.0.1:8080 or [::]:80
    ///
    /// * UNIX socket path like /tmp/mysock or Linux abstract address like @abstract
    ///
    /// * Special keyword "inetd" for serving one connection from stdin/stdout
    ///
    /// * Special keyword "sd-listen" to accept connections from file descriptor 3 (e.g. systemd socket activation).
    ///   You can also specify a named descriptor after a colon or * to use all passed sockets (if this feature is enabled)
    ///
    #[cfg_attr(
        not(any(target_os = "linux", target_os = "android")),
        doc = "Note that this platform does not support all the modes described above."
    )]
    #[cfg_attr(
        not(feature = "user_facing_default"),
        doc = "Note that some features may be disabled by compile-time settings."
    )]
    pub listen_address: crate::ListenerAddress,

    #[allow(missing_docs)]
    #[clap(flatten)]
    pub listener_options: crate::UserOptions,
}

/// Clap helper to provide optional listener address  a named argument `--listen-address` or `-l`.
///
/// For `clap(flatten)`-ing into your primary options struct.
///
/// Also invides a number of additional optional options to adjust the way it listens.
///
/// Provides documentation about how to specify listening addresses into `--help`.
///
/// Example:
///
/// ```,no_run
/// # use clap::Parser;
/// #[derive(Parser)]
/// /// Doc comment here is highly adviced
/// struct Args {
///    #[clap(flatten)]
///    listener: tokio_listener::ListenerAddressLFlag,
/// }
/// ```
#[derive(Parser)]
pub struct ListenerAddressLFlag {
    /// Socket address to listen for incoming connections.  
    ///
    /// Various types of addresses are supported:
    ///
    /// * TCP socket address and port, like 127.0.0.1:8080 or [::]:80
    ///
    /// * UNIX socket path like /tmp/mysock or Linux abstract address like @abstract
    ///
    /// * Special keyword "inetd" for serving one connection from stdin/stdout
    ///
    /// * Special keyword "sd-listen" to accept connections from file descriptor 3 (e.g. systemd socket activation).
    ///   You can also specify a named descriptor after a colon or * to use all passed sockets (if this feature is enabled)
    ///
    #[cfg_attr(
        not(any(target_os = "linux", target_os = "android")),
        doc = "Note that this platform does not support all the modes described above."
    )]
    #[cfg_attr(
        not(feature = "user_facing_default"),
        doc = "Note that some features may be disabled by compile-time settings."
    )]
    #[clap(short = 'l', long = "listen-address")]
    pub listen_address: Option<crate::ListenerAddress>,

    #[allow(missing_docs)]
    #[clap(flatten)]
    pub listener_options: crate::UserOptions,
}

impl ListenerAddressPositional {
    #[allow(clippy::missing_errors_doc)]
    /// Simple function to activate the listener without any extra parameters (just as nodelay, retries or keepalives) based on supplied arguments.
    pub async fn bind(&self) -> std::io::Result<crate::Listener> {
        crate::Listener::bind(
            &self.listen_address,
            &crate::SystemOptions::default(),
            &self.listener_options,
        )
        .await
    }
}
impl ListenerAddressLFlag {
    /// Simple function to activate the listener (if it is set)
    /// without any extra parameters (just as nodelay, retries or keepalives) based on supplied arguments.
    pub async fn bind(&self) -> Option<std::io::Result<crate::Listener>> {
        if let Some(addr) = self.listen_address.as_ref() {
            Some(
                crate::Listener::bind(
                    addr,
                    &crate::SystemOptions::default(),
                    &self.listener_options,
                )
                .await,
            )
        } else {
            None
        }
    }
}