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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
use std::{error::Error as ErrorTrait, ffi::NulError, fmt::Display};
use crate::{
boxed::ZBox,
exception::PhpException,
flags::{ClassFlags, DataType, ZvalTypeFlags},
types::ZendObject,
};
pub type Result<T, E = Error> = std::result::Result<T, E>;
#[derive(Debug)]
#[non_exhaustive]
pub enum Error {
IncorrectArguments(usize, usize),
ZvalConversion(DataType),
UnknownDatatype(u32),
InvalidTypeToDatatype(ZvalTypeFlags),
InvalidScope,
InvalidPointer,
InvalidProperty,
InvalidCString,
Callable,
InvalidException(ClassFlags),
IntegerOverflow,
Exception(ZBox<ZendObject>),
}
impl Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Error::IncorrectArguments(n, expected) => write!(
f,
"Expected at least {} arguments, got {} arguments.",
expected, n
),
Error::ZvalConversion(ty) => write!(
f,
"Could not convert Zval from type {} into primitive type.",
ty
),
Error::UnknownDatatype(dt) => write!(f, "Unknown datatype {}.", dt),
Error::InvalidTypeToDatatype(dt) => {
write!(f, "Type flags did not contain a datatype: {:?}", dt)
}
Error::InvalidScope => write!(f, "Invalid scope."),
Error::InvalidPointer => write!(f, "Invalid pointer."),
Error::InvalidProperty => write!(f, "Property does not exist on object."),
Error::InvalidCString => write!(
f,
"String given contains NUL-bytes which cannot be present in a C string."
),
Error::Callable => write!(f, "Could not call given function."),
Error::InvalidException(flags) => {
write!(f, "Invalid exception type was thrown: {:?}", flags)
}
Error::IntegerOverflow => {
write!(f, "Converting integer arguments resulted in an overflow.")
}
Error::Exception(e) => write!(f, "Exception was thrown: {:?}", e),
}
}
}
impl ErrorTrait for Error {}
impl From<NulError> for Error {
fn from(_: NulError) -> Self {
Self::InvalidCString
}
}
impl From<Error> for PhpException {
fn from(err: Error) -> Self {
Self::default(err.to_string())
}
}