serde_hkx_features/serde/
de.rs1#[cfg(feature = "extra_fmt")]
3use crate::error::UnsupportedExtensionPathSnafu;
4use crate::{
5 convert::OutFormat,
6 error::{DeSnafu, Error, FailedReadFileSnafu, Result},
7};
8use serde_hkx::{from_bytes, from_str};
9use snafu::ResultExt as _;
10use std::{io::Read, path::Path};
11
12pub fn deserialize<'a, I, T>(bytes: &'a Vec<u8>, string: &'a mut String, input: I) -> Result<T>
22where
23 I: AsRef<Path>,
24 T: havok_serde::Deserialize<'a>,
25{
26 let input = input.as_ref();
27 let input_ext = input.extension();
28
29 let classes = if let Some(input_ext) = input_ext {
31 let fmt =
32 OutFormat::from_extension(input_ext).map_err(|_| Error::UnsupportedExtensionPath {
33 path: input.to_path_buf(),
34 })?;
35
36 match fmt {
37 OutFormat::Amd64 | OutFormat::Win32 => from_bytes(bytes).context(DeSnafu {
38 input: input.to_path_buf(),
39 })?,
40
41 OutFormat::Xml => {
42 let mut decoder = encoding_rs_io::DecodeReaderBytes::new(bytes.as_slice());
43 decoder
44 .read_to_string(&mut *string)
45 .context(FailedReadFileSnafu {
46 path: input.to_path_buf(),
47 })?;
48 from_str(&*string).context(DeSnafu {
49 input: input.to_path_buf(),
50 })?
51 }
52 #[cfg(feature = "extra_fmt")]
53 _ => {
54 return UnsupportedExtensionPathSnafu {
55 path: input.to_path_buf(),
56 }
57 .fail();
58 }
59 }
60 } else {
61 return Err(Error::MissingExtension {
62 path: input.to_path_buf(),
63 });
64 };
65
66 Ok(classes)
67}