hkxc/args/
verify.rs

1use crate::args::progress_handler::CliProgressHandler;
2use serde_hkx_features::{
3    error::Result,
4    verify::{verify_dir, verify_file},
5};
6use std::path::{Path, PathBuf};
7
8/// ANSI color representation command examples.
9pub const EXAMPLES: &str = color_print::cstr!(
10    r#"In case of error, dir and file behave differently
11    - dir: All error paths are displayed
12    - file: Error path + diff displayed
13
14<blue><bold><underline>Examples</underline></bold></blue>
15- <blue!>Test the reproducibility of hkx</blue!>
16  <cyan!>hkxc verify</cyan!> ./defaultmale.hkx
17
18- <blue!>Test the reproducibility of hkx files</blue!>
19  <cyan!>./hkxc verify</cyan!> ./input --log-level info --log-file "./verify_inputs.log"
20"#
21);
22
23#[derive(Debug, clap::Args)]
24#[clap(arg_required_else_help = true, after_long_help = EXAMPLES)]
25pub(crate) struct Args {
26    /// hkx file path or dir for which you want to verify reproducibility. It is recommended to use log in INFO mode.
27    pub path: PathBuf,
28    /// Option to make the diff display colorful in case of error when file is specified.
29    #[clap(long)]
30    pub color: bool,
31}
32
33pub fn verify<P>(path: P, color: bool) -> Result<()>
34where
35    P: AsRef<Path>,
36{
37    let path = path.as_ref();
38
39    if path.is_file() {
40        println!("Verifying...");
41        verify_file(path, color).map(|_| {
42            color_print::cprintln!(
43                "<green>Complete hkx reproduction: {}</green>",
44                path.display()
45            );
46        })
47    } else {
48        verify_dir(path, CliProgressHandler::new())
49    }
50}
51
52// NOTE: We don't try it except local PC because MIRI makes an error. Therefore, leave it commented out.
53#[cfg(test)]
54mod tests {
55    use super::*;
56
57    #[ignore = "need templates"]
58    #[test]
59    fn test_verify_dir() {
60        let input = "../../tests/input";
61        verify(input, false).unwrap_or_else(|err| panic!("{err}"));
62    }
63}