binary-ensemble 0.3.0

A CLI tool for working with and compressing ensembles of districting plans
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
use ben::decode::read::extract_assignment_ben;
use ben::decode::*;
use ben::encode::*;
use ben::{logln, BenVariant};
use clap::{Parser, ValueEnum};
use std::{
    fs::File,
    io::{self, BufReader, BufWriter, Result, Write},
    path::Path,
};
/// Defines the mode of operation.
#[derive(Parser, Debug, Clone, ValueEnum, PartialEq)]
enum Mode {
    Encode,
    XEncode,
    Decode,
    XDecode,
    Read,
    XzCompress,
    XzDecompress,
}

/// Defines the command line arguments accepted by the program.
#[derive(Parser, Debug)]
#[command(
    name = "Binary Ensemble CLI Tool",
    about = "This is a command line tool for encoding and decoding binary ensemble files.",
    version = "0.2.0"
)]
struct Args {
    /// Mode to run the program in (encode, decode, or read).
    #[arg(short, long, value_enum)]
    mode: Mode,

    /// Input file to read from.
    #[arg()]
    input_file: Option<String>,

    /// Output file to write to. Optional.
    /// If not provided, the output file will be determined
    /// based on the input file and the mode of operation.
    #[arg(short, long)]
    output_file: Option<String>,

    /// The standard behaviour is to try and derive the output file
    /// name from the input file name. If this flag is set, then this
    /// logic is ignored and the output is printed to stdout.
    /// This flag is considered a higher priority than
    /// the output_file flag, so if both are present, the output
    /// will be printed to stdout.
    #[arg(short, long)]
    print: bool,

    /// Sample number to extract. Optional.
    #[arg(short = 'n', long)]
    sample_number: Option<usize>,

    /// If input and output files are not provided,
    /// then this tells the x-encode, x-decode, and decode modes
    /// that the expected formats are BEN and XBEN
    #[arg(short = 'b', long)]
    ben_and_xben: bool,

    /// If input and output files are not provided,
    /// then this tells the x-encode and x-decode modes
    /// that the expected formats are JSONL and XBEN
    #[arg(short = 'J', long)]
    jsonl_and_xben: bool,

    /// If the input and output files are not provided,
    /// then this tells the decode mode that the expected
    /// formats are JSONL and BEN
    #[arg(short = 'j', long)]
    jsonl_and_ben: bool,

    /// When saving a file in the BEN format, the deault is to have
    /// an assignment vector saved followed by the number of repetitions
    /// of that assignment vector (this is useful for Markov chian methods
    /// like ReCom). This flag will cause the program to forgo the repetition
    /// count and just save all of the assignment vectors as they are encountered.
    #[arg(short = 'a', long)]
    save_all: bool,

    /// If the output file already exists, this flag
    /// will cause the program to overwrite it without
    /// asking the user for confirmation.
    #[arg(short = 'w', long)]
    overwrite: bool,

    /// Enables verbose printing for the CLI. Optional.
    #[arg(short, long)]
    verbose: bool,

    /// When running x-encoder, this flag will determine the number of cpus to use on the
    /// system. By default, all available cpus will be used.
    #[arg(short = 'c', long)]
    n_cpus: Option<u32>,

    /// When running x-encoder, this flag will deterimine the level of compression to use.
    /// By default, the highest level of compression will be used.
    /// Valid values are 0-9, where 0 is no compression and 9 is the highest level of compression.
    #[arg(short = 'l', long)]
    compression_level: Option<u32>,
}

fn encode_setup(
    mode: Mode,
    input_file_name: String,
    output_file_name: Option<String>,
    overwrite: bool,
) -> Result<String> {
    let extension = if mode == Mode::XEncode {
        ".xben"
    } else if mode == Mode::Encode {
        ".ben"
    } else {
        ".xz"
    };

    let out_file_name = match output_file_name {
        Some(name) => name.to_owned(),
        None => {
            if input_file_name.ends_with(".ben") && extension == ".xben" {
                input_file_name.trim_end_matches(".ben").to_owned() + extension
            } else {
                input_file_name.to_string() + extension
            }
        }
    };

    if let Err(e) = check_overwrite(&out_file_name, overwrite) {
        return Err(e);
    }

    Ok(out_file_name)
}

fn decode_setup(
    in_file_name: String,
    out_file_name: Option<String>,
    full_decode: bool,
    overwrite: bool,
) -> Result<String> {
    let out_file_name = if let Some(name) = out_file_name {
        name.to_owned()
    } else if in_file_name.ends_with(".ben") {
        in_file_name.trim_end_matches(".ben").to_owned()
    } else if in_file_name.ends_with(".xben") {
        if !full_decode {
            in_file_name.trim_end_matches(".xben").to_owned() + ".ben"
        } else {
            in_file_name.trim_end_matches(".xben").to_owned()
        }
    } else if in_file_name.ends_with(".xz") {
        eprintln!(
            "Error: Unsupported file type for decode mode {:?}. Please decompress xz files with \
            either the xz command line tool or the xz-decompress mode of this tool.",
            in_file_name
        );
        return Err(std::io::Error::from(std::io::ErrorKind::InvalidInput));
    } else {
        eprintln!(
            "Error: Unsupported file type for decode mode {:?}. Supported types are .ben and .xben.",
            in_file_name
        );
        return Err(std::io::Error::from(std::io::ErrorKind::InvalidInput));
    };

    if let Err(e) = check_overwrite(&out_file_name, overwrite) {
        return Err(e);
    }

    Ok(out_file_name)
}

fn check_overwrite(file_name: &str, overwrite: bool) -> Result<()> {
    if Path::new(file_name).exists() && !overwrite {
        eprint!(
            "File {:?} already exists, do you want to overwrite it? (y/[n]): ",
            file_name
        );
        let mut user_input = String::new();
        std::io::stdin().read_line(&mut user_input).unwrap();
        eprintln!();
        if user_input.trim().to_lowercase() != "y" {
            return Err(std::io::Error::from(std::io::ErrorKind::AlreadyExists));
        }
    }
    Ok(())
}

fn main() {
    let args = Args::parse();

    if args.verbose {
        std::env::set_var("RUST_LOG", "trace");
    }

    match args.mode {
        Mode::Encode => {
            logln!("Running in encode mode");

            let reader: Box<dyn io::BufRead>;
            let writer: Box<dyn Write>;

            match args.input_file {
                Some(in_file) => {
                    reader = Box::new(BufReader::new(File::open(&in_file).unwrap()))
                        as Box<dyn io::BufRead>;

                    if args.print {
                        writer = Box::new(BufWriter::new(io::stdout())) as Box<dyn Write>;
                    } else {
                        let out_file_name = match encode_setup(
                            args.mode,
                            in_file,
                            args.output_file,
                            args.overwrite,
                        ) {
                            Ok(name) => name,
                            Err(err) => {
                                eprintln!("Error: {:?}", err);
                                return;
                            }
                        };
                        let out_file = File::create(&out_file_name).unwrap();
                        writer = Box::new(BufWriter::new(out_file)) as Box<dyn Write>;
                    }
                }
                None => {
                    reader = Box::new(BufReader::new(io::stdin())) as Box<dyn io::BufRead>;

                    writer = if args.print {
                        Box::new(BufWriter::new(io::stdout())) as Box<dyn Write>
                    } else {
                        match args.output_file {
                            Some(name) => {
                                if let Err(e) = check_overwrite(&name, args.overwrite) {
                                    eprintln!("Error: {:?}", e);
                                    return;
                                }
                                let out_file = File::create(&name).unwrap();
                                Box::new(BufWriter::new(out_file)) as Box<dyn Write>
                            }
                            None => Box::new(BufWriter::new(io::stdout())) as Box<dyn Write>,
                        }
                    }
                }
            };

            let possible_error = if args.save_all {
                encode_jsonl_to_ben(reader, writer, BenVariant::Standard)
            } else {
                encode_jsonl_to_ben(reader, writer, BenVariant::MkvChain)
            };

            match possible_error {
                Ok(_) => {}
                Err(err) => {
                    eprintln!("Error: {:?}", err);
                }
            }
        }
        Mode::XEncode => {
            logln!("Running in xencode mode");

            let mut ben_and_xben = args.ben_and_xben;
            let mut jsonl_and_xben = args.ben_and_xben;

            let reader: Box<dyn io::BufRead>;
            let writer: Box<dyn Write>;

            match args.input_file {
                Some(in_file) => {
                    if in_file.ends_with(".ben") {
                        ben_and_xben = true;
                    } else if in_file.ends_with(".jsonl") {
                        jsonl_and_xben = true;
                    }

                    reader = Box::new(BufReader::new(File::open(&in_file).unwrap()))
                        as Box<dyn io::BufRead>;

                    writer = if args.print {
                        Box::new(BufWriter::new(io::stdout())) as Box<dyn Write>
                    } else {
                        let out_file_name = match encode_setup(
                            args.mode,
                            in_file,
                            args.output_file,
                            args.overwrite,
                        ) {
                            Ok(name) => name,
                            Err(err) => {
                                eprintln!("Error: {:?}", err);
                                return;
                            }
                        };
                        let out_file = File::create(&out_file_name).unwrap();
                        Box::new(BufWriter::new(out_file)) as Box<dyn Write>
                    };
                }
                None => {
                    reader = Box::new(BufReader::new(io::stdin())) as Box<dyn io::BufRead>;

                    writer = match args.output_file {
                        Some(name) => {
                            if let Err(e) = check_overwrite(&name, args.overwrite) {
                                eprintln!("Error: {:?}", e);
                                return;
                            }
                            let out_file = File::create(&name).unwrap();
                            Box::new(BufWriter::new(out_file)) as Box<dyn Write>
                        }
                        None => Box::new(BufWriter::new(io::stdout())) as Box<dyn Write>,
                    };
                }
            };

            if ben_and_xben {
                if let Err(err) =
                    encode_ben_to_xben(reader, writer, args.n_cpus, args.compression_level)
                {
                    eprintln!("Error: {:?}", err);
                }
            } else if jsonl_and_xben {
                let possible_error = if args.save_all {
                    encode_jsonl_to_xben(
                        reader,
                        writer,
                        BenVariant::Standard,
                        args.n_cpus,
                        args.compression_level,
                    )
                } else {
                    encode_jsonl_to_xben(
                        reader,
                        writer,
                        BenVariant::MkvChain,
                        args.n_cpus,
                        args.compression_level,
                    )
                };
                if let Err(e) = possible_error {
                    eprintln!("Error: {:?}", e);
                }
            } else {
                eprintln!("Error: Unsupported file type(s) for xencode mode");
            }
        }
        Mode::Decode => {
            logln!("Running in decode mode");

            let mut ben_and_xben = args.ben_and_xben;
            let mut jsonl_and_ben = args.jsonl_and_ben;

            let reader: Box<dyn io::BufRead>;
            let writer: Box<dyn Write>;

            match args.input_file {
                Some(file) => {
                    if file.ends_with(".ben") {
                        jsonl_and_ben = true;
                    } else if file.ends_with(".xben") {
                        ben_and_xben = true;
                    }

                    reader = Box::new(BufReader::new(File::open(&file).unwrap()))
                        as Box<dyn io::BufRead>;

                    writer = if args.print {
                        Box::new(BufWriter::new(io::stdout())) as Box<dyn Write>
                    } else {
                        let out_file_name =
                            match decode_setup(file, args.output_file, false, args.overwrite) {
                                Ok(name) => name,
                                Err(err) => {
                                    eprintln!("Error: {:?}", err);
                                    return;
                                }
                            };
                        let out_file = File::create(&out_file_name).unwrap();
                        Box::new(BufWriter::new(out_file)) as Box<dyn Write>
                    };
                }
                None => {
                    reader = Box::new(BufReader::new(io::stdin())) as Box<dyn io::BufRead>;

                    writer = if args.print {
                        Box::new(BufWriter::new(io::stdout())) as Box<dyn Write>
                    } else {
                        match args.output_file {
                            Some(out_name) => {
                                if let Err(e) = check_overwrite(&out_name, args.overwrite) {
                                    eprintln!("Error: {:?}", e);
                                    return;
                                }
                                let out_file = File::create(&out_name).unwrap();
                                Box::new(BufWriter::new(out_file)) as Box<dyn Write>
                            }
                            None => Box::new(BufWriter::new(io::stdout())) as Box<dyn Write>,
                        }
                    }
                }
            }

            if ben_and_xben {
                if let Err(err) = decode_xben_to_ben(reader, writer) {
                    eprintln!("Error: {:?}", err);
                }
            } else if jsonl_and_ben {
                if let Err(err) = decode_ben_to_jsonl(reader, writer) {
                    eprintln!("Error: {:?}", err);
                }
            } else {
                eprintln!("Error: Unsupported file type(s) for decode mode");
            }
        }
        Mode::XDecode => {
            logln!("Running in x-decode mode");

            let reader: Box<dyn io::BufRead>;
            let writer: Box<dyn Write>;

            match args.input_file {
                Some(file) => {
                    reader = Box::new(BufReader::new(File::open(&file).unwrap()))
                        as Box<dyn io::BufRead>;

                    writer = if args.print {
                        Box::new(BufWriter::new(io::stdout())) as Box<dyn Write>
                    } else {
                        let out_file_name =
                            match decode_setup(file, args.output_file, true, args.overwrite) {
                                Ok(name) => name,
                                Err(err) => {
                                    eprintln!("Error: {:?}", err);
                                    return;
                                }
                            };
                        let out_file = File::create(&out_file_name).unwrap();
                        Box::new(BufWriter::new(out_file)) as Box<dyn Write>
                    }
                }
                None => {
                    reader = Box::new(BufReader::new(io::stdin())) as Box<dyn io::BufRead>;

                    writer = if args.print {
                        Box::new(BufWriter::new(io::stdout())) as Box<dyn Write>
                    } else {
                        match args.output_file {
                            Some(out_name) => {
                                if let Err(e) = check_overwrite(&out_name, args.overwrite) {
                                    eprintln!("Error: {:?}", e);
                                    return;
                                }
                                let out_file = File::create(&out_name).unwrap();
                                Box::new(BufWriter::new(out_file)) as Box<dyn Write>
                            }
                            None => Box::new(BufWriter::new(io::stdout())) as Box<dyn Write>,
                        }
                    }
                }
            }

            if let Err(err) = decode_xben_to_jsonl(reader, writer) {
                eprintln!("Error: {:?}", err);
            }
        }
        Mode::Read => {
            logln!("Running in read mode");
            let file: File = File::open(
                &args
                    .input_file
                    .expect("Must provide input file for read mode."),
            )
            .unwrap();
            let reader: BufReader<File> = BufReader::new(file);

            if args.sample_number.is_none() {
                eprintln!("Error: Sample number is required in read mode");
                return;
            }

            let mut writer = if args.print {
                Box::new(BufWriter::new(io::stdout())) as Box<dyn Write>
            } else {
                match args.output_file {
                    Some(name) => {
                        let file: File = File::create(name).unwrap();
                        Box::new(BufWriter::new(file)) as Box<dyn Write>
                    }
                    None => Box::new(BufWriter::new(io::stdout())) as Box<dyn Write>,
                }
            };

            args.sample_number
                .map(|n| match extract_assignment_ben(reader, n) {
                    Ok(vec) => writer.write_all(format!("{:?}\n", vec).as_bytes()).unwrap(),
                    Err(e) => eprintln!("Error: {:?}", e),
                });
        }
        Mode::XzCompress => {
            logln!("Running in xz compress mode");

            let in_file_name = args
                .input_file
                .expect("Must provide input file for xz-compress mode.");
            let in_file = File::open(&in_file_name).unwrap();
            let reader = BufReader::new(in_file);

            let out_file_name = match args.output_file {
                Some(name) => name,
                None => in_file_name + ".xz",
            };

            if Path::new(&out_file_name).exists() {
                eprint!(
                    "File {:?} already exists, do you want to overwrite it? (y/[n]): ",
                    out_file_name
                );
                let mut user_input = String::new();
                std::io::stdin().read_line(&mut user_input).unwrap();
                if user_input.trim().to_lowercase() != "y" {
                    return;
                }
                eprintln!();
            }

            let out_file = File::create(out_file_name).unwrap();
            let writer = BufWriter::new(out_file);

            if let Err(err) = xz_compress(reader, writer, args.n_cpus, args.compression_level) {
                eprintln!("Error: {:?}", err);
            }
            logln!("Done!");
        }
        Mode::XzDecompress => {
            logln!("Running in xz decompress mode");

            let in_file_name = args
                .input_file
                .expect("Must provide input file for xz-decompress mode.");

            if !in_file_name.ends_with(".xz") {
                eprintln!("Error: Unsupported file type for xz decompress mode");
                return;
            }

            let output_file_name = match args.output_file {
                Some(name) => name,
                None => in_file_name[..in_file_name.len() - 3].to_string(),
            };

            if Path::new(&output_file_name).exists() {
                eprint!(
                    "File {:?} already exists, do you want to overwrite it? (y/[n]): ",
                    output_file_name
                );
                eprintln!();
                let mut user_input = String::new();
                std::io::stdin().read_line(&mut user_input).unwrap();
                if user_input.trim().to_lowercase() != "y" {
                    return;
                }
            }

            let in_file = File::open(&in_file_name).unwrap();
            let reader = BufReader::new(in_file);

            let out_file = File::create(output_file_name).unwrap();
            let writer = BufWriter::new(out_file);

            if let Err(err) = xz_decompress(reader, writer) {
                eprintln!("Error: {:?}", err);
            }
        }
    }
}