extern crate nix;
mod pipe;
pub mod process;
use std::path::PathBuf;
use std::time::{Duration, Instant};
use pipe::Pipe;
#[derive(Copy, Clone, PartialEq, std::fmt::Debug)]
pub enum ShellProcState {
Idle,
SubprocessRunning,
Terminated
}
#[derive(Copy, Clone, PartialEq, std::fmt::Debug)]
pub enum ShellError {
CouldNotStartProcess,
InvalidData,
IoTimeout,
ShellRunning,
ShellTerminated,
CouldNotKill,
PipeError(nix::errno::Errno)
}
#[derive(std::fmt::Debug)]
pub struct ShellProc {
pub state: ShellProcState, pub exit_status: u8, pub pid: i32, pub wrkdir: PathBuf, pub exec_time: Duration, rc: u8, uuid: String, start_time: Instant, stdout_cache: Option<String>, echo_command: String, stdin_pipe: Pipe,
stdout_pipe: Pipe,
stderr_pipe: Pipe
}
impl std::fmt::Display for ShellError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
let code_str: String = match self {
ShellError::CouldNotStartProcess => String::from("Could not start process"),
ShellError::InvalidData => String::from("Invalid data from process"),
ShellError::IoTimeout => String::from("I/O timeout"),
ShellError::ShellTerminated => String::from("Shell has terminated"),
ShellError::ShellRunning => String::from("Tried to clean shell up while still running"),
ShellError::CouldNotKill => String::from("Could not send signal to shell process"),
ShellError::PipeError(errno) => format!("Pipe error: {}", errno),
};
write!(f, "{}", code_str)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_proc_fmt_shell_error() {
assert_eq!(format!("{}", ShellError::CouldNotStartProcess), String::from("Could not start process"));
assert_eq!(format!("{}", ShellError::InvalidData), String::from("Invalid data from process"));
assert_eq!(format!("{}", ShellError::IoTimeout), String::from("I/O timeout"));
assert_eq!(format!("{}", ShellError::ShellTerminated), String::from("Shell has terminated"));
assert_eq!(format!("{}", ShellError::ShellRunning), String::from("Tried to clean shell up while still running"));
assert_eq!(format!("{}", ShellError::CouldNotKill), String::from("Could not send signal to shell process"));
assert_eq!(format!("{}", ShellError::PipeError(nix::errno::Errno::EACCES)), format!("Pipe error: {}", nix::errno::Errno::EACCES));
}
}