From bd0c9d711812f5672a4eec10028eb1ebcfa0debf Mon Sep 17 00:00:00 2001 From: Jannik Peters Date: Thu, 29 Aug 2024 10:51:44 +0200 Subject: [PATCH] Add basic clap scaffolding a la dnsi --- .gitignore | 13 ----------- Cargo.toml | 7 ++++++ src/args.rs | 15 +++++++++++++ src/commands/help.rs | 13 +++++++++++ src/commands/mod.rs | 19 +++++++++++++++++ src/error.rs | 51 ++++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 5 +++++ src/main.rs | 7 ++++++ 8 files changed, 117 insertions(+), 13 deletions(-) create mode 100644 Cargo.toml create mode 100644 src/args.rs create mode 100644 src/commands/help.rs create mode 100644 src/commands/mod.rs create mode 100644 src/error.rs create mode 100644 src/lib.rs create mode 100644 src/main.rs diff --git a/.gitignore b/.gitignore index d01bd1a..67bbd32 100644 --- a/.gitignore +++ b/.gitignore @@ -1,21 +1,8 @@ -# Generated by Cargo -# will have compiled files and executables debug/ target/ -# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries -# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html -Cargo.lock - # These are backup files generated by rustfmt **/*.rs.bk # MSVC Windows builds of rustc generate these, which store debugging information *.pdb - -# RustRover -# JetBrains specific template is maintained in a separate JetBrains.gitignore that can -# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore -# and can be added to the global gitignore or merged into this file. For a more nuclear -# option (not recommended) you can uncomment the following to ignore the entire idea folder. -#.idea/ \ No newline at end of file diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..07d8685 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "dnst" +version = "0.1.0" +edition = "2021" + +[dependencies] +clap = { version = "4", features = ["derive"] } diff --git a/src/args.rs b/src/args.rs new file mode 100644 index 0000000..02a8725 --- /dev/null +++ b/src/args.rs @@ -0,0 +1,15 @@ +use super::commands::Command; +use super::error::Error; + +#[derive(Clone, Debug, clap::Parser)] +#[command(version, disable_help_subcommand = true)] +pub struct Args { + #[command(subcommand)] + command: Command, +} + +impl Args { + pub fn execute(self) -> Result<(), Error> { + self.command.execute() + } +} diff --git a/src/commands/help.rs b/src/commands/help.rs new file mode 100644 index 0000000..d8b02d3 --- /dev/null +++ b/src/commands/help.rs @@ -0,0 +1,13 @@ +use crate::error::Error; + +#[derive(Clone, Debug, clap::Args)] +pub struct Help { + #[arg(value_name = "COMMAND")] + command: Option, +} + +impl Help { + pub fn execute(self) -> Result<(), Error> { + Ok(()) + } +} diff --git a/src/commands/mod.rs b/src/commands/mod.rs new file mode 100644 index 0000000..02ee94e --- /dev/null +++ b/src/commands/mod.rs @@ -0,0 +1,19 @@ +//! The command of _dnst_. + +pub mod help; + +use super::error::Error; + +#[derive(Clone, Debug, clap::Subcommand)] +pub enum Command { + /// Show the manual pages + Help(self::help::Help), +} + +impl Command { + pub fn execute(self) -> Result<(), Error> { + match self { + Self::Help(help) => help.execute(), + } + } +} diff --git a/src/error.rs b/src/error.rs new file mode 100644 index 0000000..a4d2226 --- /dev/null +++ b/src/error.rs @@ -0,0 +1,51 @@ +use std::borrow::Cow; +use std::num::ParseIntError; +use std::string::ParseError; +use std::{error, fmt, io}; + +#[derive(Clone, Debug)] +pub struct Error { + message: Cow<'static, str>, +} + +impl From<&'static str> for Error { + fn from(message: &'static str) -> Self { + Self { + message: Cow::Borrowed(message), + } + } +} + +impl From for Error { + fn from(message: String) -> Self { + Self { + message: Cow::Owned(message), + } + } +} + +impl From for Error { + fn from(_err: ParseError) -> Self { + Self::from("message parse error") + } +} + +impl From for Error { + fn from(_err: ParseIntError) -> Self { + Self::from("message parse error") + } +} + +impl From for Error { + fn from(err: io::Error) -> Self { + Self::from(err.to_string()) + } +} + +impl fmt::Display for Error { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + fmt::Display::fmt(&self.message, f) + } +} + +impl error::Error for Error {} diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..de6645c --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,5 @@ +pub use self::args::Args; + +pub mod args; +pub mod commands; +pub mod error; diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..264443b --- /dev/null +++ b/src/main.rs @@ -0,0 +1,7 @@ +use clap::Parser; + +fn main() { + if let Err(err) = dnst::Args::parse().execute() { + eprintln!("{}", err); + } +}