Skip to main content

nbt_rs/
error.rs

1//! Definitions of the error types.
2
3use thiserror::Error;
4
5use crate::types::NbtString;
6
7/// Errors that can occur while parsing NBT data.
8#[derive(Debug, Error, Clone, PartialEq, Eq, PartialOrd, Ord)]
9pub enum ParseError {
10    /// The encountered tag ID is not valid.
11    #[error("Invalid tag ID: {0}")]
12    InvalidTagId(u8),
13    /// The input ended before the parser finished.
14    #[error("Unexpected end of input")]
15    UnexpectedEndOfInput,
16    /// The string data is not valid UTF-8.
17    #[error("Invalid UTF-8 data")]
18    InvalidUtf8,
19    /// The encountered length of an `Array` or a `List` is negative.
20    #[error("Negative length encountered: {0}")]
21    NegativeLength(i32),
22    /// Extra bytes remaining after the parser finished.
23    #[error("Leftover data: {0} bytes")]
24    LeftoverData(usize),
25    /// A non-unique tag name was encountered.
26    #[error("Duplicate tag name")]
27    DuplicateTagName(NbtString),
28    /// The data is not a valid NBT file.
29    #[error("Not an NBT file")]
30    NotNBT,
31}
32
33/// Represents the errors caused by trying to create an invalid NBT tag.
34#[derive(Debug, Error, Clone, PartialEq, Eq, PartialOrd, Ord)]
35pub enum ValidationError {
36    /// Indicates that the `Array` exceeds the maximum allowed length.
37    #[error("The array is to long {0}/{max}", max = i32::MAX)]
38    ArrayTooLong(usize),
39    /// Indicates that the `String` exceeds the maximum allowed length.
40    #[error("The string is to long {0}/{max}", max = u16::MAX)]
41    StringTooLong(usize),
42}