| Crates.io | linux-top-parser-rs |
| lib.rs | linux-top-parser-rs |
| version | 0.2.0 |
| created_at | 2025-10-18 04:05:53.33939+00 |
| updated_at | 2025-10-18 04:05:53.33939+00 |
| description | A lightweight Rust library for parsing Linux `top` command output into structured data types and JSON. |
| homepage | https://github.com/tim/linux-top-parser-rs |
| repository | https://github.com/tim/linux-top-parser-rs |
| max_upload_size | |
| id | 1888784 |
| size | 408,682 |
A lightweight Rust library for parsing Linux top command output into structured data types and JSON.
top output (single or multi-snapshot) into typed Rust structsnom parser combinator library for reliable parsingindexmapAdd to your Cargo.toml:
[dependencies]
linux-top-parser-rs = "0.1.0"
use linux_top_parser_rs::parse_multiple_top_info_blocks;
use std::fs;
fn main() {
let input = fs::read_to_string("TOP_CPU_TEXT_FILE_PATH.txt")
.expect("Failed to read file");
let (_, top_info_blocks) = parse_multiple_top_info_blocks(&input)
.expect("Parse failed");
}
The repository includes a complete example program:
# Parse the default sample file
cargo run --example parse_top_output
# Parse a custom top output file
cargo run --example parse_top_output -- path/to/your/top_output.txt
Example implementation: examples/parse_top_output.rs
Run the full test suite:
cargo test
The tests use sample top outputs from the data/ directory and validate against expected JSON outputs like data/multi_expected.json and data/multi_all_expected.json.
# Install cargo-llvm-cov
cargo install cargo-llvm-cov
# Generate coverage report
cargo llvm-cov --html --open
All types are defined in src/parser/using_nom.rs. Key structures:
TaskStates — Counts for total, running, sleeping, stopped, and zombie processesCpuStates — CPU time percentages (user, system, nice, idle, iowait, hardware interrupts, software interrupts, steal)PhysicalMemory — Physical RAM metrics (total, free, used, buffers/cache)VirtualMemory — Swap space metricsLoadAverage & UpTimeAndLoadAverage — System load and uptime from the header line