2023-01-25 21:41:08 +01:00
|
|
|
use std::{fs::File, io::prelude::*, path::Path};
|
|
|
|
|
|
|
|
use anyhow::{Context, Result};
|
|
|
|
use flate2::read::GzDecoder;
|
|
|
|
use serde::de::DeserializeOwned;
|
|
|
|
|
|
|
|
pub fn from_reader<R, T>(reader: R) -> Result<T>
|
|
|
|
where
|
|
|
|
R: Read,
|
|
|
|
T: DeserializeOwned,
|
|
|
|
{
|
|
|
|
let mut decoder = GzDecoder::new(reader);
|
|
|
|
let mut buf = vec![];
|
|
|
|
decoder
|
|
|
|
.read_to_end(&mut buf)
|
|
|
|
.context("Failed to read file")?;
|
|
|
|
|
2023-01-28 00:58:47 +01:00
|
|
|
fastnbt::from_bytes(&buf).context("Failed to decode NBT data")
|
2023-01-25 21:41:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn from_file<P, T>(path: P) -> Result<T>
|
|
|
|
where
|
|
|
|
P: AsRef<Path>,
|
|
|
|
T: DeserializeOwned,
|
|
|
|
{
|
|
|
|
let file = File::open(path).context("Failed to open file")?;
|
|
|
|
from_reader(file)
|
|
|
|
}
|