Struct NetworkInterfaces

Source
pub struct NetworkInterfaces { /* private fields */ }
Expand description

Represents the collection of network interfaces defined in an interfaces(5) file.

The NetworkInterfaces struct provides methods to load, manipulate, and save network interface configurations.

§Examples

Loading and modifying interfaces:

use interface_rs::NetworkInterfaces;
use interface_rs::interface::Interface;

let mut net_ifaces = NetworkInterfaces::load("tests/interfaces").unwrap();

// Modify an interface
if let Some(iface) = net_ifaces.get_interface_mut("eth0") {
    iface.method = Some("static".to_string());
    iface.options.push(("address".to_string(), "192.168.1.100".to_string()));
}

// Save changes
net_ifaces.save().unwrap();

Implementations§

Source§

impl NetworkInterfaces

Source

pub fn load<P: AsRef<Path>>(path: P) -> Result<Self, NetworkInterfacesError>

Loads the interfaces(5) file into memory.

§Arguments
  • path - The path to the interfaces file.
§Returns

A NetworkInterfaces instance containing the parsed interfaces.

§Errors

Returns a NetworkInterfacesError if the file cannot be read or parsed.

Source

pub fn get_interface(&self, name: &str) -> Option<&Interface>

Retrieves a reference to an interface by name.

§Arguments
  • name - The name of the interface.
§Returns

An Option containing a reference to the Interface if found.

Source

pub fn get_interface_mut(&mut self, name: &str) -> Option<&mut Interface>

Retrieves a mutable reference to an interface by name.

§Arguments
  • name - The name of the interface.
§Returns

An Option containing a mutable reference to the Interface if found.

Source

pub fn add_interface(&mut self, iface: Interface)

Adds or updates an interface in the collection.

§Arguments
  • iface - The Interface to add or update.
Source

pub fn delete_interface(&mut self, name: &str)

Deletes an interface by name.

§Arguments
  • name - The name of the interface to delete.
Source

pub fn len(&self) -> usize

Returns the number of interfaces.

Source

pub fn is_empty(&self) -> bool

Checks if the collection is empty.

Source

pub fn next_unused_vlan_in_range(&self, start: u16, end: u16) -> Option<u16>

Finds the next unused VLAN ID within a specified range.

§Arguments
  • start - The starting VLAN ID (inclusive).
  • end - The ending VLAN ID (inclusive).
§Returns
  • Option<u16> - The next unused VLAN ID, or None if all are used.
Source

pub fn get_existing_vni_vlan(&self, vni_id: u32) -> Option<u16>

Retrieves the VLAN associated with an existing VNI interface.

§Arguments
  • vni_id - The VNI ID to search for (e.g., 1347682).
§Returns
  • Option<u16> - The VLAN ID specified in the bridge-access option, or None if the interface does not exist or the option is not present.
Source

pub fn get_bridge_interfaces(&self) -> Vec<String>

Retrieves all port names that have a bridge-access option defined.

§Returns

A Vec<String> containing the names of the ports with bridge-access defined.

Source

pub fn save(&mut self) -> Result<(), NetworkInterfacesError>

Saves changes back to the interfaces(5) file.

§Errors

Returns a NetworkInterfacesError if the file cannot be written or has been modified on disk.

Source

pub fn reload(&mut self) -> Result<(), NetworkInterfacesError>

Reloads the interfaces file from disk.

§Errors

Returns a NetworkInterfacesError if the file cannot be read or parsed.

Source§

impl NetworkInterfaces

Source

pub fn iter(&self) -> impl Iterator<Item = (&String, &Interface)>

Returns an iterator over the interfaces.

Trait Implementations§

Source§

impl Debug for NetworkInterfaces

Source§

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

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

impl Display for NetworkInterfaces

Source§

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

Formats the value using the given formatter. Read more

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> 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> 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.