|
|
|
use serde::de::DeserializeOwned;
|
|
|
|
use serde_json::from_slice;
|
|
|
|
use shlex::split;
|
|
|
|
use std::process::{Command, Output};
|
|
|
|
use u_lib::{datatypes::DataResult, messaging::AsMsg, utils::VecDisplay};
|
|
|
|
|
|
|
|
const PANEL_BINARY: &str = "/u_panel";
|
|
|
|
|
|
|
|
type PanelResult<T> = Result<DataResult<T>, String>;
|
|
|
|
|
|
|
|
pub struct Panel;
|
|
|
|
|
|
|
|
impl Panel {
|
|
|
|
fn run(args: &[&str]) -> Output {
|
|
|
|
Command::new(PANEL_BINARY)
|
|
|
|
.arg("--json")
|
|
|
|
.args(args)
|
|
|
|
.output()
|
|
|
|
.unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn output_argv<T: AsMsg + DeserializeOwned>(args: &[&str]) -> PanelResult<T> {
|
|
|
|
let result = Self::run(args);
|
|
|
|
from_slice(&result.stdout).map_err(|e| {
|
|
|
|
eprintln!(
|
|
|
|
"Failed to decode panel response: '{}'",
|
|
|
|
VecDisplay(result.stdout)
|
|
|
|
);
|
|
|
|
e.to_string()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn output<T: AsMsg + DeserializeOwned>(args: impl Into<String>) -> PanelResult<T> {
|
|
|
|
let splitted = split(args.into().as_ref()).unwrap();
|
|
|
|
Self::output_argv(
|
|
|
|
splitted
|
|
|
|
.iter()
|
|
|
|
.map(|s| s.as_ref())
|
|
|
|
.collect::<Vec<&str>>()
|
|
|
|
.as_ref(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn status_is_ok<T: AsMsg + DeserializeOwned>(data: PanelResult<T>) -> T {
|
|
|
|
match data.unwrap() {
|
|
|
|
DataResult::Ok(r) => r,
|
|
|
|
DataResult::Err(e) => panic!("Panel failed: {}", e),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn check_status<'s, T: AsMsg + DeserializeOwned>(args: &'s str) {
|
|
|
|
let result: PanelResult<T> = Self::output(args);
|
|
|
|
Self::status_is_ok(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn check_output<T: AsMsg + DeserializeOwned>(args: impl Into<String>) -> T {
|
|
|
|
let result = Self::output(args);
|
|
|
|
Self::status_is_ok(result)
|
|
|
|
}
|
|
|
|
}
|