You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

61 lines
1.7 KiB

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