pub mod implement;
use std::{fmt::Display, marker::PhantomData};
#[derive(Debug)]
pub struct External;
#[derive(Debug)]
pub struct Internal;
#[derive(Debug)]
pub struct Holding;
#[derive(Debug, Clone)]
pub struct Process<T = Holding> {
pub(crate) pid: u32,
#[cfg(windows)]
handl: isize,
pub(crate) mrk: PhantomData<T>,
}
use crate::sigscan::SigScan;
#[derive(Debug, thiserror::Error)]
pub enum ProcessError {
#[error("process not found: {0}")]
UnableToFindProcess(U32OrString),
#[error("unable to open process: {0}")]
UnableToOpenProcess(U32OrString),
#[error("unable to get task, are you running as root?")]
UnableToGetTask,
}
#[derive(Debug)]
pub enum U32OrString {
U32(u32),
String(String),
}
impl Display for U32OrString {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
U32OrString::U32(x) => write!(f, "int {}", x),
U32OrString::String(x) => write!(f, "str {}", x),
}
}
}
impl<T> Process<T> {
pub fn get_pid(&self) -> u32 {
self.pid
}
}
impl Process<Holding> {
pub fn this_process() -> Process<Internal> {
Process::<Internal>::new()
}
pub fn find_pid(pid: u32) -> Result<Process<External>, ProcessError> {
Process::<External>::try_from(pid)
}
pub fn find_name(name: &str) -> Result<Process<External>, ProcessError> {
Process::<External>::try_from(name)
}
}
impl TryFrom<u32> for Process<External> {
type Error = crate::structures::process::ProcessError;
fn try_from(value: u32) -> Result<Self, Self::Error> {
Self::find_by_pid(value)
}
}
impl TryFrom<&str> for Process<External> {
type Error = crate::structures::process::ProcessError;
fn try_from(value: &str) -> Result<Self, Self::Error> {
Self::find_by_name(value)
}
}
impl SigScan for Process<External> {}
trait Proc {
fn find_by_name(name: &str) -> Result<Process<External>, ProcessError>;
fn find_by_pid(pid: u32) -> Result<Process<External>, ProcessError>;
}