From a24d12097bd8b8c589079181872300833765b2cf Mon Sep 17 00:00:00 2001 From: "Francisco J. Sanchez" Date: Tue, 27 Feb 2024 21:28:49 +0100 Subject: [PATCH] release 2.0.1 --- CHANGELOG.md | 5 ++++ Cargo.lock | 57 ++------------------------------------ Cargo.toml | 15 +++++----- src/bin/rq.rs | 66 +++++++++++++++++++++++++------------------- src/expr/dice.rs | 15 ++++++++-- src/expr/interval.rs | 15 ++++++++-- src/lib.rs | 17 +++++++++--- 7 files changed, 90 insertions(+), 100 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a65a8c7..f29152b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,11 @@ ## Unreleased - ReleaseDate +## 2.0.1 - 2023-02-27 + +Internal improvements and CLI args renames. Old arg names are still supported +but hidden and may be removed or repurposed in a future release. + ## 2.0.0 - 2023-02-27 This release focuses on simplify things while keeping the core functionality. diff --git a/Cargo.lock b/Cargo.lock index 971971c..855a6a4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -13,9 +13,9 @@ dependencies = [ [[package]] name = "anstream" -version = "0.6.12" +version = "0.6.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "96b09b5178381e0874812a9b157f7fe84982617e48f71f4e3235482775e5b540" +checksum = "d96bd03f33fe50a863e394ee9718a706f988b9079b20c3784fb726e7678b62fb" dependencies = [ "anstyle", "anstyle-parse", @@ -72,7 +72,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c918d541ef2913577a0f9566e9ce27cb35b6df072075769e0b26cb5a554520da" dependencies = [ "clap_builder", - "clap_derive", ] [[package]] @@ -87,18 +86,6 @@ dependencies = [ "strsim", ] -[[package]] -name = "clap_derive" -version = "4.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "307bc0538d5f0f83b8248db3087aa92fe504e4691294d0c96c0eabc33f47ba47" -dependencies = [ - "heck", - "proc-macro2", - "quote", - "syn", -] - [[package]] name = "clap_lex" version = "0.7.0" @@ -111,16 +98,6 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7" -[[package]] -name = "colorchoice-clap" -version = "1.0.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6fe9ad5d064caf028aeb50818a5c7857c28f746ad04111652b85d9bca8b0d0be" -dependencies = [ - "clap", - "colorchoice", -] - [[package]] name = "getrandom" version = "0.2.12" @@ -132,12 +109,6 @@ dependencies = [ "wasi", ] -[[package]] -name = "heck" -version = "0.4.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" - [[package]] name = "libc" version = "0.2.153" @@ -250,17 +221,15 @@ checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f" [[package]] name = "rng-query" -version = "2.0.0" +version = "2.0.1" dependencies = [ "anstream", "clap", - "colorchoice-clap", "owo-colors", "rand", "rand_pcg", "regex", "test-case", - "thiserror", ] [[package]] @@ -313,26 +282,6 @@ dependencies = [ "test-case-core", ] -[[package]] -name = "thiserror" -version = "1.0.57" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1e45bcbe8ed29775f228095caf2cd67af7a4ccf756ebff23a306bf3e8b47b24b" -dependencies = [ - "thiserror-impl", -] - -[[package]] -name = "thiserror-impl" -version = "1.0.57" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a953cb265bef375dae3de6663da4d3804eee9682ea80d8e2542529b73c531c81" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - [[package]] name = "unicode-ident" version = "1.0.12" diff --git a/Cargo.toml b/Cargo.toml index d96cd82..4e9ae6d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "rng-query" description = "CLI to use pseudorandomness the easy way" -version = "2.0.0" +version = "2.0.1" edition = "2021" authors = ["Zheoni "] license = "MIT" @@ -10,24 +10,25 @@ categories = ["command-line-utilities"] repository = "https://github.com/Zheoni/rng-query" readme = "README.md" - [dependencies] rand = "0.8.5" rand_pcg = "0.3.1" regex = "1.10.3" -thiserror = "1.0.47" owo-colors = "4.0.0" -anstream = "0.6.12" # bin -clap = { version = "4.5.1", features = ["derive"], optional = true } -colorchoice-clap = { version = "1.0.3", optional = true } +clap = { version = "4.5.1", features = ["cargo"], optional = true } +anstream = { version = "0.6.12", optional = true } [dev-dependencies] test-case = "3.1.0" [features] default = ["bin"] -bin = ["dep:clap", "dep:colorchoice-clap"] +bin = ["dep:clap", "dep:anstream"] + +[profile.release] +debug = "none" +strip = true [[bin]] name = "rq" diff --git a/src/bin/rq.rs b/src/bin/rq.rs index 9c00c45..0f2d5d2 100644 --- a/src/bin/rq.rs +++ b/src/bin/rq.rs @@ -1,50 +1,58 @@ use std::io::{self, BufRead, IsTerminal}; use anstream::println; -use clap::Parser; +use clap::{arg, command}; use owo_colors::OwoColorize; use rng_query::State; -#[derive(clap::Parser)] -struct Args { - /// Query to evaluate in line, skip to read from stdin - query: Option, - /// Hide expression in the output sample - #[arg(short = 'E', long)] - hide_expr: bool, - /// Eval STDIN - /// - /// By default it will be treated just as data, one entry per line. With - /// this enabled, STDIN will be a regular entry expression PER LINE. - #[arg(short = 'e', long)] - eval_stdin: bool, - /// Seed the pseudorandom generator - #[arg(long)] - seed: Option, - /// Enable or disable color - #[command(flatten)] - color: colorchoice_clap::Color, -} - pub fn main() -> Result<(), Box> { - let args = Args::parse(); - args.color.write_global(); + let matches = command!() + .arg(arg!([query] "Query to evaluate")) + .arg( + arg!(-q --quiet "Quiet, only show the selected values") + .visible_alias("only-values") + .alias("hide-expr") + .short_alias('E'), + ) + .arg(arg!(-e --eval "Evaluate STDIN lines as expressions").alias("eval-stdin")) + .arg(arg!(--seed "Seed the pseudorandom generator")) + .arg( + arg!(--color "Controls when to use color") + .default_value("auto") + .value_parser(clap::builder::EnumValueParser::::new()), + ) + .get_matches(); + + let color = match matches + .get_one::("color") + .expect("default color value") + { + clap::ColorChoice::Auto => anstream::ColorChoice::Auto, + clap::ColorChoice::Always => anstream::ColorChoice::Always, + clap::ColorChoice::Never => anstream::ColorChoice::Never, + }; + color.write_global(); + + let seed = matches.get_one::("seed").copied(); + let query = matches.get_one::("query"); + let eval_stdin = matches.get_flag("eval"); + let quiet = matches.get_flag("quiet"); - let mut state = if let Some(seed) = args.seed { + let mut state = if let Some(seed) = seed { State::with_seed(seed) } else { State::new() }; let stdin = io::stdin(); - if args.query.is_none() || !stdin.is_terminal() { + if query.is_none() || !stdin.is_terminal() { for line in stdin.lock().lines() { let line = line?; let line = line.trim(); if line.is_empty() { continue; } - if args.eval_stdin { + if eval_stdin { state.add_entry(line)?; } else { state.add_data(line); @@ -52,7 +60,7 @@ pub fn main() -> Result<(), Box> { } } - let input = match &args.query { + let input = match query { Some(q) => q, None => "", }; @@ -60,7 +68,7 @@ pub fn main() -> Result<(), Box> { match state.run_query(input) { Ok(output) => { for sample in &output { - if args.hide_expr { + if quiet { println!("{sample:#}"); } else { println!("{sample}"); diff --git a/src/expr/dice.rs b/src/expr/dice.rs index 914c8b7..17b2bdb 100644 --- a/src/expr/dice.rs +++ b/src/expr/dice.rs @@ -52,14 +52,23 @@ enum SelectWhich { } /// Error from [`Roll::from_str`] -#[derive(Debug, thiserror::Error)] +#[derive(Debug)] pub enum RollParseError { - #[error("the input is not a dice roll")] NoMatch, - #[error("invalid dice roll: {0}")] Invalid(String), } +impl std::fmt::Display for RollParseError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + RollParseError::NoMatch => f.write_str("the input is not a dice roll"), + RollParseError::Invalid(e) => write!(f, "invalid dice roll: {e}"), + } + } +} + +impl std::error::Error for RollParseError {} + impl FromStr for Roll { type Err = RollParseError; diff --git a/src/expr/interval.rs b/src/expr/interval.rs index 0ad0ef2..c5f54e7 100644 --- a/src/expr/interval.rs +++ b/src/expr/interval.rs @@ -34,14 +34,23 @@ enum IntervalKind { } /// Error from [`Interval::from_str`] -#[derive(Debug, thiserror::Error)] +#[derive(Debug)] pub enum IntervalParseError { - #[error("the input is not an interval")] NoMatch, - #[error("invalid interval: {0}")] Invalid(String), } +impl std::fmt::Display for IntervalParseError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + IntervalParseError::NoMatch => f.write_str("the input is not an interval"), + IntervalParseError::Invalid(e) => write!(f, "invalid interval: {e}"), + } + } +} + +impl std::error::Error for IntervalParseError {} + impl FromStr for Interval { type Err = IntervalParseError; diff --git a/src/lib.rs b/src/lib.rs index 686f0e6..633d829 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -113,15 +113,24 @@ impl State { } /// Query error -#[derive(Debug, thiserror::Error)] +#[derive(Debug)] pub enum Error { /// Parsing options - #[error("options: {0}")] Options(String), /// Parsing expressions - #[error("expression: {0}")] Expr(String), /// Query structure error - #[error("parsing query structure: {0}")] ParseQuery(String), } + +impl std::fmt::Display for Error { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Error::Options(e) => write!(f, "options: {e}"), + Error::Expr(e) => write!(f, "expresions: {e}"), + Error::ParseQuery(e) => write!(f, "query structure: {e}"), + } + } +} + +impl std::error::Error for Error {}