Skip to content

Commit

Permalink
release 2.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
Zheoni committed Feb 27, 2024
1 parent 3cb8af4 commit a24d120
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 100 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
57 changes: 3 additions & 54 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 8 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -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 <zheoni@outlook.es>"]
license = "MIT"
Expand All @@ -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"
Expand Down
66 changes: 37 additions & 29 deletions src/bin/rq.rs
Original file line number Diff line number Diff line change
@@ -1,66 +1,74 @@
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<String>,
/// 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<u64>,
/// Enable or disable color
#[command(flatten)]
color: colorchoice_clap::Color,
}

pub fn main() -> Result<(), Box<dyn std::error::Error>> {
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> "Seed the pseudorandom generator"))
.arg(
arg!(--color <WHEN> "Controls when to use color")
.default_value("auto")
.value_parser(clap::builder::EnumValueParser::<clap::ColorChoice>::new()),
)
.get_matches();

let color = match matches
.get_one::<clap::ColorChoice>("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::<u64>("seed").copied();
let query = matches.get_one::<String>("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);
}
}
}

let input = match &args.query {
let input = match query {
Some(q) => q,
None => "",
};

match state.run_query(input) {
Ok(output) => {
for sample in &output {
if args.hide_expr {
if quiet {
println!("{sample:#}");
} else {
println!("{sample}");
Expand Down
15 changes: 12 additions & 3 deletions src/expr/dice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
15 changes: 12 additions & 3 deletions src/expr/interval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
17 changes: 13 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}

0 comments on commit a24d120

Please sign in to comment.