serde_hkx_features/
tree.rs

1//! Show dependency tree from havok behavior state machine (hkx/xml file)
2use crate::{ClassMap, error::Result, fs::ReadExt};
3use serde_hkx::tree::HavokTree as _;
4use std::path::Path;
5use tokio::fs;
6
7/// Output reference tree to stdout/file.
8/// - `output`: If not provided, then stdout.
9///
10/// # Errors
11/// If the extension is not `hkx` or `xml`.
12pub async fn write_tree<I, O>(input: I, output: Option<O>) -> Result<()>
13where
14    I: AsRef<Path>,
15    O: AsRef<Path>,
16{
17    let tree = generate(input).await?; // NOTE: With newline
18    match output.as_ref() {
19        Some(output) => fs::write(output, &tree).await?,
20        None => print!("{tree}"),
21    };
22    Ok(())
23}
24
25/// Generate reference tree.
26///
27/// # Errors
28/// If the unknown extension. (Not `hkx`, `xml`...).
29pub async fn generate<P>(input: P) -> Result<String>
30where
31    P: AsRef<Path>,
32{
33    let bytes = input.read_bytes().await?;
34    let mut string = String::new();
35    let mut class_map: ClassMap = {
36        #[cfg(not(feature = "extra_fmt"))]
37        {
38            crate::serde::de::deserialize(&bytes, &mut string, input)?
39        }
40        #[cfg(feature = "extra_fmt")]
41        {
42            crate::serde_extra::de::deserialize(&bytes, &mut string, input)?
43        }
44    };
45
46    Ok(class_map.tree_for_bytes())
47}