proc_heim/process/model/
process.rs

1use std::process::ExitStatus;
2
3use tokio::process::Child;
4
5use crate::process::{
6    log_reader::LogReaderHandle, reader::MessageReaderHandle, writer::MessageWriterHandle,
7};
8
9#[derive(Debug)]
10pub(crate) struct Process {
11    pub child: Child,
12    pub message_writer: Option<MessageWriterHandle>,
13    pub message_reader: Option<MessageReaderHandle>,
14    pub log_reader: Option<LogReaderHandle>,
15}
16
17#[derive(Debug, Default)]
18pub(crate) struct ProcessBuilder {
19    pub message_writer: Option<MessageWriterHandle>,
20    pub message_reader: Option<MessageReaderHandle>,
21    pub log_reader: Option<LogReaderHandle>,
22}
23
24impl ProcessBuilder {
25    pub fn build(self, child: Child) -> Process {
26        Process {
27            child,
28            message_writer: self.message_writer,
29            message_reader: self.message_reader,
30            log_reader: self.log_reader,
31        }
32    }
33}
34
35/// Type representing information about spawned process.
36#[derive(Debug, Clone, PartialEq, Eq)]
37pub struct ProcessInfo {
38    pid: Option<u32>,
39    exit_status: Option<ExitStatus>,
40}
41
42impl ProcessInfo {
43    pub(crate) fn new(pid: Option<u32>, exit_status: Option<ExitStatus>) -> Self {
44        Self { pid, exit_status }
45    }
46
47    /// Retrieve information about OS-assigned process identifier. For more information see [`Child::id`](tokio::process::Child::id) docs.
48    pub fn pid(&self) -> Option<u32> {
49        self.pid
50    }
51
52    /// Retrieve information about process exit status.
53    /// If the process has exited, then `Some(status)` is returned.
54    /// If the exit status is not available at this time then `None` is returned.
55    pub fn exit_status(&self) -> Option<ExitStatus> {
56        self.exit_status
57    }
58
59    /// Check if process is still running. This is equivalent of `self.exit_status().is_none()`.
60    pub fn is_running(&self) -> bool {
61        self.exit_status.is_none()
62    }
63}