From 3d8ee620875085ae7e8d7ef31f4f8e3738cfdca1 Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Mon, 7 Feb 2022 18:49:59 +0300 Subject: style(rust): Format all Rust code with rustfmt Change-Id: Iab7e00cc26a4f9727d3ab98691ef379921a33052 Reviewed-on: https://cl.tvl.fyi/c/depot/+/5240 Tested-by: BuildkiteCI Reviewed-by: kanepyork Reviewed-by: Profpatsch Reviewed-by: grfn Reviewed-by: tazjin --- users/Profpatsch/execline/exec_helpers.rs | 84 ++++++++++++++++++++++--------- 1 file changed, 60 insertions(+), 24 deletions(-) (limited to 'users/Profpatsch/execline') diff --git a/users/Profpatsch/execline/exec_helpers.rs b/users/Profpatsch/execline/exec_helpers.rs index b9e1f5797386..a57cbca35391 100644 --- a/users/Profpatsch/execline/exec_helpers.rs +++ b/users/Profpatsch/execline/exec_helpers.rs @@ -1,13 +1,16 @@ -use std::os::unix::process::CommandExt; use std::ffi::OsStr; -use std::os::unix::ffi::{OsStringExt, OsStrExt}; +use std::os::unix::ffi::{OsStrExt, OsStringExt}; +use std::os::unix::process::CommandExt; pub fn no_args(current_prog_name: &str) -> () { let mut args = std::env::args_os(); // remove argv[0] let _ = args.nth(0); if args.len() > 0 { - die_user_error(current_prog_name, format!("Expected no arguments, got {:?}", args.collect::>())) + die_user_error( + current_prog_name, + format!("Expected no arguments, got {:?}", args.collect::>()), + ) } } @@ -16,31 +19,46 @@ pub fn args(current_prog_name: &str, no_of_positional_args: usize) -> Vec>())) + die_user_error( + current_prog_name, + format!( + "Expected {} arguments, got {}, namely {:?}", + no_of_positional_args, + args.len(), + args.collect::>() + ), + ) } args.map(|arg| arg.into_vec()).collect() } -pub fn args_for_exec(current_prog_name: &str, no_of_positional_args: usize) -> (Vec>, Vec>) { +pub fn args_for_exec( + current_prog_name: &str, + no_of_positional_args: usize, +) -> (Vec>, Vec>) { let mut args = std::env::args_os(); // remove argv[0] let _ = args.nth(0); let mut args = args.map(|arg| arg.into_vec()); let mut pos_args = vec![]; // get positional args - for i in 1..no_of_positional_args+1 { - pos_args.push( - args.nth(0).expect( - &format!("{}: expects {} positional args, only got {}", current_prog_name, no_of_positional_args, i)) - ); + for i in 1..no_of_positional_args + 1 { + pos_args.push(args.nth(0).expect(&format!( + "{}: expects {} positional args, only got {}", + current_prog_name, no_of_positional_args, i + ))); } // prog... is the rest of the iterator - let prog : Vec> = args.collect(); + let prog: Vec> = args.collect(); (pos_args, prog) } -pub fn exec_into_args<'a, 'b, Args, Arg, Env, Key, Val>(current_prog_name: &str, args: Args, env_additions: Env) -> ! - where +pub fn exec_into_args<'a, 'b, Args, Arg, Env, Key, Val>( + current_prog_name: &str, + args: Args, + env_additions: Env, +) -> ! +where Args: IntoIterator, Arg: AsRef<[u8]>, Env: IntoIterator, @@ -50,27 +68,40 @@ pub fn exec_into_args<'a, 'b, Args, Arg, Env, Key, Val>(current_prog_name: &str, // TODO: is this possible without collecting into a Vec first, just leaving it an IntoIterator? let args = args.into_iter().collect::>(); let mut args = args.iter().map(|v| OsStr::from_bytes(v.as_ref())); - let prog = args.nth(0).expect(&format!("{}: first argument must be an executable", current_prog_name)); + let prog = args.nth(0).expect(&format!( + "{}: first argument must be an executable", + current_prog_name + )); // TODO: same here let env = env_additions.into_iter().collect::>(); - let env = env.iter().map(|(k,v)| (OsStr::from_bytes(k.as_ref()), OsStr::from_bytes(v.as_ref()))); + let env = env + .iter() + .map(|(k, v)| (OsStr::from_bytes(k.as_ref()), OsStr::from_bytes(v.as_ref()))); let err = std::process::Command::new(prog).args(args).envs(env).exec(); - die_missing_executable(current_prog_name, format!("exec failed: {}, while trying to execing into {:?}", err, prog)); + die_missing_executable( + current_prog_name, + format!( + "exec failed: {}, while trying to execing into {:?}", + err, prog + ), + ); } /// Exit 1 to signify a generic expected error /// (e.g. something that sometimes just goes wrong, like a nix build). pub fn die_expected_error(current_prog_name: &str, msg: S) -> ! -where S: AsRef +where + S: AsRef, { - die_with(1, current_prog_name, msg) + die_with(1, current_prog_name, msg) } /// Exit 100 to signify a user error (“the user is holding it wrong”). /// This is a permanent error, if the program is executed the same way /// it should crash with 100 again. pub fn die_user_error(current_prog_name: &str, msg: S) -> ! -where S: AsRef +where + S: AsRef, { die_with(100, current_prog_name, msg) } @@ -78,14 +109,16 @@ where S: AsRef /// Exit 101 to signify an unexpected crash (failing assertion or panic). /// This is the same exit code that `panic!()` emits. pub fn die_panic(current_prog_name: &str, msg: S) -> ! -where S: AsRef +where + S: AsRef, { die_with(101, current_prog_name, msg) } /// Exit 111 to signify a temporary error (such as resource exhaustion) pub fn die_temporary(current_prog_name: &str, msg: S) -> ! -where S: AsRef +where + S: AsRef, { die_with(111, current_prog_name, msg) } @@ -93,20 +126,23 @@ where S: AsRef /// Exit 126 to signify an environment problem /// (the user has set up stuff incorrectly so the program cannot work) pub fn die_environment_problem(current_prog_name: &str, msg: S) -> ! -where S: AsRef +where + S: AsRef, { die_with(126, current_prog_name, msg) } /// Exit 127 to signify a missing executable. pub fn die_missing_executable(current_prog_name: &str, msg: S) -> ! -where S: AsRef +where + S: AsRef, { die_with(127, current_prog_name, msg) } fn die_with(status: i32, current_prog_name: &str, msg: S) -> ! - where S: AsRef +where + S: AsRef, { eprintln!("{}: {}", current_prog_name, msg.as_ref()); std::process::exit(status) -- cgit 1.4.1