toe_beans/v4/client/
config.rs

1use std::net::{Ipv4Addr, SocketAddrV4};
2
3use clap::{Parser, Subcommand};
4
5use super::RESOLVED_CLIENT_PORT;
6
7/// A DHCPv4 client
8#[derive(Parser, Debug)]
9#[command(version)]
10pub struct Arguments {
11    /// There is a command for each type of interaction a client would want to make with a server
12    #[command(subcommand)]
13    pub command: Commands,
14    /// These are global arguments that apply to all commands and not just a single one.
15    #[command(flatten)]
16    pub client_config: Option<ClientConfig>,
17}
18
19/// Configuration that can be passed in via the command line or programmatically to a Client
20#[derive(Parser, Debug)]
21pub struct ClientConfig {
22    /// The name of the network interface to try broadcasting to.
23    #[arg(long)]
24    pub interface: Option<String>,
25    /// The client will bind to this address and port.
26    #[arg(long)]
27    pub listen_address: Option<SocketAddrV4>,
28}
29
30impl Default for ClientConfig {
31    fn default() -> Self {
32        Self {
33            interface: None,
34            listen_address: Some(SocketAddrV4::new(
35                Ipv4Addr::new(0, 0, 0, 0),
36                RESOLVED_CLIENT_PORT,
37            )),
38        }
39    }
40}
41
42impl ClientConfig {
43    /// Combine two ClientConfig, consuming them in the process.
44    /// For `a.merge(b)`, b's value is used if a's value is None.
45    pub fn merge(self, other: Self) -> Self {
46        Self {
47            interface: self.interface.or(other.interface),
48            listen_address: self.listen_address.or(other.listen_address),
49        }
50    }
51}
52
53// #[allow(clippy::large_enum_variant)]
54/// All subcommands
55#[derive(Subcommand, Debug)]
56pub enum Commands {
57    /// Lease an IP address from the server
58    Dora {},
59    /// Send a release message to the server
60    Release {},
61    /// Send an inform message to the server
62    Inform {},
63    /// Send a decline message to the server
64    Decline {},
65}