From 8a006fd36162d8b4492a0aa4129fd78a8c7030a2 Mon Sep 17 00:00:00 2001 From: Mike Belopuhov Date: Sat, 20 Mar 2021 00:02:12 +0100 Subject: [PATCH] Replace displaydoc with a handrolled Display impl --- Cargo.toml | 3 +-- src/error.rs | 70 ++++++++++++++++++++++++++++++++++++++-------------- 2 files changed, 53 insertions(+), 20 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 04fd4ed..d1b8ddc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/src/error.rs b/src/error.rs index a25f371..84ffd5e 100644 --- a/src/error.rs +++ b/src/error.rs @@ -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 = ::core::result::Result; /// 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), - /// 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, @@ -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), } @@ -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(msg: T) -> Self