get_port/lib.rs
1pub mod tcp;
2mod tests;
3pub mod udp;
4
5pub struct Range {
6 pub min: u16,
7 pub max: u16,
8}
9
10type Port = u16;
11
12impl Default for Range {
13 fn default() -> Self {
14 Range {
15 min: 1024,
16 max: 65535,
17 }
18 }
19}
20
21/// A trait for defining behaviour on the lib's functions
22pub trait Ops {
23 /// Returns any port in default range.
24 ///
25 /// # Arguments
26 ///* `host` - a string slice pointing to the hostname to test the port availability on.
27 ///
28 /// # Examples
29 ///```
30 ///use get_port::tcp::TcpPort;
31 ///use get_port::udp::UdpPort;
32 ///use get_port::Ops;
33 ///
34 ///let tcp_port = TcpPort::any("127.0.0.1").unwrap();
35 ///let udp_port = UdpPort::any("127.0.0.1").unwrap();
36 /// ```
37 fn any(host: &str) -> Option<Port>;
38
39 /// Returns any port in given range.
40 ///
41 /// # Arguments
42 /// * `host` - a string slice pointing to the hostname to test the port availability on.
43 /// * `r` - the Range to choose a port from.
44 ///
45 /// # Examples
46 /// ```
47 /// use get_port::tcp::TcpPort;
48 /// use get_port::{Ops, Range};
49 /// use get_port::udp::UdpPort;
50 ///
51 /// let tcp_port = TcpPort::in_range("127.0.0.1", Range {min: 6000, max: 7000 }).unwrap();
52 /// let udp_port = UdpPort::in_range("127.0.0.1", Range {min: 8000, max: 9000 }).unwrap();
53 /// ```
54 fn in_range(host: &str, r: Range) -> Option<Port>;
55
56 /// Returns any port from the supplied list.
57 ///
58 /// # Arguments
59 /// * `host` - a string slice pointing to the hostname to test the port availability on.
60 /// * `v` - a vector of Ports (`u16`) to choose from.
61 ///
62 /// # Examples
63 /// ```
64 /// use get_port::tcp::TcpPort;
65 /// use get_port::Ops;
66 /// use get_port::udp::UdpPort;
67 ///
68 /// let tcp_port = TcpPort::from_list("127.0.0.1", vec![5000, 6000]).unwrap();
69 /// let udp_port = UdpPort::from_list("127.0.0.1", vec![5000, 6000]).unwrap();
70 /// ```
71 fn from_list(host: &str, v: Vec<Port>) -> Option<Port>;
72
73 /// Returns any port from the default range except for the ones in the supplied list.
74 ///
75 /// # Arguments
76 /// * `host` - a string slice pointing to the hostname to test the port availability on.
77 /// * `v` - a vector of Ports (`u16`) to exclude.
78 ///
79 /// # Examples
80 /// ```
81 /// use get_port::tcp::TcpPort;
82 /// use get_port::Ops;
83 /// use get_port::udp::UdpPort;
84 ///
85 /// let tcp_port = TcpPort::except("127.0.0.1", vec![1456, 6541]).unwrap();
86 /// let udp_port = UdpPort::except("127.0.0.1", vec![1456, 6541]).unwrap();
87 /// ```
88 fn except(host: &str, v: Vec<Port>) -> Option<Port>;
89
90 /// Returns any port from the supplied range except for the ones in the supplied list.
91 ///
92 /// # Arguments
93 /// * `host` - a string slice pointing to the hostname to test the port availability on.
94 /// * `r` - the Range to choose a port from.
95 /// * `v` - a vector of Ports (`u16`) to exclude.
96 ///
97 /// # Examples
98 /// ```
99 /// use get_port::tcp::TcpPort;
100 /// use get_port::{Ops, Range};
101 /// use get_port::udp::UdpPort;
102 ///
103 /// let tcp_port = TcpPort::in_range_except("127.0.0.1", Range { min: 6000, max: 7000 }, vec![6500]).unwrap();
104 /// let udp_port = UdpPort::in_range_except("127.0.0.1", Range { min: 6000, max: 7000 }, vec![6500]).unwrap();
105 /// ```
106 fn in_range_except(host: &str, r: Range, v: Vec<Port>) -> Option<Port>;
107
108 /// Utility function to check whether a port is available or not.
109 fn is_port_available(host: &str, p: Port) -> bool;
110}