Skip to content

Commit 62ffa68

Browse files
committed
feat(seeding): add a flag to pass a seed value to the CLI
1 parent 4cf7995 commit 62ffa68

2 files changed

Lines changed: 30 additions & 22 deletions

File tree

src/clap.rs

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -49,35 +49,43 @@ pub struct CliArgs {
4949
/// Debug errors with more precise information.
5050
#[arg(short, long, default_value_t = false)]
5151
debug: bool,
52+
/// Generate with a given random seed
53+
#[arg(short, long)]
54+
seed: Option<u64>,
5255
}
5356

5457
impl CliArgs {
5558
/// Check if the sequence of commands given are meaningful
5659
fn check_arguments(&self) -> Res<()> {
5760
let provided = self.list_provided();
5861

59-
Err(
60-
if self.list
61-
&& let Some(other) = find_other_than(&provided, &["list", "user", "type"])
62-
{
63-
Error::ConflictingArgs("list", other)
64-
} else if self.interactive
65-
&& let Some(other) = find_other_than(&provided, &["interactive", "user", "type"])
66-
{
67-
Error::ConflictingArgs("interactive", other)
68-
} else if self.values.is_some()
69-
&& let Some(other) = find_other_than(&provided, &["values", "user", "type"])
70-
{
71-
Error::ConflictingArgs("values", other)
72-
} else if self.json.is_some() && self.schema_file != "schema.json" {
73-
Error::ConflictingArgs("json", "schema")
74-
} else {
75-
return Ok(());
76-
},
77-
)
62+
let conflicting_arguments = if self.list
63+
&& let Some(other) = find_other_than(&provided, &["list", "user", "type"])
64+
{
65+
("list", other)
66+
} else if self.interactive
67+
&& let Some(other) = find_other_than(&provided, &["interactive", "user", "type"])
68+
{
69+
("interactive", other)
70+
} else if self.values.is_some()
71+
&& let Some(other) = find_other_than(&provided, &["values", "user", "type"])
72+
{
73+
("values", other)
74+
} else if self.json.is_some() && self.schema_file != "schema.json" {
75+
("json", "schema")
76+
} else if self.seed.is_some() && self.list {
77+
("seed", "list")
78+
} else {
79+
return Ok(());
80+
};
81+
82+
Err(Error::ConflictingArgs(conflicting_arguments.0, conflicting_arguments.1))
7883
}
7984

80-
/// Count the number of provided arguments
85+
/// Count the number of provided arguments that may invalidate others
86+
///
87+
/// This excludes flags, like `--debug`, or options that can always be
88+
/// passed, like `--seed`.
8189
fn list_provided(&self) -> Vec<&'static str> {
8290
let mut provided = Vec::with_capacity(5);
8391

@@ -136,7 +144,7 @@ impl CliArgs {
136144
/// Run the generation based on the parsed CLI arguments.
137145
fn run(self) -> Res<String> {
138146
self.check_arguments()?;
139-
let mut data = Data::new(self.user_defined)?;
147+
let mut data = Data::new(self.user_defined, self.seed)?;
140148

141149
if let Some(data_type) = self.values {
142150
return data.values(&data_type);

src/generator.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ impl<Rng: RngCore> Data<Rng> {
244244

245245
impl Data<ThreadRng> {
246246
/// Build the [`Data`] handler from user inputs
247-
pub fn new(input_data: Vec<String>) -> Res<Self> {
247+
pub fn new(input_data: Vec<String>, _seed: Option<u64>) -> Res<Self> {
248248
let mut user_defined = HashMap::new();
249249

250250
for data_type in input_data {

0 commit comments

Comments
 (0)