pub struct PortRange {
pub min: u16,
pub max: u16,
}Expand description
A range of TCP/UDP ports.
Represents a contiguous range of ports from min to max (inclusive).
Used in exit policies to specify which ports a rule applies to.
§Invariants
minmust be less than or equal tomax- Valid port values are 0-65535, though port 0 is typically not used
§Example
use stem_rs::exit_policy::PortRange;
// Create a range for common web ports
let web_ports = PortRange::new(80, 443).unwrap();
assert!(web_ports.contains(80));
assert!(web_ports.contains(443));
assert!(web_ports.contains(200));
assert!(!web_ports.contains(22));
// Create a single port
let ssh = PortRange::single(22);
assert!(ssh.contains(22));
assert!(!ssh.contains(23));
// Create a wildcard range (all ports)
let all = PortRange::all();
assert!(all.is_wildcard());
assert!(all.contains(1));
assert!(all.contains(65535));Fields§
§min: u16The minimum port number (inclusive).
max: u16The maximum port number (inclusive).
Implementations§
Source§impl PortRange
impl PortRange
Sourcepub fn new(min: u16, max: u16) -> Result<Self, Error>
pub fn new(min: u16, max: u16) -> Result<Self, Error>
Creates a new port range from minimum to maximum port.
§Arguments
min- The minimum port number (inclusive)max- The maximum port number (inclusive)
§Errors
Returns Error::Parse if min is greater than max.
§Example
use stem_rs::exit_policy::PortRange;
let range = PortRange::new(80, 443).unwrap();
assert!(range.contains(200));
// Invalid range (min > max)
assert!(PortRange::new(443, 80).is_err());Sourcepub fn all() -> Self
pub fn all() -> Self
Creates a port range covering all valid ports (1-65535).
This is equivalent to the * wildcard in exit policy rules.
§Example
use stem_rs::exit_policy::PortRange;
let all = PortRange::all();
assert!(all.is_wildcard());
assert!(all.contains(1));
assert!(all.contains(65535));
assert_eq!(all.to_string(), "*");Sourcepub fn contains(&self, port: u16) -> bool
pub fn contains(&self, port: u16) -> bool
Checks if a port is within this range.
§Arguments
port- The port number to check
§Returns
true if port is between min and max (inclusive), false otherwise.
§Example
use stem_rs::exit_policy::PortRange;
let range = PortRange::new(80, 443).unwrap();
assert!(range.contains(80)); // min boundary
assert!(range.contains(443)); // max boundary
assert!(range.contains(200)); // middle
assert!(!range.contains(79)); // below min
assert!(!range.contains(444)); // above maxSourcepub fn is_wildcard(&self) -> bool
pub fn is_wildcard(&self) -> bool
Checks if this range covers all ports (is a wildcard).
A range is considered a wildcard if it covers ports 1-65535. Port 0 is excluded as it’s not a valid destination port.
§Returns
true if this range matches any port, false otherwise.
§Example
use stem_rs::exit_policy::PortRange;
assert!(PortRange::all().is_wildcard());
assert!(PortRange::new(1, 65535).unwrap().is_wildcard());
assert!(!PortRange::new(80, 443).unwrap().is_wildcard());