From 5b491101ab08cda88e4b2de02cef2a96b674b11b Mon Sep 17 00:00:00 2001 From: Brendon Daugherty Date: Wed, 5 Jun 2024 11:25:51 -0700 Subject: [PATCH] feat(config): Implement std::default::Default I'd like to use Config::default in my own code when constructing a `Config`. Let's implement the Default trait so that others can use it without wrapping our struct due to ownership rules. --- src/config.rs | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/src/config.rs b/src/config.rs index f20551a..d16b831 100644 --- a/src/config.rs +++ b/src/config.rs @@ -39,21 +39,27 @@ pub struct Config { pub overwrite: bool, } +impl Default for Config { + fn default() -> Self { + Self { + ip_address: IpAddr::V4(Ipv4Addr::LOCALHOST), + port: 69, + directory: env::current_dir().unwrap_or_else(|_| env::temp_dir()), + receive_directory: Default::default(), + send_directory: Default::default(), + single_port: Default::default(), + read_only: Default::default(), + duplicate_packets: Default::default(), + overwrite: Default::default(), + } + } +} + impl Config { /// Creates a new configuration by parsing the supplied arguments. It is /// intended for use with [`env::args()`]. pub fn new>(mut args: T) -> Result> { - let mut config = Config { - ip_address: IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), - port: 69, - directory: env::current_dir().unwrap_or_else(|_| env::temp_dir()), - receive_directory: PathBuf::new(), - send_directory: PathBuf::new(), - single_port: false, - read_only: false, - duplicate_packets: 0, - overwrite: false, - }; + let mut config = Config::default(); args.next();