bookshelf/
lib.rs

1//! A library for reading and writing various minecraft files from
2//! many different editions and versions of the game.
3#![cfg_attr(docsrs, feature(doc_cfg))]
4
5use std::fs::File;
6use std::io::Result;
7
8/// Module to aid in reading and writing binary
9pub(crate) mod bin;
10/// Module for serializing and deserializing NBT data
11#[cfg(feature = "nbt")]
12#[cfg_attr(docsrs, doc(cfg(feature = "nbt")))]
13pub mod nbt;
14
15/// Module for writing and reading `Minecraft: Java Edition` files
16#[cfg(feature = "je")]
17#[cfg_attr(docsrs, doc(cfg(feature = "je")))]
18pub mod je;
19
20/// Module for writing and reading `Minecraft: Bedrock Edition` files
21#[cfg(feature = "be")]
22#[cfg_attr(docsrs, doc(cfg(feature = "be")))]
23pub mod be;
24
25/// Trait for any struct that can converted to and from a binary stream.
26pub trait BinaryFile {
27    /// Reads a binary stream and returns an instance of `Self`
28    fn from_reader<R: std::io::Read>(reader: R) -> Result<Self> where Self: Sized;
29    /// Writes the strut as a binary stream
30    fn to_writer<W: std::io::Write>(&self, writer: W) -> Result<()>;
31}
32
33/// Trait for structs that interact with random access files
34/// such as region files.
35pub trait RandomAccess {
36    fn new(file: File) -> Result<Self> where Self: Sized;
37}
38
39/// Trait that allows a struct to be used as an endianness when working with binary
40pub trait Endianness {
41    /// Wether or not this endianness is the same as the target machine
42    const IS_NATIVE: bool;
43}
44
45/// Represents little endianness
46pub struct LE {}
47
48impl Endianness for LE {
49    const IS_NATIVE: bool = cfg!(target_endian = "little");
50}
51
52/// Represents big endianness
53pub struct BE {}
54
55impl Endianness for BE {
56    const IS_NATIVE: bool = cfg!(target_endian = "big");
57}