use serde::de::DeserializeOwned; use serde_json::{from_slice, Value}; use std::fmt::{Debug, Display}; use std::process::{Command, Output}; use u_lib::{conv::bytes_to_string, datatypes::PanelResult, proc_output::ProcOutput}; const PANEL_BINARY: &str = "/u_panel"; pub struct Panel; impl Panel { fn run(args: &[&str]) -> Output { Command::new(PANEL_BINARY).args(args).output().unwrap() } pub fn output_argv(argv: &[&str]) -> PanelResult { let result = Self::run(argv); let output = ProcOutput::from_output(&result).into_vec(); from_slice(&output) .map_err(|e| { eprintln!( "Failed to decode panel response: '{}'", bytes_to_string(&output) ); e.to_string() }) .unwrap() } pub fn output( args: impl Into + Display, ) -> PanelResult { eprintln!(">>> {PANEL_BINARY} {}", &args); let splitted = shlex::split(args.into().as_ref()).unwrap(); let result = Self::output_argv( splitted .iter() .map(|s| s.as_ref()) .collect::>() .as_ref(), ); match &result { PanelResult::Ok(r) => eprintln!("<<<+ {r:02x?}"), PanelResult::Err(e) => eprintln!("<<(data: PanelResult) -> T { match data { PanelResult::Ok(r) => r, PanelResult::Err(e) => panic!("Panel failed: {}", e), } } pub fn check_status(args: impl Into + Display) { let result: PanelResult = Self::output(args); Self::status_is_ok(result); } pub fn check_output(args: impl Into + Display) -> T { let result = Self::output(args); Self::status_is_ok(result) } }