| 1 |
// SPDX-FileCopyrightText: Peter Pentchev <[email protected]> |
| 2 |
// SPDX-License-Identifier: BSD-2-Clause |
| 3 |
#![allow(clippy::result_large_err)] |
| 4 |
|
| 5 |
use std::io; |
| 6 |
|
| 7 |
use anyhow::{Context, Result}; |
| 8 |
use clap::Parser; |
| 9 |
use clap_derive::Parser; |
| 10 |
use tracing::Level; |
| 11 |
|
| 12 |
mod defs; |
| 13 |
mod output; |
| 14 |
mod parse; |
| 15 |
|
| 16 |
use defs::Config; |
| 17 |
use tracing::debug; |
| 18 |
|
| 19 |
#[derive(Debug, Parser)] |
| 20 |
#[clap(author, version, about)] |
| 21 |
struct Cli { |
| 22 |
/// The name of the JSON test data file. |
| 23 |
#[clap(short = 'f', long)] |
| 24 |
data_file: String, |
| 25 |
|
| 26 |
/// The name of the file to generate. |
| 27 |
#[clap(short, long)] |
| 28 |
output: String, |
| 29 |
|
| 30 |
/// Verbose operation; display diagnostic output. |
| 31 |
#[clap(short, long)] |
| 32 |
verbose: bool, |
| 33 |
|
| 34 |
/// The path to the template to use. |
| 35 |
template_path: String, |
| 36 |
} |
| 37 |
|
| 38 |
fn parse_args() -> Result<Config> { |
| 39 |
let args = Cli::try_parse().context("Could not parse the command-line arguments")?; |
| 40 |
Ok(Config { |
| 41 |
data_path: args.data_file, |
| 42 |
output_path: args.output, |
| 43 |
template_path: args.template_path, |
| 44 |
verbose: args.verbose, |
| 45 |
}) |
| 46 |
} |
| 47 |
|
| 48 |
/// Initialize the logging subsystem provided by the `tracing` library. |
| 49 |
fn setup_tracing(verbose: bool) -> Result<()> { |
| 50 |
let sub = tracing_subscriber::fmt() |
| 51 |
.without_time() |
| 52 |
.with_max_level(if verbose { Level::TRACE } else { Level::INFO }) |
| 53 |
.with_writer(io::stderr) |
| 54 |
.finish(); |
| 55 |
#[allow(clippy::absolute_paths)] |
| 56 |
tracing::subscriber::set_global_default(sub).context("Could not initialize the tracing logger") |
| 57 |
} |
| 58 |
|
| 59 |
fn main() -> Result<()> { |
| 60 |
let cfg = parse_args()?; |
| 61 |
setup_tracing(cfg.verbose)?; |
| 62 |
let template = parse::parse_template(&cfg).context("Could not parse the template file")?; |
| 63 |
let test_data = parse::parse_test_data(&cfg).context("Could not parse the test data")?; |
| 64 |
output::write_output_file(&cfg, &template, &test_data) |
| 65 |
.context("Could not generate the output file")?; |
| 66 |
debug!("Everything seems fine!"); |
| 67 |
Ok(()) |
| 68 |
} |