From 033563f6311ffa8ad4d2a0d00779ce4058dad767 Mon Sep 17 00:00:00 2001 From: Julio Biason Date: Tue, 7 Feb 2023 17:44:12 -0300 Subject: [PATCH] This is more in line with what I want --- mapoptiontest/src/main.rs | 46 +++++++++++++++++++++++++++------------ 1 file changed, 32 insertions(+), 14 deletions(-) diff --git a/mapoptiontest/src/main.rs b/mapoptiontest/src/main.rs index 6d18bd7..42e2fc5 100644 --- a/mapoptiontest/src/main.rs +++ b/mapoptiontest/src/main.rs @@ -1,17 +1,35 @@ -fn main() { - let all_evens = [2u8, 4, 6, 8]; - let result: Result, _> = all_evens - .iter() - .map(|value| if value % 2 == 0 { Some(value) } else { None }) - .map(|opt| opt.ok_or(format!("Not good"))) - .collect(); - println!("All Evens: {:?}", result); +use std::env; +use std::path::PathBuf; + +fn split(line: &str) -> Option<(String, Vec)> { + let mut split = line.split(' '); + let root = split.next()?; + let arguments = split.map(|x| x.to_string()).collect(); + Some((root.to_string(), arguments)) +} - let not_evens = [2, 2, 2, 2, 2, 1]; - let result: Result, _> = not_evens +fn as_command(command: &str) -> Option { + env::split_paths(&env::var_os("PATH")?).find_map(|dir| { + let full_path = dir.join(command); + if full_path.exists() { + Some(full_path) + } else { + None + } + }) +} + +fn main() { + let commands = ["ls -la", "ls", "exa", "not"]; + let result = commands .iter() - .map(|value| if value % 2 == 0 { Some(value) } else { None }) - .map(|opt| opt.ok_or(format!("Not good"))) - .collect(); - println!("Not Evens: {:?}", result); + .map(|line| split(&line)) + .map(|opt| { + let (command, args) = opt?; + let full_command = as_command(&command)?; + Some((full_command, args)) + }) + .map(|opt| opt.ok_or("Not good enough")) + .collect::, _>>(); + println!("Result: {:?}", result) }