use {
serde::{Deserialize, Serialize},
std::path::PathBuf,
};
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct InstanceInfo {
pub name: String,
pub pid: u32,
pub transport: TransportInfo,
pub started_at: u64,
pub cwd: Option<PathBuf>,
}
impl InstanceInfo {
#[must_use]
pub fn new(name: String, pid: u32, transport: TransportInfo) -> Self {
Self {
name,
pid,
transport,
started_at: std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map_or(0, |d| d.as_secs()),
cwd: std::env::current_dir().ok(),
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(tag = "type", content = "address")]
pub enum TransportInfo {
Tcp {
host: String,
port: u16,
},
Local {
path: String,
},
}
impl TransportInfo {
#[must_use]
pub fn tcp(host: impl Into<String>, port: u16) -> Self {
Self::Tcp {
host: host.into(),
port,
}
}
#[must_use]
pub fn local(path: impl Into<String>) -> Self {
Self::Local { path: path.into() }
}
#[must_use]
pub fn display(&self) -> String {
match self {
Self::Tcp { host, port } => format!("{host}:{port}"),
Self::Local { path } => path.clone(),
}
}
}
impl std::fmt::Display for TransportInfo {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.display())
}
}
#[cfg(test)]
#[path = "info_tests.rs"]
mod tests;