From e52132c8c81aced28256fb9d6b66bdcd130e7e94 Mon Sep 17 00:00:00 2001 From: Logan Saso Date: Thu, 16 May 2019 10:53:57 -0700 Subject: [PATCH] main: refactored parameter parsing CONTRIBUTING.md: removed a space to suport markdown linking Refactored cmdline and net_params arguments to use option adapters to achieve the same parsed results in a "more rusty" way. Deleted a space in the contributing markdown to link properly. Signed-off-by: Logan Saso --- CONTRIBUTING.md | 2 +- src/main.rs | 39 +++++++++++++++------------------------ 2 files changed, 16 insertions(+), 25 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 7585808d1..6211fc327 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -10,7 +10,7 @@ for each submitted Pull Request (PR). ## Certificate of Origin -In order to get a clear contribution chain of trust we use the [signed-off-by language] (https://01.org/community/signed-process) +In order to get a clear contribution chain of trust we use the [signed-off-by language](https://01.org/community/signed-process) used by the Linux kernel project. ## Patch format diff --git a/src/main.rs b/src/main.rs index aa2292886..0abe1fab9 100755 --- a/src/main.rs +++ b/src/main.rs @@ -75,38 +75,29 @@ fn main() { .expect("Missing argument: disk"); let disk_path = disk_arg.as_path(); - let cmdline = if cmd_arguments.is_present("cmdline") { - cmd_arguments - .value_of("cmdline") - .map(std::string::ToString::to_string) - .unwrap() - } else { - String::new() - }; + let cmdline = cmd_arguments + .value_of("cmdline") + .map(std::string::ToString::to_string) + .unwrap_or_else(String::new); - let mut net_params = None; - if cmd_arguments.is_present("net") { - if let Some(net) = cmd_arguments.value_of("net") { - net_params = Some(net.to_string()); - } else { - net_params = Some(String::new()) - } - } + let net_params = cmd_arguments + .value_of("net") + .map(std::string::ToString::to_string); let rng_path = match cmd_arguments.occurrences_of("rng") { 0 => None, _ => Some(cmd_arguments.value_of("rng").unwrap().to_string()), }; - let mut vcpus = DEFAULT_VCPUS; - if let Some(cpus) = cmd_arguments.value_of("cpus") { - vcpus = cpus.parse::().unwrap(); - } + let vcpus = cmd_arguments + .value_of("cpus") + .and_then(|c| c.parse::().ok()) + .unwrap_or(DEFAULT_VCPUS); - let mut memory = DEFAULT_MEMORY; - if let Some(mem) = cmd_arguments.value_of("memory") { - memory = mem.parse::().unwrap(); - } + let memory = cmd_arguments + .value_of("memory") + .and_then(|m| m.parse::().ok()) + .unwrap_or(DEFAULT_MEMORY); println!( "Cloud Hypervisor Guest\n\tvCPUs: {}\n\tMemory: {} MB\n\tKernel: {:?}\n\tKernel cmdline: {}\n\tDisk: {:?}",