tokio_listener/claptools.rs
1#![allow(rustdoc::broken_intra_doc_links)]
2
3use clap::Parser;
4/// Clap helper to require listener address as a required positional argument `listen_address`,
5/// for `clap(flatten)`-ing into your primary options struct.
6///
7/// Also invides a number of additional optional options to adjust the way it listens.
8///
9/// Provides documentation about how to specify listening addresses into `--help`.
10///
11/// Example:
12///
13/// ```,no_run
14/// # use clap::Parser;
15/// #[derive(Parser)]
16/// /// Doc comment here is highly adviced
17/// struct Args {
18/// #[clap(flatten)]
19/// listener: tokio_listener::ListenerAddressPositional,
20/// }
21/// ```
22#[derive(Parser)]
23pub struct ListenerAddressPositional {
24 /// Socket address to listen for incoming connections.
25 ///
26 /// Various types of addresses are supported:
27 ///
28 /// * TCP socket address and port, like 127.0.0.1:8080 or [::]:80
29 ///
30 /// * UNIX socket path like /tmp/mysock or Linux abstract address like @abstract
31 ///
32 /// * Special keyword "inetd" for serving one connection from stdin/stdout
33 ///
34 /// * Special keyword "sd-listen" to accept connections from file descriptor 3 (e.g. systemd socket activation).
35 /// You can also specify a named descriptor after a colon or * to use all passed sockets (if this feature is enabled)
36 ///
37 #[cfg_attr(
38 not(any(target_os = "linux", target_os = "android")),
39 doc = "Note that this platform does not support all the modes described above."
40 )]
41 #[cfg_attr(
42 not(feature = "user_facing_default"),
43 doc = "Note that some features may be disabled by compile-time settings."
44 )]
45 pub listen_address: crate::ListenerAddress,
46
47 #[allow(missing_docs)]
48 #[clap(flatten)]
49 pub listener_options: crate::UserOptions,
50}
51
52/// Clap helper to provide optional listener address a named argument `--listen-address` or `-l`.
53///
54/// For `clap(flatten)`-ing into your primary options struct.
55///
56/// Also invides a number of additional optional options to adjust the way it listens.
57///
58/// Provides documentation about how to specify listening addresses into `--help`.
59///
60/// Example:
61///
62/// ```,no_run
63/// # use clap::Parser;
64/// #[derive(Parser)]
65/// /// Doc comment here is highly adviced
66/// struct Args {
67/// #[clap(flatten)]
68/// listener: tokio_listener::ListenerAddressLFlag,
69/// }
70/// ```
71#[derive(Parser)]
72pub struct ListenerAddressLFlag {
73 /// Socket address to listen for incoming connections.
74 ///
75 /// Various types of addresses are supported:
76 ///
77 /// * TCP socket address and port, like 127.0.0.1:8080 or [::]:80
78 ///
79 /// * UNIX socket path like /tmp/mysock or Linux abstract address like @abstract
80 ///
81 /// * Special keyword "inetd" for serving one connection from stdin/stdout
82 ///
83 /// * Special keyword "sd-listen" to accept connections from file descriptor 3 (e.g. systemd socket activation).
84 /// You can also specify a named descriptor after a colon or * to use all passed sockets (if this feature is enabled)
85 ///
86 #[cfg_attr(
87 not(any(target_os = "linux", target_os = "android")),
88 doc = "Note that this platform does not support all the modes described above."
89 )]
90 #[cfg_attr(
91 not(feature = "user_facing_default"),
92 doc = "Note that some features may be disabled by compile-time settings."
93 )]
94 #[clap(short = 'l', long = "listen-address")]
95 pub listen_address: Option<crate::ListenerAddress>,
96
97 #[allow(missing_docs)]
98 #[clap(flatten)]
99 pub listener_options: crate::UserOptions,
100}
101
102impl ListenerAddressPositional {
103 #[allow(clippy::missing_errors_doc)]
104 /// Simple function to activate the listener without any extra parameters (just as nodelay, retries or keepalives) based on supplied arguments.
105 pub async fn bind(&self) -> std::io::Result<crate::Listener> {
106 crate::Listener::bind(
107 &self.listen_address,
108 &crate::SystemOptions::default(),
109 &self.listener_options,
110 )
111 .await
112 }
113}
114impl ListenerAddressLFlag {
115 /// Simple function to activate the listener (if it is set)
116 /// without any extra parameters (just as nodelay, retries or keepalives) based on supplied arguments.
117 pub async fn bind(&self) -> Option<std::io::Result<crate::Listener>> {
118 if let Some(addr) = self.listen_address.as_ref() {
119 Some(
120 crate::Listener::bind(
121 addr,
122 &crate::SystemOptions::default(),
123 &self.listener_options,
124 )
125 .await,
126 )
127 } else {
128 None
129 }
130 }
131}