mqrs/src/main.rs

84 lines
1.9 KiB
Rust

use anyhow::Result;
use clap::Parser;
use log::Level;
use std::env;
mod posix;
mod sysv;
#[derive(Debug, Parser)]
enum Backend {
/// Handle POSIX message queues
#[clap(subcommand)]
Posix(PosixCommand),
/// Handle SysV message queues
#[clap(subcommand)]
Sysv(SysvCommand),
}
#[derive(Debug, Parser)]
enum PosixCommand {
Create(posix::Create),
Info(posix::Info),
List(posix::List),
Unlink(posix::Unlink),
Send(posix::Send),
Recv(posix::Recv),
}
#[derive(Debug, Parser)]
enum SysvCommand {
Create(sysv::Create),
Info(sysv::Info),
List(sysv::List),
Unlink(sysv::Unlink),
Send(sysv::Send),
Recv(sysv::Recv),
}
#[derive(Debug, Parser)]
#[clap(
arg_required_else_help = true,
infer_subcommands = true,
subcommand_required = true
)]
struct Opts {
/// Set a log level
#[arg(short, long, value_name = "LEVEL", default_value_t = Level::Info)]
pub log_level: Level,
/// Backend to be used
#[clap(subcommand)]
backend: Backend,
}
fn main() -> Result<()> {
let opts: Opts = Opts::parse();
if env::var("RUST_LOG").is_err() {
env::set_var("RUST_LOG", format!("{}", opts.log_level));
}
env_logger::init();
match opts.backend {
Backend::Posix(p) => match p {
PosixCommand::Create(c) => c.run()?,
PosixCommand::Info(i) => i.run()?,
PosixCommand::List(l) => l.run()?,
PosixCommand::Unlink(u) => u.run()?,
PosixCommand::Send(s) => s.run()?,
PosixCommand::Recv(r) => r.run()?,
},
Backend::Sysv(s) => match s {
SysvCommand::Create(c) => c.run()?,
SysvCommand::Info(i) => i.run()?,
SysvCommand::List(l) => l.run()?,
SysvCommand::Unlink(u) => u.run()?,
SysvCommand::Send(s) => s.run()?,
SysvCommand::Recv(r) => r.run()?,
},
}
Ok(())
}