use std::fmt;
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct BackgroundProcessId(String);
impl BackgroundProcessId {
pub fn new(value: impl Into<String>) -> Result<Self, String> {
let value = value.into();
let valid = !value.is_empty()
&& value.len() <= 64
&& value
.chars()
.all(|character| character.is_ascii_alphanumeric() || character == '-');
if !valid {
return Err(
"background process id must be 1-64 ASCII letters, digits, or hyphens".to_owned(),
);
}
Ok(Self(value))
}
#[must_use]
pub fn as_str(&self) -> &str {
&self.0
}
}
impl fmt::Display for BackgroundProcessId {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str(&self.0)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum BackgroundProcessState {
Running {
pid: u32,
},
Exited {
code: i32,
},
Killed,
}
impl BackgroundProcessState {
#[must_use]
pub fn label(&self) -> String {
match self {
Self::Running { pid } => format!("running pid {pid}"),
Self::Exited { code } => format!("exited {code}"),
Self::Killed => "killed".to_owned(),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BackgroundProcessSummary {
id: BackgroundProcessId,
display_name: Option<String>,
command: String,
cwd: Option<String>,
state: BackgroundProcessState,
}
impl BackgroundProcessSummary {
#[must_use]
pub fn new(
id: BackgroundProcessId,
command: impl Into<String>,
cwd: Option<String>,
state: BackgroundProcessState,
) -> Self {
Self {
id,
display_name: None,
command: command.into(),
cwd,
state,
}
}
#[must_use]
pub fn with_display_name(mut self, display_name: Option<String>) -> Self {
self.display_name = display_name
.map(|value| value.trim().to_owned())
.filter(|value| !value.is_empty());
self
}
#[must_use]
pub fn id(&self) -> &BackgroundProcessId {
&self.id
}
#[must_use]
pub fn display_name(&self) -> Option<&str> {
self.display_name.as_deref()
}
#[must_use]
pub fn display_label(&self) -> &str {
self.display_name.as_deref().unwrap_or(self.id.as_str())
}
#[must_use]
pub fn command(&self) -> &str {
&self.command
}
#[must_use]
pub fn cwd(&self) -> Option<&str> {
self.cwd.as_deref()
}
#[must_use]
pub fn state(&self) -> &BackgroundProcessState {
&self.state
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BackgroundProcessOutput {
summary: BackgroundProcessSummary,
stdout: String,
stderr: String,
stdout_truncated: bool,
stderr_truncated: bool,
}
impl BackgroundProcessOutput {
#[must_use]
pub fn new(
summary: BackgroundProcessSummary,
stdout: impl Into<String>,
stderr: impl Into<String>,
stdout_truncated: bool,
stderr_truncated: bool,
) -> Self {
Self {
summary,
stdout: stdout.into(),
stderr: stderr.into(),
stdout_truncated,
stderr_truncated,
}
}
#[must_use]
pub fn summary(&self) -> &BackgroundProcessSummary {
&self.summary
}
#[must_use]
pub fn stdout(&self) -> &str {
&self.stdout
}
#[must_use]
pub fn stderr(&self) -> &str {
&self.stderr
}
#[must_use]
pub const fn stdout_truncated(&self) -> bool {
self.stdout_truncated
}
#[must_use]
pub const fn stderr_truncated(&self) -> bool {
self.stderr_truncated
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BackgroundProcessStop {
summary: BackgroundProcessSummary,
changed: bool,
}
impl BackgroundProcessStop {
#[must_use]
pub fn new(summary: BackgroundProcessSummary, changed: bool) -> Self {
Self { summary, changed }
}
#[must_use]
pub fn summary(&self) -> &BackgroundProcessSummary {
&self.summary
}
#[must_use]
pub const fn changed(&self) -> bool {
self.changed
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BackgroundProcessLookupError {
id: BackgroundProcessId,
known: Vec<BackgroundProcessId>,
}
impl BackgroundProcessLookupError {
#[must_use]
pub fn new(id: BackgroundProcessId, known: Vec<BackgroundProcessId>) -> Self {
Self { id, known }
}
#[must_use]
pub fn message(&self) -> String {
if self.known.is_empty() {
format!(
"No background process `{}`. No background processes are retained.",
self.id
)
} else {
let known = self
.known
.iter()
.map(BackgroundProcessId::as_str)
.collect::<Vec<_>>()
.join(", ");
format!("No background process `{}`. Known: {known}.", self.id)
}
}
}
pub trait BackgroundProcessControl: Send + Sync {
fn list(&self) -> Vec<BackgroundProcessSummary>;
fn read(
&self,
id: &BackgroundProcessId,
) -> Result<BackgroundProcessOutput, BackgroundProcessLookupError>;
fn stop(
&self,
id: &BackgroundProcessId,
) -> Result<BackgroundProcessStop, BackgroundProcessLookupError>;
}