1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
//! Top-level error type for PHP serialization/deserialization.

use displaydoc::Display;
use std::{fmt, io};
use thiserror::Error;

/// Result type for PHP serialization/deserialization.
pub type Result<T> = ::core::result::Result<T, Error>;

/// PHP serialization/deserialization error.
#[derive(Error, Debug, Display)]
pub enum Error {
    /// Error writing serializated value: {0}
    WriteSerialized(#[source] io::Error),
    /// Error reading serializing value: {0}
    ReadSerialized(#[source] io::Error),
    /// Unexpected end of file while reading,
    UnexpectedEof,
    /// Expected `{expected}` but got `{actual}` instead.
    Unexpected {
        /// Byte expected.
        expected: char,
        /// Actual byte found.
        actual: char,
    },
    /// Expected a digit, but got `{actual}` instead.
    ExpectedDigit {
        /// Non-digit found.
        actual: char,
    },
    /// Deserialized bytestring is not valid UTF: {0}
    Utf8Error(#[source] std::str::Utf8Error),
    /// Could not convert into char from decimal value: {0}
    CharConversionFailed(#[source] std::char::CharTryFromError),
    /// Not a valid number or incorrect number type: {0}
    NotAValidNumber(#[source] Box<dyn std::error::Error>),
    /// Not a valid value for boolean: {0}
    InvalidBooleanValue(char),
    /// Unsupported array key type (must be all strings or all numeric): {0}
    UnsupportedArrayKeyType(char),
    /// Invalid type indicator on value: {0}
    InvalidTypeIndicator(char),
    /// Feature not implemented by `serde_php`: {0}
    MissingFeature(&'static str),
    /// Array-index mismatch (must be in-order and numeric), expected {expected}
    /// but got {actual}
    IndexMismatch {
        /// Expected index.
        expected: usize,
        /// Actual index found.
        actual: usize,
    },
    /// Attempted to serialize sequence of unknown length.
    ///
    /// PHP requires all collections to be length prefixed. Serializing
    /// sequences of unknown length requires writing these to a memory buffer
    /// with potentially unbounded space requirements and is thus disabled.
    LengthRequired,
    /// PHP Deserialization failed: {0}
    SerializationFailed(String),
    /// PHP Serialization failed: {0}
    DeserializationFailed(String),
}

impl serde::ser::Error for Error {
    #[inline]
    fn custom<T>(msg: T) -> Self
    where
        T: fmt::Display,
    {
        Error::SerializationFailed(msg.to_string())
    }
}

impl serde::de::Error for Error {
    #[inline]
    fn custom<T>(msg: T) -> Self
    where
        T: fmt::Display,
    {
        Error::DeserializationFailed(msg.to_string())
    }
}