use serde::{Serialize, Deserialize};
use std::str::FromStr;
use std::fmt;
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
#[serde(rename_all = "lowercase")] pub enum ServiceStatus {
None,
Started,
Error,
}
impl FromStr for ServiceStatus {
type Err = ();
fn from_str(input: &str) -> Result<Self, Self::Err> {
match input {
"none" => Ok(ServiceStatus::None),
"started" => Ok(ServiceStatus::Started),
"error" => Ok(ServiceStatus::Error),
_ => Err(()),
}
}
}
impl fmt::Display for ServiceStatus {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ServiceStatus::None => write!(f, "none"),
ServiceStatus::Started => write!(f, "started"),
ServiceStatus::Error => write!(f, "error"),
}
}
}
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Service {
pub name: String,
pub status: ServiceStatus,
pub user: Option<String>,
pub file: String,
pub exit_code: Option<i8>,
}
impl Service {
pub fn from(json_str: &str) -> anyhow::Result<Self> {
let pkg: Self = serde_json::from_str(json_str)?;
Ok(pkg)
}
}
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct ServiceInfo {
pub name: String,
pub service_name: String,
pub running: bool,
pub loaded: bool,
pub schedulable: bool,
pub pid: Option<u32>,
pub exit_code: Option<i32>,
pub user: Option<String>,
pub status: ServiceStatus, pub file: String,
pub command: String,
pub working_dir: Option<String>,
pub root_dir: Option<String>,
pub log_path: Option<String>,
pub error_log_path: Option<String>,
pub interval: Option<String>,
pub cron: Option<String>,
}
impl ServiceInfo {
pub fn from(json_str: &str) -> anyhow::Result<Self> {
let pkg: Self = serde_json::from_str(json_str)?;
Ok(pkg)
}
}