weakauras_codec_ace_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 `^1`.
15    InvalidPrefix,
16    /// Invalid identifier. Valid values are `^^`, `^Z`, `^B`, `^b`, `^S`, `^N`, `^F`, `^f`, `^T`, `^t`.
17    InvalidIdentifier,
18    /// Invalid escape character. Valid ranges are `0x40..=0x5D`, `0x5E..=0x60` and `0x7A..=0x7D`.
19    InvalidEscapeCharacter,
20    /// Failed to parse a floating-point number.
21    InvalidFloatNumber,
22    /// A floating-point number stored as a mantissa-exponent pair is missing its exponent.
23    MissingExponent,
24    /// According to the input, a map has a key that is either a null or a NaN.
25    /// That is not valid in Lua.
26    InvalidMapKeyType,
27    /// A map has a key without a corresponding value.
28    MapMissingValue,
29    /// The input ended before an identifier marking the end of a map.
30    UnclosedMap,
31    /// The input ended unexpectedly.
32    UnexpectedEof,
33    /// Exceeded recursion limit while deserializing nested data.
34    RecursionLimitExceeded,
35}
36
37impl From<ParseFloatError> for DeserializationError {
38    fn from(_value: ParseFloatError) -> Self {
39        Self::InvalidFloatNumber
40    }
41}
42
43impl From<TryFromLuaValueError> for DeserializationError {
44    fn from(_value: TryFromLuaValueError) -> Self {
45        Self::InvalidMapKeyType
46    }
47}
48
49impl fmt::Display for DeserializationError {
50    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
51        match self {
52            Self::InvalidPrefix => write!(f, "Invalid prefix"),
53            Self::InvalidIdentifier => write!(f, "Invalid identifier"),
54            Self::InvalidEscapeCharacter => write!(f, "Invalid escape character"),
55            Self::InvalidFloatNumber => write!(f, "Failed to parse a floating-point number"),
56            Self::MissingExponent => write!(f, "A floating-point number is missing an exponent"),
57            Self::InvalidMapKeyType => write!(f, "Invalid map key type"),
58            Self::MapMissingValue => write!(f, "Map has a key without a corresponding value"),
59            Self::UnclosedMap => write!(
60                f,
61                "Input ended before an identifier marking the end of a map"
62            ),
63            Self::UnexpectedEof => write!(f, "Unexpected EOF"),
64            Self::RecursionLimitExceeded => write!(f, "Recursion limit exceeded"),
65        }
66    }
67}
68
69impl error::Error for DeserializationError {}
70
71/// Errors than can occur while serializing.
72#[derive(Clone, Debug, PartialEq, Eq)]
73pub enum SerializationError {
74    /// AceSerialize does not support serializing NaNs.
75    NanEncountered,
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::NanEncountered => write!(f, "Encountered a NaN"),
84            Self::RecursionLimitExceeded => write!(f, "Recursion limit exceeded"),
85        }
86    }
87}
88
89impl error::Error for SerializationError {}