ussr_nbt/
lib.rs

1#![doc = include_str!("../README.md")]
2
3pub mod borrow;
4pub mod endian;
5pub mod mutf8;
6pub mod num;
7pub mod owned;
8mod swap_endian;
9
10use std::io;
11
12use thiserror::Error;
13
14pub const TAG_END: u8 = 0;
15pub const TAG_BYTE: u8 = 1;
16pub const TAG_SHORT: u8 = 2;
17pub const TAG_INT: u8 = 3;
18pub const TAG_LONG: u8 = 4;
19pub const TAG_FLOAT: u8 = 5;
20pub const TAG_DOUBLE: u8 = 6;
21pub const TAG_BYTE_ARRAY: u8 = 7;
22pub const TAG_STRING: u8 = 8;
23pub const TAG_LIST: u8 = 9;
24pub const TAG_COMPOUND: u8 = 10;
25pub const TAG_INT_ARRAY: u8 = 11;
26pub const TAG_LONG_ARRAY: u8 = 12;
27
28/// Errors that can occur while reading NBT data.
29#[non_exhaustive]
30#[derive(Debug, Error)]
31pub enum NbtReadError {
32    #[error(transparent)]
33    Io(#[from] io::Error),
34
35    #[error("Root tag not a compound: {0}")]
36    InvalidRootTag(u8),
37
38    #[error("Invalid tag: {0}")]
39    InvalidTag(u8),
40
41    #[error("Depth limit exceeded")]
42    DepthLimitExceeded,
43}
44
45/// Options for reading NBT data.
46//? Automatically handle endianess?
47//? Check for duplicate keys?
48pub struct ReadOpts {
49    /// The maximum depth to read.
50    /// Defaults to `128`.
51    pub depth_limit: u16,
52
53    /// Whether to read the root compound name or not.
54    /// Defaults to `true`.
55    pub name: bool,
56}
57
58/// Options for writing NBT data.
59pub struct WriteOpts {
60    /// Whether to write the root compound name or not.
61    /// Defaults to `true`.
62    pub name: bool,
63}
64
65impl Default for ReadOpts {
66    #[inline]
67    fn default() -> Self {
68        Self {
69            depth_limit: 128,
70            name: true,
71        }
72    }
73}
74
75impl ReadOpts {
76    #[inline]
77    pub fn new() -> Self {
78        Self::default()
79    }
80
81    #[inline]
82    pub fn with_depth_limit(mut self, depth_limit: u16) -> Self {
83        self.depth_limit = depth_limit;
84        self
85    }
86
87    #[inline]
88    pub fn with_name(mut self, name: bool) -> Self {
89        self.name = name;
90        self
91    }
92}
93
94impl Default for WriteOpts {
95    #[inline]
96    fn default() -> Self {
97        Self { name: true }
98    }
99}
100
101impl WriteOpts {
102    #[inline]
103    pub fn new() -> Self {
104        Self::default()
105    }
106
107    #[inline]
108    pub fn with_name(mut self, name: bool) -> Self {
109        self.name = name;
110        self
111    }
112}