PortRange

Struct PortRange 

Source
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

  • min must be less than or equal to max
  • 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: u16

The minimum port number (inclusive).

§max: u16

The maximum port number (inclusive).

Implementations§

Source§

impl PortRange

Source

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());
Source

pub fn single(port: u16) -> Self

Creates a port range containing only a single port.

§Arguments
  • port - The single port number
§Example
use stem_rs::exit_policy::PortRange;

let ssh = PortRange::single(22);
assert!(ssh.contains(22));
assert!(!ssh.contains(23));
assert_eq!(ssh.to_string(), "22");
Source

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(), "*");
Source

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 max
Source

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());

Trait Implementations§

Source§

impl Clone for PortRange

Source§

fn clone(&self) -> PortRange

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for PortRange

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for PortRange

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl PartialEq for PortRange

Source§

fn eq(&self, other: &PortRange) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Eq for PortRange

Source§

impl StructuralPartialEq for PortRange

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.