weakauras_codec_lib_serialize/
error.rs

1// Copyright 2025 Velithris
2// SPDX-License-Identifier: MIT
3
4use core::fmt;
5use std::error;
6
7use core::num::ParseFloatError;
8use weakauras_codec_lua_value::error::TryFromLuaValueError;
9
10/// Errors than can occur while deserializing.
11#[derive(Clone, Debug, PartialEq, Eq)]
12#[non_exhaustive]
13pub enum DeserializationError {
14    /// The input does not start with a valid prefix.
15    InvalidPrefix,
16    /// Invalid tag.
17    InvalidTag,
18    /// Invalid embedded tag.
19    InvalidEmbeddedTag,
20    /// Invalid string reference.
21    InvalidStringReference,
22    /// Invalid map reference.
23    InvalidMapReference,
24    /// Failed to parse a floating-point number.
25    InvalidFloatNumber,
26    /// According to the input, a map has a key that is either a null or a NaN.
27    /// That is not valid in Lua.
28    InvalidMapKeyType,
29    /// The input ended unexpectedly.
30    UnexpectedEof,
31    /// Exceeded recursion limit while deserializing nested data.
32    RecursionLimitExceeded,
33}
34
35impl From<ParseFloatError> for DeserializationError {
36    fn from(_value: ParseFloatError) -> Self {
37        Self::InvalidFloatNumber
38    }
39}
40
41impl From<TryFromLuaValueError> for DeserializationError {
42    fn from(_value: TryFromLuaValueError) -> Self {
43        Self::InvalidMapKeyType
44    }
45}
46
47impl fmt::Display for DeserializationError {
48    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
49        match self {
50            Self::InvalidPrefix => write!(f, "Invalid prefix"),
51            Self::InvalidTag => write!(f, "Invalid tag"),
52            Self::InvalidEmbeddedTag => write!(f, "Invalid embedded tag"),
53            Self::InvalidStringReference => write!(f, "Invalid string reference"),
54            Self::InvalidMapReference => write!(f, "Invalid map reference"),
55            Self::InvalidFloatNumber => write!(f, "Failed to parse a floating-point number"),
56            Self::InvalidMapKeyType => write!(f, "Invalid map key type"),
57            Self::UnexpectedEof => write!(f, "Unexpected EOF"),
58            Self::RecursionLimitExceeded => write!(f, "Recursion limit exceeded"),
59        }
60    }
61}
62
63impl error::Error for DeserializationError {}
64
65/// Errors than can occur while serializing.
66#[derive(Clone, Debug, PartialEq, Eq)]
67pub enum SerializationError {
68    /// More than `2^24` unique strings.
69    TooManyUniqueStrings,
70    /// A string is larger than `2^24` bytes.
71    StringIsTooLarge,
72    /// A map is larger than `2^24` key-value pairs.
73    MapIsTooLarge,
74    /// An array is larger than `2^24` elements.
75    ArrayIsTooLarge,
76    /// Exceeded recursion limit while serializing nested data.
77    RecursionLimitExceeded,
78}
79
80impl fmt::Display for SerializationError {
81    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
82        match self {
83            Self::TooManyUniqueStrings => write!(f, "Too many unique strings"),
84            Self::StringIsTooLarge => write!(f, "String is too large"),
85            Self::MapIsTooLarge => write!(f, "Map is too large"),
86            Self::ArrayIsTooLarge => write!(f, "Array is too large"),
87            Self::RecursionLimitExceeded => write!(f, "Recursion limit exceeded"),
88        }
89    }
90}
91
92impl error::Error for SerializationError {}