Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
896 changes: 450 additions & 446 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
To use `tera-rand` in your project, include the following in your `Cargo.toml`:
```toml
[dependencies]
tera-rand = "0.1.2"
tera-rand = "0.2.1"
```

Please see [`tera-rand` documentation] for examples on using `tera-rand` functions.
Expand All @@ -25,7 +25,7 @@ This random data can be useful for tasks such as simulating traffic or populate

### Installation

You can install a `tera-rand-cli` binary from crates.io using `cargo install tera-rand-cli@0.1.1`.
You can install a `tera-rand-cli` binary from crates.io using `cargo install tera-rand-cli@0.2.1`.

Alternatively, if you would like to build from source, ensure you have Rust installed at version
1.72 or higher. Then, checkout this repository and run`cargo build --release` from the root
Expand Down
24 changes: 12 additions & 12 deletions tera-rand-cli/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "tera-rand-cli"
version = "0.2.0"
version = "0.2.1"
authors = ["Raymond Liu <philosobyte@gmail.com>"]
categories = ["template-engine", "command-line-utilities"]
description = "A CLI tool for generating a feed of random data from a Tera template"
Expand All @@ -11,16 +11,16 @@ repository = "https://github.com/Philosobyte/tera-rand"
rust-version = "1.72"

[dependencies]
anyhow = "=1.0.75"
clap = { version = "=4.4.6", features = ["derive"] }
chrono = "=0.4.31"
iso8601 = "=0.6.1"
tera = "=1.19.1"
tera-rand = { version = "=0.2.0", path = "../tera-rand" }
thiserror = "=1.0.50"
anyhow = "1.0"
clap = { version = "=4.5", features = ["derive"] }
chrono = "0.4"
iso8601 = "0.6"
tera = "1.20"
tera-rand = { version = "0.2", path = "../tera-rand" }
thiserror = "2.0"

[dev-dependencies]
assert_cmd = "=2.0.12"
regex = "=1.10.2"
tracing = "=0.1.40"
tracing-test = "=0.2.4"
assert_cmd = "2.1"
regex = "1.12"
tracing = "0.1"
tracing-test = "0.2"
8 changes: 4 additions & 4 deletions tera-rand-cli/tests/integration_test.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use assert_cmd::output::OutputError;
use assert_cmd::Command;
use assert_cmd::{cargo_bin_cmd, Command};
use regex::Regex;
use std::process::Output;
use tracing::trace;
Expand All @@ -8,7 +8,7 @@ use tracing_test::traced_test;
#[test]
#[traced_test]
fn test_simple_output_with_record_limit() {
let mut cmd: Command = Command::cargo_bin("tera-rand-cli").unwrap();
let mut cmd: Command = cargo_bin_cmd!("tera-rand-cli");
cmd.args(&["-f", "resources/test/cpu_util.json", "--record-limit", "1"]);

let output: Output = cmd.unwrap();
Expand All @@ -23,7 +23,7 @@ fn test_simple_output_with_record_limit() {
#[test]
#[traced_test]
fn test_error_when_file_not_passed_in() {
let mut cmd: Command = Command::cargo_bin("tera-rand-cli").unwrap();
let mut cmd: Command = cargo_bin_cmd!("tera-rand-cli");

let output_error: OutputError = cmd.unwrap_err();
let output: &Output = output_error.as_output().unwrap();
Expand All @@ -36,7 +36,7 @@ fn test_error_when_file_not_passed_in() {
#[test]
#[traced_test]
fn test_error_when_file_does_not_exist() {
let mut cmd: Command = Command::cargo_bin("tera-rand-cli").unwrap();
let mut cmd: Command = cargo_bin_cmd!("tera-rand-cli");
cmd.args(&["-f", "this-file-does-not-exist.json"]);

let output_error: OutputError = cmd.unwrap_err();
Expand Down
17 changes: 9 additions & 8 deletions tera-rand/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "tera-rand"
version = "0.2.0"
version = "0.2.1"
authors = ["Raymond Liu <philosobyte@gmail.com>"]
categories = ["template-engine",]
description = "A suite of random data generation functions for the Tera template engine"
Expand All @@ -12,16 +12,17 @@ rust-version = "1.72"

[dependencies]
anyhow = "1.0"
dashmap = "5.5"
lazy_static = "1.4"
rand = "0.8"
dashmap = "6.1"
lazy_static = "1.5"
rand = "0.9"
rand_distr = "0.5"
serde = "1.0"
tera = "1.19"
thiserror = "1.0"
uuid = { version = "1.5", features = ["v4"], optional = true }
tera = "1.20"
thiserror = "2.0"
uuid = { version = "1.19", features = ["v4"], optional = true }

[dev-dependencies]
regex = "1.10"
regex = "1.12"
tracing = "0.1"
tracing-test = "0.2"

Expand Down
16 changes: 8 additions & 8 deletions tera-rand/src/common.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use crate::error::arg_parse_error;
use rand::distributions::uniform::{SampleRange, SampleUniform};
use rand::distributions::Standard;
use rand::distr::uniform::{SampleRange, SampleUniform};
use rand::prelude::Distribution;
use rand::{random, thread_rng, Rng};
use rand::{random, rng, Rng};
use serde::de::DeserializeOwned;
use serde::Serialize;
use std::collections::HashMap;
use std::ops::RangeInclusive;
use rand::distr::StandardUniform;
use tera::{from_value, to_value, Result, Value};

// Parse an argument for the given `parameter` name from `args`, a map of arguments.
Expand Down Expand Up @@ -49,12 +49,12 @@ pub(crate) fn gen_value_in_range<T>(
where
T: SampleUniform,
RangeInclusive<T>: SampleRange<T>,
Standard: Distribution<T>,
StandardUniform: Distribution<T>,
{
match (start_opt, end_opt) {
(Some(start), Some(end)) => thread_rng().gen_range(start..=end),
(Some(start), None) => thread_rng().gen_range(start..=default_end),
(None, Some(end)) => thread_rng().gen_range(default_start..=end),
(Some(start), Some(end)) => rng().random_range(start..=end),
(Some(start), None) => rng().random_range(start..=default_end),
(None, Some(end)) => rng().random_range(default_start..=end),
(None, None) => random::<T>(),
}
}
Expand All @@ -70,7 +70,7 @@ pub(crate) fn parse_range_and_gen_value_in_range<T>(
where
T: SampleUniform + DeserializeOwned + Serialize,
RangeInclusive<T>: SampleRange<T>,
Standard: Distribution<T>,
StandardUniform: Distribution<T>,
{
let start_opt: Option<T> = parse_arg(args, "start")?;
let end_opt: Option<T> = parse_arg(args, "end")?;
Expand Down
4 changes: 2 additions & 2 deletions tera-rand/src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::error::{empty_file, internal_error, missing_arg, read_file_error};
use dashmap::mapref::one::Ref;
use dashmap::DashMap;
use lazy_static::lazy_static;
use rand::{thread_rng, Rng};
use rand::{rng, Rng};
use std::collections::HashMap;
use std::fs::File;
use std::io::{BufRead, BufReader};
Expand Down Expand Up @@ -39,7 +39,7 @@ pub fn random_from_file(args: &HashMap<String, Value>) -> Result<Value> {
let possible_values_ref: Ref<String, Vec<String>> = read_all_file_lines(filepath)?;
let possible_values: &Vec<String> = possible_values_ref.value();

let index_to_sample: usize = thread_rng().gen_range(0usize..possible_values.len());
let index_to_sample: usize = rng().random_range(0usize..possible_values.len());
convert_line_to_json_value(possible_values_ref.key(), possible_values, index_to_sample)
}

Expand Down
6 changes: 3 additions & 3 deletions tera-rand/src/net.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::common::{gen_value_in_range, parse_arg};
use crate::error::cidr_prefix_length_out_of_bounds;
use rand::{thread_rng, Rng};
use rand::{rng, Rng};
use std::collections::HashMap;
use std::net::{Ipv4Addr, Ipv6Addr};
use tera::{to_value, Result, Value};
Expand Down Expand Up @@ -196,7 +196,7 @@ pub fn random_ipv4_cidr(args: &HashMap<String, Value>) -> Result<Value> {
parse_cidr_prefix_length_and_check_bounds(args, "length_end", 0u32, u32::BITS)?
.unwrap_or(u32::BITS);

let random_prefix_length: u32 = thread_rng().gen_range(length_start..=length_end);
let random_prefix_length: u32 = rng().random_range(length_start..=length_end);
let bits_to_shift: u32 = u32::BITS - random_prefix_length;

let random_prefix: u32 = match bits_to_shift {
Expand Down Expand Up @@ -299,7 +299,7 @@ pub fn random_ipv6_cidr(args: &HashMap<String, Value>) -> Result<Value> {
parse_cidr_prefix_length_and_check_bounds(args, "length_end", 0u32, u128::BITS)?
.unwrap_or(u128::BITS);

let random_prefix_length: u32 = thread_rng().gen_range(length_start..=length_end);
let random_prefix_length: u32 = rng().random_range(length_start..=length_end);
let bits_to_shift: u32 = u128::BITS - random_prefix_length;

let random_prefix: u128 = match bits_to_shift {
Expand Down
16 changes: 8 additions & 8 deletions tera-rand/src/string.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::common::parse_arg;
use crate::error::unsupported_arg;
use rand::distributions::{Alphanumeric, DistString, Standard};
use rand::thread_rng;
use rand::distr::{Alphanumeric, SampleString, StandardUniform};
use rand::rng;
use std::collections::HashMap;
use tera::{to_value, Result, Value};

Expand Down Expand Up @@ -32,9 +32,9 @@ use tera::{to_value, Result, Value};
/// let rendered: String = tera
/// .render_str(r#"{{ random_string(space="alphanumeric") }}"#, &context)
/// .unwrap();
/// // use standard space
/// // use standard uniform space
/// let rendered: String = tera
/// .render_str(r#"{{ random_string(space="standard") }}"#, &context)
/// .render_str(r#"{{ random_string(space="standard-uniform") }}"#, &context)
/// .unwrap();
/// ```
pub fn random_string(args: &HashMap<String, Value>) -> Result<Value> {
Expand All @@ -44,8 +44,8 @@ pub fn random_string(args: &HashMap<String, Value>) -> Result<Value> {
parse_arg(args, "space")?.unwrap_or_else(|| String::from("alphanumeric"));

let random_string: String = match space_as_string.as_str() {
"alphanumeric" => Ok(Alphanumeric.sample_string(&mut thread_rng(), str_length)),
"standard" => Ok(Standard.sample_string(&mut thread_rng(), str_length)),
"alphanumeric" => Ok(Alphanumeric.sample_string(&mut rng(), str_length)),
"standard-uniform" => Ok(StandardUniform.sample_string(&mut rng(), str_length)),
_ => Err(unsupported_arg("space", space_as_string)),
}?;
let json_value: Value = to_value(random_string)?;
Expand Down Expand Up @@ -97,7 +97,7 @@ mod tests {
test_tera_rand_function(
random_string,
"random_string",
r#"{ "some_field": "{{ random_string(space="standard") }}" }"#,
r#"{ "some_field": "{{ random_string(space="standard-uniform") }}" }"#,
r#"\{ "some_field": ".{8}" }"#,
);
}
Expand All @@ -108,7 +108,7 @@ mod tests {
test_tera_rand_function(
random_string,
"random_string",
r#"{ "some_field": "{{ random_string(space="standard", length=12) }}" }"#,
r#"{ "some_field": "{{ random_string(space="standard-uniform", length=12) }}" }"#,
r#"\{ "some_field": ".{12}" }"#,
);
}
Expand Down