diff --git a/Cargo.lock b/Cargo.lock index 032bdbd..94effc7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -26,6 +26,12 @@ dependencies = [ "memchr", ] +[[package]] +name = "anyhow" +version = "1.0.81" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0952808a6c2afd1aa8947271f3a60f1a6763c7b912d210184c5149b5cf147247" + [[package]] name = "async-broadcast" version = "0.7.0" @@ -873,6 +879,7 @@ checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" name = "steamos-manager" version = "24.3.0" dependencies = [ + "anyhow", "tokio", "zbus", ] diff --git a/Cargo.toml b/Cargo.toml index c9fee4b..944d17e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,5 +7,6 @@ edition = "2021" strip="symbols" [dependencies] +anyhow = "1" tokio = { version = "1", features = ["fs", "process", "io-std", "rt-multi-thread", "macros"] } zbus = { version = "4", features = ["tokio"] } diff --git a/src/manager.rs b/src/manager.rs index 7322cbd..51ca239 100644 --- a/src/manager.rs +++ b/src/manager.rs @@ -23,6 +23,7 @@ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +use anyhow::Result; use std::{ffi::OsStr, fmt, fs}; use tokio::{fs::File, io::AsyncWriteExt, process::Command}; use zbus::{interface, zvariant::Fd}; @@ -90,7 +91,7 @@ const MIN_BUFFER_SIZE: u32 = 100; const BOARD_NAME_PATH: &str = "/sys/class/dmi/id/board_name"; const GALILEO_NAME: &str = "Galileo"; -fn is_galileo() -> std::io::Result { +fn is_galileo() -> Result { let mut board_name = fs::read_to_string(BOARD_NAME_PATH)?; board_name = board_name.trim().to_string(); @@ -98,7 +99,7 @@ fn is_galileo() -> std::io::Result { Ok(matches) } -async fn script_exit_code(executable: &str, args: &[impl AsRef]) -> std::io::Result { +async fn script_exit_code(executable: &str, args: &[impl AsRef]) -> Result { // Run given script and return true on success let mut child = Command::new(executable).args(args).spawn()?; let status = child.wait().await?; @@ -109,7 +110,7 @@ async fn run_script( name: &str, executable: &str, args: &[impl AsRef], -) -> std::io::Result { +) -> Result { // Run given script to get exit code and return true on success. // Return false on failure, but also print an error if needed match script_exit_code(executable, args).await { @@ -124,20 +125,17 @@ async fn run_script( async fn script_output( executable: &str, args: &[impl AsRef], -) -> Result> { +) -> Result { // Run given command and return the output given let output = Command::new(executable).args(args).output(); let output = output.await?; - let s = match std::str::from_utf8(&output.stdout) { - Ok(v) => v, - Err(e) => panic!("Invalid UTF-8 sequence: {}", e), - }; + let s = std::str::from_utf8(&output.stdout)?; Ok(s.to_string()) } -async fn setup_iwd_config(want_override: bool) -> Result<(), std::io::Error> { +async fn setup_iwd_config(want_override: bool) -> std::io::Result<()> { // Copy override.conf file into place or out of place depending // on install value @@ -153,7 +151,7 @@ async fn setup_iwd_config(want_override: bool) -> Result<(), std::io::Error> { } } -async fn restart_iwd() -> std::io::Result { +async fn restart_iwd() -> Result { // First reload systemd since we modified the config most likely // othorwise we wouldn't be restarting iwd. match run_script("reload systemd", "systemctl", &["daemon-reload"]).await { @@ -174,7 +172,7 @@ async fn restart_iwd() -> std::io::Result { } } -async fn stop_tracing(should_trace: bool) -> std::io::Result { +async fn stop_tracing(should_trace: bool) -> Result { if !should_trace { return Ok(true); } @@ -190,7 +188,7 @@ async fn stop_tracing(should_trace: bool) -> std::io::Result { .await } -async fn start_tracing(buffer_size: u32, should_trace: bool) -> std::io::Result { +async fn start_tracing(buffer_size: u32, should_trace: bool) -> Result { if !should_trace { return Ok(true); }