|
| 1 | +//! `rsp ohh anonymize` — stream-anonymize an OHH hand-history file. |
| 2 | +//! |
| 3 | +//! This module is intentionally thin: it maps CLI flags onto an |
| 4 | +//! [`AnonymizeConfig`], opens I/O streams, and delegates the actual |
| 5 | +//! work to [`rs_poker::open_hand_history::anonymize::anonymize_stream`]. |
| 6 | +use std::fs::File; |
| 7 | +use std::io::{self, BufRead, BufReader, BufWriter, Write}; |
| 8 | +use std::path::PathBuf; |
| 9 | +use std::time::Duration; |
| 10 | + |
| 11 | +use clap::{Args, ValueEnum}; |
| 12 | +use rs_poker::open_hand_history::anonymize::{ |
| 13 | + AnonymizeConfig, Anonymizer, NameStrategy, StreamError, TimeFuzzConfig, anonymize_stream, |
| 14 | +}; |
| 15 | + |
| 16 | +/// Name-strategy choices exposed on the command line. |
| 17 | +/// |
| 18 | +/// Mirrors [`NameStrategy`] but has its own `clap::ValueEnum` so the |
| 19 | +/// library type doesn't take on a CLI dependency. |
| 20 | +#[derive(Debug, Clone, Copy, ValueEnum)] |
| 21 | +enum NameStrategyArg { |
| 22 | + /// Preserve original player names. |
| 23 | + Keep, |
| 24 | + /// Random names per-hand; cross-hand identity is lost. |
| 25 | + PerHand, |
| 26 | + /// Stable names across the whole stream (default). |
| 27 | + Stable, |
| 28 | +} |
| 29 | + |
| 30 | +impl From<NameStrategyArg> for NameStrategy { |
| 31 | + fn from(v: NameStrategyArg) -> Self { |
| 32 | + match v { |
| 33 | + NameStrategyArg::Keep => NameStrategy::Keep, |
| 34 | + NameStrategyArg::PerHand => NameStrategy::PerHand, |
| 35 | + NameStrategyArg::Stable => NameStrategy::Stable, |
| 36 | + } |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +/// Anonymize an Open Hand History file. |
| 41 | +/// |
| 42 | +/// Reads a JSONL `.ohh` file (or stdin with `-`) and writes an |
| 43 | +/// anonymized copy (or stdout with `-`). Memory usage stays O(one |
| 44 | +/// hand) regardless of input size. |
| 45 | +#[derive(Args, Debug)] |
| 46 | +pub struct AnonymizeArgs { |
| 47 | + /// Input `.ohh` file, or `-` for stdin. |
| 48 | + input: PathBuf, |
| 49 | + |
| 50 | + /// Output `.ohh` file, or `-` for stdout. |
| 51 | + #[arg(short, long, default_value = "-")] |
| 52 | + output: PathBuf, |
| 53 | + |
| 54 | + /// How to replace player names. |
| 55 | + #[arg(long, value_enum, default_value_t = NameStrategyArg::Stable)] |
| 56 | + names: NameStrategyArg, |
| 57 | + |
| 58 | + /// Disable site-name rotation. |
| 59 | + #[arg(long)] |
| 60 | + keep_site: bool, |
| 61 | + |
| 62 | + /// Disable network-name rotation. |
| 63 | + #[arg(long)] |
| 64 | + keep_network: bool, |
| 65 | + |
| 66 | + /// Disable internal-version rotation. |
| 67 | + #[arg(long)] |
| 68 | + keep_version: bool, |
| 69 | + |
| 70 | + /// Disable table-name rotation. |
| 71 | + #[arg(long)] |
| 72 | + keep_tables: bool, |
| 73 | + |
| 74 | + /// Disable game-number / tournament-number / tournament-name |
| 75 | + /// rotation. |
| 76 | + #[arg(long)] |
| 77 | + keep_game_numbers: bool, |
| 78 | + |
| 79 | + /// Disable timestamp fuzzing entirely. |
| 80 | + #[arg(long)] |
| 81 | + keep_times: bool, |
| 82 | + |
| 83 | + /// Maximum absolute global time shift, in minutes. |
| 84 | + #[arg(long, default_value_t = 30)] |
| 85 | + shift_minutes: u64, |
| 86 | + |
| 87 | + /// Maximum absolute per-hand jitter, in seconds. |
| 88 | + #[arg(long, default_value_t = 5)] |
| 89 | + jitter_seconds: u64, |
| 90 | + |
| 91 | + /// Optional seed for reproducible output. |
| 92 | + #[arg(long)] |
| 93 | + seed: Option<u64>, |
| 94 | +} |
| 95 | + |
| 96 | +/// Errors surfaced by `rsp ohh anonymize`. |
| 97 | +#[derive(Debug, thiserror::Error)] |
| 98 | +pub enum AnonymizeError { |
| 99 | + /// Opening the input or output file failed. |
| 100 | + #[error("I/O error: {0}")] |
| 101 | + Io(#[from] io::Error), |
| 102 | + /// The underlying [`anonymize_stream`] driver returned an error. |
| 103 | + #[error(transparent)] |
| 104 | + Stream(#[from] StreamError), |
| 105 | +} |
| 106 | + |
| 107 | +/// Entry point invoked from [`crate::ohh::run`]. |
| 108 | +pub fn run(args: AnonymizeArgs) -> Result<(), AnonymizeError> { |
| 109 | + let config = build_config(&args); |
| 110 | + let mut anonymizer = Anonymizer::new(config); |
| 111 | + |
| 112 | + let input: Box<dyn BufRead> = open_input(&args.input)?; |
| 113 | + let mut output: Box<dyn Write> = open_output(&args.output)?; |
| 114 | + |
| 115 | + let count = anonymize_stream(input, &mut output, &mut anonymizer)?; |
| 116 | + output.flush()?; |
| 117 | + |
| 118 | + eprintln!("anonymized {count} hand(s)"); |
| 119 | + Ok(()) |
| 120 | +} |
| 121 | + |
| 122 | +/// Translate CLI flags into an [`AnonymizeConfig`]. |
| 123 | +fn build_config(args: &AnonymizeArgs) -> AnonymizeConfig { |
| 124 | + let time_fuzz = if args.keep_times { |
| 125 | + None |
| 126 | + } else { |
| 127 | + Some(TimeFuzzConfig { |
| 128 | + max_global_shift: Duration::from_secs(args.shift_minutes * 60), |
| 129 | + max_per_hand_jitter: Duration::from_secs(args.jitter_seconds), |
| 130 | + }) |
| 131 | + }; |
| 132 | + |
| 133 | + AnonymizeConfig { |
| 134 | + name_strategy: args.names.into(), |
| 135 | + name_pool: None, |
| 136 | + rotate_site: !args.keep_site, |
| 137 | + rotate_network: !args.keep_network, |
| 138 | + rotate_internal_version: !args.keep_version, |
| 139 | + rotate_table_name: !args.keep_tables, |
| 140 | + rotate_game_numbers: !args.keep_game_numbers, |
| 141 | + time_fuzz, |
| 142 | + seed: args.seed, |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +/// Open an input path, treating `-` as stdin. |
| 147 | +fn open_input(path: &PathBuf) -> io::Result<Box<dyn BufRead>> { |
| 148 | + if path.as_os_str() == "-" { |
| 149 | + Ok(Box::new(BufReader::new(io::stdin().lock()))) |
| 150 | + } else { |
| 151 | + Ok(Box::new(BufReader::new(File::open(path)?))) |
| 152 | + } |
| 153 | +} |
| 154 | + |
| 155 | +/// Open an output path, treating `-` as stdout. |
| 156 | +fn open_output(path: &PathBuf) -> io::Result<Box<dyn Write>> { |
| 157 | + if path.as_os_str() == "-" { |
| 158 | + Ok(Box::new(BufWriter::new(io::stdout().lock()))) |
| 159 | + } else { |
| 160 | + Ok(Box::new(BufWriter::new(File::create(path)?))) |
| 161 | + } |
| 162 | +} |
0 commit comments