Skip to content

Commit

Permalink
feat(config): Implement std::default::Default
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
BKDaugherty authored and altugbakan committed Jun 6, 2024
1 parent 4a95f6c commit 427e207
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T: Iterator<Item = String>>(mut args: T) -> Result<Config, Box<dyn Error>> {
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();

Expand Down

0 comments on commit 427e207

Please sign in to comment.