use std::time::{Duration, Instant};
use clap::{Parser, ValueEnum};
use nexmark::event::{Event, EventType};
use nexmark::EventGenerator;
#[derive(Debug, Parser)]
pub struct Args {
#[clap(short, long = "type", value_enum, default_value = "all")]
type_: Type,
#[clap(short, long)]
number: Option<usize>,
#[clap(long, default_value = "0")]
offset: u64,
#[clap(long, default_value = "1")]
step: u64,
#[clap(long, value_enum, default_value = "json")]
format: Format,
#[clap(long)]
no_wait: bool,
}
#[derive(Debug, Clone, Copy, ValueEnum)]
enum Type {
All,
Person,
Auction,
Bid,
}
#[derive(Debug, Clone, Copy, ValueEnum)]
enum Format {
Json,
Rust,
}
fn main() {
let opts = Args::parse();
let number = opts.number.unwrap_or(usize::MAX);
let iter = EventGenerator::default()
.with_offset(opts.offset)
.with_step(opts.step);
let iter = match opts.type_ {
Type::All => iter,
Type::Person => iter.with_type_filter(EventType::Person),
Type::Auction => iter.with_type_filter(EventType::Auction),
Type::Bid => iter.with_type_filter(EventType::Bid),
};
let start_time = Instant::now();
let start_ts = iter.timestamp();
for event in iter.take(number) {
if !opts.no_wait {
let emit_time = start_time + Duration::from_millis(event.timestamp() - start_ts);
if let Some(t) = emit_time.checked_duration_since(Instant::now()) {
std::thread::sleep(t);
}
}
match opts.format {
Format::Json => println!("{}", serde_json::to_string(&event).unwrap()),
Format::Rust => match &event {
Event::Person(e) => println!("{e:?}"),
Event::Auction(e) => println!("{e:?}"),
Event::Bid(e) => println!("{e:?}"),
},
}
}
}