serde_hkx_features/verify/
mod.rs

1//! Verification of hkx -> Rust data type -> hkx conversion to original hkx
2
3mod dir;
4mod file;
5
6pub use self::dir::verify_dir;
7pub use self::file::verify_file;
8use crate::progress::ProgressHandler;
9use crate::{
10    ClassMap,
11    error::{DeSnafu, FailedReadFileSnafu, Result, SerSnafu},
12};
13use serde_hkx::bytes::serde::hkx_header::HkxHeader;
14use snafu::ResultExt as _;
15use std::path::Path;
16
17/// Checks reproduction for file/dir hkx.
18///
19/// # Errors
20/// If an error occurs, return a diff showing the location of each error.(If `input` is one file path)
21pub fn verify<I, P>(input: I, color: bool, progress: P) -> Result<()>
22where
23    I: AsRef<Path>,
24    P: ProgressHandler + Send + Sync,
25{
26    let path = input.as_ref();
27
28    if path.is_file() {
29        verify_file(path, color)
30    } else {
31        verify_dir(path, progress)
32    }
33}
34
35pub(super) fn verify_inner<I>(input: I) -> Result<(Vec<u8>, Vec<u8>)>
36where
37    I: AsRef<Path>,
38{
39    let input = input.as_ref();
40
41    let expected_bytes = std::fs::read(input).with_context(|_| FailedReadFileSnafu {
42        path: input.to_path_buf(),
43    })?;
44
45    let actual_bytes = {
46        let classes: ClassMap =
47            serde_hkx::from_bytes(&expected_bytes).with_context(|_| DeSnafu { input })?;
48        let header = HkxHeader::from_bytes(&expected_bytes).with_context(|_| DeSnafu { input })?;
49
50        serde_hkx::to_bytes(&classes, &header).with_context(|_| SerSnafu { input })?
51    };
52
53    Ok((expected_bytes, actual_bytes))
54}