diff --git a/rustfmt.toml b/rustfmt.toml new file mode 100644 index 0000000..7530651 --- /dev/null +++ b/rustfmt.toml @@ -0,0 +1 @@ +max_width = 120 diff --git a/src/bin/shawl-child.rs b/src/bin/shawl-child.rs index c08aebb..331aede 100644 --- a/src/bin/shawl-child.rs +++ b/src/bin/shawl-child.rs @@ -3,10 +3,7 @@ use log::info; use clap::Parser; #[derive(clap::Parser, Debug)] -#[clap( - name = "shawl-child", - about = "Dummy program to test wrapping with Shawl" -)] +#[clap(name = "shawl-child", about = "Dummy program to test wrapping with Shawl")] struct Cli { /// Run forever unless forcibly killed #[clap(long)] diff --git a/src/cli.rs b/src/cli.rs index f9240aa..278e9d5 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -49,14 +49,7 @@ pub enum Priority { } impl Priority { - pub const ALL: &'static [&'static str] = &[ - "realtime", - "high", - "above-normal", - "normal", - "below-normal", - "idle", - ]; + pub const ALL: &'static [&'static str] = &["realtime", "high", "above-normal", "normal", "below-normal", "idle"]; } impl Priority { @@ -303,9 +296,7 @@ pub enum Subcommand { #[clap(long)] name: String, }, - #[clap( - about = "Run a command as a service; only works when launched by the Windows service manager" - )] + #[clap(about = "Run a command as a service; only works when launched by the Windows service manager")] Run { #[clap(flatten)] common: CommonOpts, diff --git a/src/control.rs b/src/control.rs index 5c0e412..113c023 100644 --- a/src/control.rs +++ b/src/control.rs @@ -2,12 +2,7 @@ use crate::cli::CommonOpts; use log::error; use std::io::Write; -pub fn add_service( - name: String, - cwd: Option, - dependencies: &[String], - opts: CommonOpts, -) -> Result<(), ()> { +pub fn add_service(name: String, cwd: Option, dependencies: &[String], opts: CommonOpts) -> Result<(), ()> { let shawl_path = quote( &std::env::current_exe() .expect("Unable to determine Shawl location") @@ -85,12 +80,7 @@ fn construct_shawl_run_args(name: &str, cwd: &Option, opts: &CommonOpts) }; if let Some(pass) = &opts.pass { shawl_args.push("--pass".to_string()); - shawl_args.push( - pass.iter() - .map(|x| x.to_string()) - .collect::>() - .join(","), - ); + shawl_args.push(pass.iter().map(|x| x.to_string()).collect::>().join(",")); } if let Some(cwd) = &cwd { shawl_args.push("--cwd".to_string()); diff --git a/src/main.rs b/src/main.rs index 16853e0..625f667 100644 --- a/src/main.rs +++ b/src/main.rs @@ -103,9 +103,7 @@ fn main() -> Result<(), Box> { }; if should_log { let (name, common) = match &cli.sub { - Subcommand::Add { name, common, .. } | Subcommand::Run { name, common, .. } => { - (name, common) - } + Subcommand::Add { name, common, .. } | Subcommand::Run { name, common, .. } => (name, common), }; prepare_logging( name, diff --git a/src/service.rs b/src/service.rs index fb61bab..b510bd9 100644 --- a/src/service.rs +++ b/src/service.rs @@ -3,10 +3,7 @@ use log::{debug, error, info}; use std::{io::BufRead, os::windows::process::CommandExt}; use windows_service::{ define_windows_service, - service::{ - ServiceControl, ServiceControlAccept, ServiceExitCode, ServiceState, ServiceStatus, - ServiceType, - }, + service::{ServiceControl, ServiceControlAccept, ServiceExitCode, ServiceState, ServiceStatus, ServiceType}, service_control_handler::{self, ServiceControlHandlerResult}, service_dispatcher, }; @@ -21,9 +18,7 @@ enum ProcessStatus { Terminated, } -fn check_process( - child: &mut std::process::Child, -) -> Result> { +fn check_process(child: &mut std::process::Child) -> Result> { match child.try_wait() { Ok(None) => Ok(ProcessStatus::Running), Ok(Some(status)) => match status.code() { @@ -193,9 +188,7 @@ pub fn run_service(start_arguments: Vec) -> windows_service: error!("Unable to launch command: {}", e); service_exit_code = match e.raw_os_error() { Some(win_code) => ServiceExitCode::Win32(win_code as u32), - None => { - ServiceExitCode::Win32(windows::Win32::Foundation::ERROR_PROCESS_ABORTED.0) - } + None => ServiceExitCode::Win32(windows::Win32::Foundation::ERROR_PROCESS_ABORTED.0), }; break; } @@ -209,18 +202,16 @@ pub fn run_service(start_arguments: Vec) -> windows_service: return; } if let Some(stdout) = stdout_option { - std::io::BufReader::new(stdout) - .lines() - .for_each(|line| match line { - Ok(ref x) if !x.is_empty() => { - if output_logs_need_target { - debug!(target: "{shawl-cmd}", "{}", x); - } else { - debug!("stdout: {:?}", x); - } + std::io::BufReader::new(stdout).lines().for_each(|line| match line { + Ok(ref x) if !x.is_empty() => { + if output_logs_need_target { + debug!(target: "{shawl-cmd}", "{}", x); + } else { + debug!("stdout: {:?}", x); } - _ => (), - }); + } + _ => (), + }); } }); @@ -231,18 +222,16 @@ pub fn run_service(start_arguments: Vec) -> windows_service: return; } if let Some(stderr) = stderr_option { - std::io::BufReader::new(stderr) - .lines() - .for_each(|line| match line { - Ok(ref x) if !x.is_empty() => { - if output_logs_need_target { - debug!(target: "{shawl-cmd}", "{}", x); - } else { - debug!("stderr: {:?}", x); - } + std::io::BufReader::new(stderr).lines().for_each(|line| match line { + Ok(ref x) if !x.is_empty() => { + if output_logs_need_target { + debug!(target: "{shawl-cmd}", "{}", x); + } else { + debug!("stderr: {:?}", x); } - _ => (), - }); + } + _ => (), + }); } }); @@ -255,9 +244,7 @@ pub fn run_service(start_arguments: Vec) -> windows_service: controls_accepted: ServiceControlAccept::empty(), exit_code: ServiceExitCode::NO_ERROR, checkpoint: 0, - wait_hint: std::time::Duration::from_millis( - opts.stop_timeout.unwrap_or(3000) + 1000, - ), + wait_hint: std::time::Duration::from_millis(opts.stop_timeout.unwrap_or(3000) + 1000), process_id: None, })?; @@ -339,8 +326,7 @@ pub fn run_service(start_arguments: Vec) -> windows_service: } Ok(ProcessStatus::Terminated) => { info!("Command was terminated by a signal"); - service_exit_code = - ServiceExitCode::Win32(windows::Win32::Foundation::ERROR_PROCESS_ABORTED.0); + service_exit_code = ServiceExitCode::Win32(windows::Win32::Foundation::ERROR_PROCESS_ABORTED.0); if should_restart_terminated_command(opts.restart, opts.no_restart) { break 'inner; } else { @@ -349,8 +335,7 @@ pub fn run_service(start_arguments: Vec) -> windows_service: } Err(e) => { info!("Error trying to determine command status: {:?}", e); - service_exit_code = - ServiceExitCode::Win32(windows::Win32::Foundation::ERROR_PROCESS_ABORTED.0); + service_exit_code = ServiceExitCode::Win32(windows::Win32::Foundation::ERROR_PROCESS_ABORTED.0); break 'inner; } }