|
1 | 1 | use std::collections::HashMap;
|
2 |
| -use std::env; |
3 | 2 | use std::fs::File;
|
4 | 3 | use std::io::BufWriter;
|
5 |
| -use std::path::Path; |
| 4 | +use std::path::PathBuf; |
6 | 5 |
|
| 6 | +use anyhow::{Context, Result}; |
| 7 | +use clap::Parser; |
7 | 8 | use tidy::features::{Feature, collect_lang_features, collect_lib_features};
|
8 | 9 |
|
9 |
| -#[derive(serde::Serialize)] |
| 10 | +#[derive(Debug, Parser)] |
| 11 | +struct Cli { |
| 12 | + /// Path to `library/` directory. |
| 13 | + #[arg(long)] |
| 14 | + library_path: PathBuf, |
| 15 | + /// Path to `compiler/` directory. |
| 16 | + #[arg(long)] |
| 17 | + compiler_path: PathBuf, |
| 18 | + /// Path to `output/` directory. |
| 19 | + #[arg(long)] |
| 20 | + output_path: PathBuf, |
| 21 | +} |
| 22 | + |
| 23 | +#[derive(Debug, serde::Serialize)] |
10 | 24 | struct FeaturesStatus {
|
11 | 25 | lang_features_status: HashMap<String, Feature>,
|
12 | 26 | lib_features_status: HashMap<String, Feature>,
|
13 | 27 | }
|
14 | 28 |
|
15 |
| -fn main() { |
16 |
| - let library_path_str = env::args_os().nth(1).expect("library/ path required"); |
17 |
| - let compiler_path_str = env::args_os().nth(2).expect("compiler/ path required"); |
18 |
| - let output_path_str = env::args_os().nth(3).expect("output path required"); |
19 |
| - let library_path = Path::new(&library_path_str); |
20 |
| - let compiler_path = Path::new(&compiler_path_str); |
21 |
| - let output_path = Path::new(&output_path_str); |
22 |
| - let lang_features_status = collect_lang_features(compiler_path, &mut false); |
23 |
| - let lib_features_status = collect_lib_features(library_path) |
| 29 | +fn main() -> Result<()> { |
| 30 | + let Cli { compiler_path, library_path, output_path } = Cli::parse(); |
| 31 | + |
| 32 | + let lang_features_status = collect_lang_features(&compiler_path, &mut false); |
| 33 | + let lib_features_status = collect_lib_features(&library_path) |
24 | 34 | .into_iter()
|
25 | 35 | .filter(|&(ref name, _)| !lang_features_status.contains_key(name))
|
26 | 36 | .collect();
|
27 | 37 | let features_status = FeaturesStatus { lang_features_status, lib_features_status };
|
28 |
| - let writer = File::create(output_path).expect("output path should be a valid path"); |
29 |
| - let writer = BufWriter::new(writer); |
30 |
| - serde_json::to_writer_pretty(writer, &features_status).unwrap(); |
| 38 | + |
| 39 | + let output_dir = output_path.parent().with_context(|| { |
| 40 | + format!("failed to get parent dir of output path `{}`", output_path.display()) |
| 41 | + })?; |
| 42 | + std::fs::create_dir_all(output_dir).with_context(|| { |
| 43 | + format!("failed to create output directory at `{}`", output_dir.display()) |
| 44 | + })?; |
| 45 | + |
| 46 | + let output_file = File::create(&output_path).with_context(|| { |
| 47 | + format!("failed to create file at given output path `{}`", output_path.display()) |
| 48 | + })?; |
| 49 | + let writer = BufWriter::new(output_file); |
| 50 | + serde_json::to_writer_pretty(writer, &features_status) |
| 51 | + .context("failed to write json output")?; |
| 52 | + Ok(()) |
31 | 53 | }
|
0 commit comments