Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ documentation = "https://docs.rs/serde_php"

[dependencies]
serde = { version = "1" }
smallvec = "0.6.10"
displaydoc = "0.1.4"
smallvec = "1.6"

[dev-dependencies]
serde_bytes = "0.11.2"
Expand Down
70 changes: 52 additions & 18 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,48 +1,46 @@
//! Top-level error type for PHP serialization/deserialization.

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

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

/// PHP serialization/deserialization error.
#[derive(Debug, Display)]
#[derive(Debug)]
pub enum Error {
/// Error writing serializated value: {0}
/// Error writing serialized value.
WriteSerialized(io::Error),
/// Error reading serializing value: {0}
/// Error reading serializing value.
ReadSerialized(io::Error),
/// Unexpected end of file while reading,
/// Unexpected end of file while reading.
UnexpectedEof,
/// Expected `{expected}` but got `{actual}` instead.
/// Unexpected input.
Unexpected {
/// Byte expected.
expected: char,
/// Actual byte found.
actual: char,
},
/// Expected a digit, but got `{actual}` instead.
/// Expected a digit, but got non-digit value instead.
ExpectedDigit {
/// Non-digit found.
actual: char,
},
/// Deserialized bytestring is not valid UTF: {0}
/// Deserialized bytestring is not valid UTF.
Utf8Error(std::str::Utf8Error),
/// Could not convert into char from decimal value: {0}
/// Could not convert into char from decimal value.
CharConversionFailed(std::char::CharTryFromError),
/// Not a valid number or incorrect number type: {0}
/// Not a valid number or incorrect number type.
NotAValidNumber(Box<dyn std::error::Error + Send + Sync>),
/// Not a valid value for boolean: {0}
/// Not a valid value for boolean.
InvalidBooleanValue(char),
/// Unsupported array key type (must be all strings or all numeric): {0}
/// Unsupported array key type: must be all strings or all numeric.
UnsupportedArrayKeyType(char),
/// Invalid type indicator on value: {0}
/// Invalid type indicator on value.
InvalidTypeIndicator(char),
/// Feature not implemented by `serde_php`: {0}
/// Feature not implemented by `serde_php`.
MissingFeature(&'static str),
/// Array-index mismatch (must be in-order and numeric), expected {expected}
/// but got {actual}
/// Array-index mismatch: must be in-order and numeric.
IndexMismatch {
/// Expected index.
expected: usize,
Expand All @@ -55,9 +53,9 @@ pub enum Error {
/// 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}
/// PHP Deserialization failed.
SerializationFailed(String),
/// PHP Serialization failed: {0}
/// PHP Serialization failed.
DeserializationFailed(String),
}

Expand All @@ -76,6 +74,42 @@ impl std::error::Error for Error {
}
}

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
#[allow(clippy::enum_glob_use)]
use Error::*;

match self {
WriteSerialized(err) => write!(f, "Error writing serialized value: {}", err),
ReadSerialized(err) => write!(f, "Error reading serializing value: {}", err),
UnexpectedEof => write!(f, "Unexpected end of file while reading"),
Unexpected { expected, actual } => {
write!(f, "Expected `{}` but got `{}` instead", expected, actual)
}
ExpectedDigit { actual } => write!(f, "Expected a digit, but got `{}` instead", actual),
Utf8Error(err) => write!(f, "Deserialized bytestring is not valid UTF: {}", err),
CharConversionFailed(err) => {
write!(f, "Could not convert into char from decimal value: {}", err)
}
NotAValidNumber(err) => {
write!(f, "Not a valid number or incorrect number type: {}", err)
}
InvalidBooleanValue(ch) => write!(f, "Not a valid value for boolean: {}", ch),
UnsupportedArrayKeyType(ch) => write!(f, "Unsupported array key type: {}", ch),
InvalidTypeIndicator(ch) => write!(f, "Invalid type indicator on value: {}", ch),
MissingFeature(feat) => write!(f, "Feature not implemented by `serde_php`: {}", feat),
IndexMismatch { expected, actual } => write!(
f,
"Array-index mismatch, expected {} but got {}",
expected, actual
),
LengthRequired => write!(f, "Attempted to serialize sequence of unknown length"),
SerializationFailed(err) => write!(f, "PHP Deserialization failed: {}", err),
DeserializationFailed(err) => write!(f, "PHP Serialization failed: {}", err),
}
}
}

impl serde::ser::Error for Error {
#[inline]
fn custom<T>(msg: T) -> Self
Expand Down