Skip to main content

aprender_zram_cli/
lib.rs

1//! trueno-zram CLI - zramctl replacement with SIMD acceleration.
2//!
3//! The command surface lives here rather than in `main.rs` so that something
4//! other than the `trueno-zram` binary can reach it. A command enum declared in
5//! a binary target is importable by nothing: the standalone binary was the only
6//! way to run any of this, which is exactly what the APR-MONO consolidation is
7//! meant to end. `apr zram <cmd>` and `trueno-zram <cmd>` now call the SAME
8//! [`dispatch`], so the two surfaces cannot drift.
9
10#![deny(missing_docs)]
11#![deny(clippy::panic)]
12#![warn(clippy::all, clippy::pedantic)]
13
14pub mod commands;
15pub mod output;
16
17use clap::{Parser, Subcommand};
18use std::process::ExitCode;
19
20/// trueno-zram: SIMD-accelerated zram management
21#[derive(Parser)]
22#[command(name = "trueno-zram")]
23#[command(author, version, about, long_about = None)]
24pub struct Cli {
25    /// Output format
26    #[arg(long, default_value = "table")]
27    pub format: output::OutputFormat,
28
29    /// The command to run
30    #[command(subcommand)]
31    pub command: Commands,
32}
33
34/// Every zram management operation the CLI offers.
35#[derive(Subcommand, Debug)]
36pub enum Commands {
37    /// Create and configure a zram device
38    Create(commands::CreateArgs),
39
40    /// Remove a zram device
41    Remove(commands::RemoveArgs),
42
43    /// Show zram device status
44    Status(commands::StatusArgs),
45
46    /// Run compression benchmarks
47    Benchmark(commands::BenchmarkArgs),
48}
49
50/// Parse `argv` and run one command. This is the whole of the standalone
51/// binary.
52#[must_use]
53pub fn run() -> ExitCode {
54    let cli = Cli::parse();
55    match dispatch(&cli.command, cli.format) {
56        Ok(()) => ExitCode::SUCCESS,
57        Err(e) => {
58            eprintln!("Error: {e}");
59            ExitCode::FAILURE
60        }
61    }
62}
63
64/// Run one already-parsed command.
65///
66/// Split out from [`run`] so a caller that did its own parsing -- `apr zram` --
67/// executes the identical code path instead of a copy of it. Returns the error
68/// rather than an exit code so each front end can report it in its own idiom.
69///
70/// # Errors
71/// Propagates whatever the selected command returns.
72pub fn dispatch(
73    command: &Commands,
74    format: output::OutputFormat,
75) -> Result<(), Box<dyn std::error::Error>> {
76    match command {
77        Commands::Create(args) => commands::create(args),
78        Commands::Remove(args) => commands::remove(args),
79        Commands::Status(args) => commands::status(args, format),
80        Commands::Benchmark(args) => commands::benchmark(args),
81    }
82}