process_state 1.0.0

A Rust library that lets you track, cache, and manage commands like a process manager
Documentation
# process_state

[![crates.io](https://img.shields.io/crates/v/process_state)](https://crates.io/crates/process_state)
[![docs.rs](https://docs.rs/process_state/badge.svg)](https://docs.rs/process_state/)
[![License](https://img.shields.io/badge/license-MIT%20OR%20Apache--2.0-blue.svg)](LICENSE)

`process_state` is a lightweight, cross-platform Rust library for tracking, caching, and managing child processes.  
It works on both Unix and Windows, making it ideal for build tools, infrastructure management, or any program that launches multiple commands and needs to persist their state.

---

## Features

- Track any command launched from your Rust program.
- Persist process state across sessions.
- Query running or stopped processes.
- Cross-platform (Windows and Unix).
- Easy-to-use API with minimal boilerplate.

---

## Installation

Add this to your `Cargo.toml`:

```toml
[dependencies]
process_state = "1.0.0"
````

Then run:

```bash
cargo build
```

---

## Usage

```rust
use process_state::{ProcessState, ProcessStatus};
use std::process::Command;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Initialize ProcessState
    let mut state = ProcessState::new("myapp")?;

    // Spawn a child process
    let child = if cfg!(windows) {
        Command::new("timeout").arg("5").spawn()?
    } else {
        Command::new("sleep").arg("5").spawn()?
    };

    // Track the process
    state.add_process(&child, "example", "sleep 5")?;

    // Refresh status of all tracked processes
    state.refresh()?;

    // List running processes
    for p in state.get_running() {
        println!("{} is running with PID {}", p.label, p.pid);
    }

    Ok(())
}
```

---

## API Overview

### `ProcessState`

* `new(namespace: &str) -> Result<ProcessState, Error>` – Create a new state manager with a namespace.
* `add_process(&mut self, child: &Child, label: &str, command: &str) -> Result<(), Error>` – Track a new process.
* `remove_process(&mut self, pid: u32) -> Result<(), Error>` – Stop tracking a process.
* `refresh(&mut self) -> Result<(), Error>` – Update all tracked processes’ statuses.
* `get_running(&self) -> Vec<&ProcessInfo>` – Get currently running processes.
* `get_all(&self) -> Vec<&ProcessInfo>` – Get all tracked processes.

### `ProcessInfo`

* `pid: u32` – Process ID.
* `label: String` – Label you assigned.
* `command: String` – Original command.
* `start_time: SystemTime` – Start time.
* `status: ProcessStatus` – Current status.

### `ProcessStatus`

* `Running`
* `Stopped`
* `Error(String)`

---

## License

This project is licensed under **MIT OR Apache-2.0**.

---

## Contributing

Contributions, bug reports, and feature requests are welcome!
Please submit a PR or open an issue on [GitHub](https://github.com/m-epasta/process_state).