mirror of
https://github.com/neocturne/MinedMap.git
synced 2025-07-02 05:49:06 +02:00
Refactor logic from new dump tools into library crate
This commit is contained in:
parent
5a364e2434
commit
5e96be3fda
8 changed files with 196 additions and 147 deletions
28
src/io/data.rs
Normal file
28
src/io/data.rs
Normal file
|
@ -0,0 +1,28 @@
|
|||
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")?;
|
||||
|
||||
fastnbt::from_bytes(&buf[..]).context("Failed to decode NBT data")
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue