High-performance implementation of plcbundle written in Rust
at main 61 lines 2.1 kB view raw
1use anyhow::Result; 2use clap::Args; 3use std::path::PathBuf; 4 5#[derive(Args, Debug)] 6#[command( 7 about = "Output random DIDs sampled from the index", 8 alias = "rand", 9 long_about = "Sample random DIDs from the repository for testing, analysis, or statistical 10purposes. Uses the DID index to efficiently select random identifiers without 11scanning all bundles. 12 13By default outputs DIDs as newline-delimited text, one per line. Use --json 14to output as a JSON array instead. The --seed flag enables deterministic 15sampling for reproducible results across runs. 16 17This command is particularly useful for generating test datasets, performing 18statistical analysis on DID distributions, or creating random samples for 19benchmarking and performance testing.", 20 help_template = crate::clap_help!( 21 examples: " # Sample 10 random DIDs (default)\n \ 22 {bin} random\n\n \ 23 # Sample specific number\n \ 24 {bin} random --count 50\n\n \ 25 # Deterministic sampling with seed\n \ 26 {bin} random --count 20 --seed 12345\n\n \ 27 # Output as JSON array\n \ 28 {bin} random --count 10 --json\n\n \ 29 # Using short flag\n \ 30 {bin} random -n 5" 31 ) 32)] 33pub struct RandomCommand { 34 /// Number of random DIDs to output 35 #[arg(short = 'n', long = "count", default_value = "10")] 36 pub count: usize, 37 38 /// Optional deterministic seed 39 #[arg(long)] 40 pub seed: Option<u64>, 41 42 /// Emit JSON array instead of newline-delimited text 43 #[arg(long)] 44 pub json: bool, 45} 46 47pub fn run(cmd: RandomCommand, dir: PathBuf) -> Result<()> { 48 let manager = super::utils::create_manager(dir, false, false, false)?; 49 let count = cmd.count.max(1); 50 let dids = manager.sample_random_dids(count, cmd.seed)?; 51 52 if cmd.json { 53 println!("{}", serde_json::to_string_pretty(&dids)?); 54 } else { 55 for did in dids { 56 println!("{}", did); 57 } 58 } 59 60 Ok(()) 61}