From 82dd07f293445e7ba6a174385dc5bd215ad4a122 Mon Sep 17 00:00:00 2001 From: DarkCat09 Date: Wed, 26 Mar 2025 15:38:31 +0400 Subject: [PATCH] refactor: move output formatter to separate mod --- src/main.rs | 20 ++++++++------------ src/output.rs | 10 ++++++++++ 2 files changed, 18 insertions(+), 12 deletions(-) create mode 100644 src/output.rs diff --git a/src/main.rs b/src/main.rs index c72c7d5..d0b0f70 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,6 +6,7 @@ use ntex::{ }; mod error; +mod output; type Result = core::result::Result; @@ -61,18 +62,13 @@ async fn poweroff(cfg: State) -> Result { #[web::get("/ping")] async fn ping() -> Result { - let out = tokio::process::Command::new("/bin/ping") - .arg("-c") - .arg("4") - .arg("1.1.1.1") - .output() - .await?; - - Ok(format!( - "exited with {}\n\nstdout:\n{}\n----\n\nstderr:\n{}", - out.status.code().unwrap_or(-1), - String::from_utf8_lossy(&out.stdout), - String::from_utf8_lossy(&out.stderr), + Ok(output::format( + tokio::process::Command::new("/bin/ping") + .arg("-c") + .arg("4") + .arg("1.1.1.1") + .output() + .await?, )) } diff --git a/src/output.rs b/src/output.rs new file mode 100644 index 0000000..524a777 --- /dev/null +++ b/src/output.rs @@ -0,0 +1,10 @@ +use std::process::Output; + +pub fn format(out: Output) -> String { + format!( + "exited with {}\n\nstdout:\n{}\n----\n\nstderr:\n{}", + out.status.code().unwrap_or(-1), + String::from_utf8_lossy(&out.stdout), + String::from_utf8_lossy(&out.stderr), + ) +}