toe-beans 0.10.0

DHCP library, client, and server
Documentation
use std::net::{Ipv4Addr, SocketAddrV4};

use clap::{Parser, Subcommand};

use super::RESOLVED_CLIENT_PORT;

/// A DHCPv4 client
#[derive(Parser, Debug)]
#[command(version)]
pub struct Arguments {
    /// There is a command for each type of interaction a client would want to make with a server
    #[command(subcommand)]
    pub command: Commands,
    /// These are global arguments that apply to all commands and not just a single one.
    #[command(flatten)]
    pub client_config: Option<ClientConfig>,
}

/// Configuration that can be passed in via the command line or programmatically to a Client
#[derive(Parser, Debug)]
pub struct ClientConfig {
    /// The name of the network interface to try broadcasting to.
    #[arg(long)]
    pub interface: Option<String>,
    /// The client will bind to this address and port.
    #[arg(long)]
    pub listen_address: Option<SocketAddrV4>,
}

impl Default for ClientConfig {
    fn default() -> Self {
        Self {
            interface: None,
            listen_address: Some(SocketAddrV4::new(
                Ipv4Addr::new(0, 0, 0, 0),
                RESOLVED_CLIENT_PORT,
            )),
        }
    }
}

impl ClientConfig {
    /// Combine two ClientConfig, consuming them in the process.
    /// For `a.merge(b)`, b's value is used if a's value is None.
    pub fn merge(self, other: Self) -> Self {
        Self {
            interface: self.interface.or(other.interface),
            listen_address: self.listen_address.or(other.listen_address),
        }
    }
}

// #[allow(clippy::large_enum_variant)]
/// All subcommands
#[derive(Subcommand, Debug)]
pub enum Commands {
    /// Lease an IP address from the server
    Dora {},
    /// Send a release message to the server
    Release {},
    /// Send an inform message to the server
    Inform {},
    /// Send a decline message to the server
    Decline {},
}