|
| 1 | +//! A command-line utility to extract version information from IPUMS data files. |
| 2 | +//! |
| 3 | +//! This tool reads version metadata from both Parquet and fixed-width IPUMS data files |
| 4 | +//! and outputs it in either JSON or human-readable text format. |
| 5 | +//! |
| 6 | +//! # Usage |
| 7 | +//! |
| 8 | +//! ```bash |
| 9 | +//! # For parquet data (directory containing .parquet files) |
| 10 | +//! dataversion /pkg/ipums/usa/output_data/current/parquet/us2015b |
| 11 | +//! |
| 12 | +//! # For fixed-width data (.dat.gz file) |
| 13 | +//! dataversion /pkg/ipums/usa/output_data/current/us2015b_usa.dat.gz |
| 14 | +//! |
| 15 | +//! # Output as JSON (default is text) |
| 16 | +//! dataversion --format json /path/to/data |
| 17 | +//! ``` |
| 18 | +
|
| 19 | +use cimdea::data_version::{extract_version, DataVersion}; |
| 20 | +use clap::{Parser, ValueEnum}; |
| 21 | +use std::process; |
| 22 | + |
| 23 | +#[derive(Parser, Debug)] |
| 24 | +#[command( |
| 25 | + name = "dataversion", |
| 26 | + version, |
| 27 | + about = "Extract version information from IPUMS data files", |
| 28 | + long_about = "Extract version information from IPUMS data files.\n\n\ |
| 29 | + Supports both Parquet and fixed-width (.dat.gz) formats.\n\ |
| 30 | + Version information includes release numbers, commit hashes,\n\ |
| 31 | + branch names, and other build metadata." |
| 32 | +)] |
| 33 | +struct Args { |
| 34 | + /// Path to the data file or directory. |
| 35 | + /// |
| 36 | + /// For Parquet: path to a directory containing .parquet files |
| 37 | + /// (e.g., /pkg/ipums/usa/output_data/current/parquet/us2015b) |
| 38 | + /// |
| 39 | + /// For fixed-width: path to a .dat.gz file |
| 40 | + /// (e.g., /pkg/ipums/usa/output_data/current/us2015b_usa.dat.gz) |
| 41 | + #[arg(value_name = "PATH")] |
| 42 | + path: String, |
| 43 | + |
| 44 | + /// Output format |
| 45 | + #[arg(short, long, value_enum, default_value = "text")] |
| 46 | + format: OutputFormat, |
| 47 | +} |
| 48 | + |
| 49 | +#[derive(Debug, Clone, Copy, ValueEnum)] |
| 50 | +enum OutputFormat { |
| 51 | + /// Human-readable text output |
| 52 | + Text, |
| 53 | + /// Machine-readable JSON output |
| 54 | + Json, |
| 55 | +} |
| 56 | + |
| 57 | +fn main() { |
| 58 | + let args = Args::parse(); |
| 59 | + |
| 60 | + match extract_version(&args.path) { |
| 61 | + Ok(version) => { |
| 62 | + output_version(&version, args.format); |
| 63 | + } |
| 64 | + Err(e) => { |
| 65 | + eprintln!("Error: {}", e); |
| 66 | + process::exit(1); |
| 67 | + } |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +fn output_version(version: &DataVersion, format: OutputFormat) { |
| 72 | + match format { |
| 73 | + OutputFormat::Text => { |
| 74 | + println!("{}", version.to_text()); |
| 75 | + } |
| 76 | + OutputFormat::Json => match version.to_json() { |
| 77 | + Ok(json) => println!("{}", json), |
| 78 | + Err(e) => { |
| 79 | + eprintln!("Error serializing to JSON: {}", e); |
| 80 | + process::exit(1); |
| 81 | + } |
| 82 | + }, |
| 83 | + } |
| 84 | +} |
0 commit comments