-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace unit errors with error enum (#29)
* WIP: Replacing unit type errors with actual error * Tidied up errors
- Loading branch information
1 parent
439f2ae
commit f11451f
Showing
6 changed files
with
128 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
use std::{error::Error as ErrorTrait, fmt::Display}; | ||
|
||
use crate::php::enums::DataType; | ||
|
||
/// The main result type which is passed by the library. | ||
pub type Result<T> = std::result::Result<T, Error>; | ||
|
||
/// The main error type which is passed by the library inside the custom | ||
/// [`Result`] type. | ||
#[derive(Debug, Clone)] | ||
#[non_exhaustive] | ||
pub enum Error { | ||
/// An incorrect number of arguments was given to a PHP function. | ||
/// | ||
/// The type carries two integers - the first representing the minimum | ||
/// number of arguments expected, and the second representing the number of | ||
/// arguments that were received. | ||
IncorrectArguments(u32, u32), | ||
|
||
/// There was an error converting a Zval into a primitive type. | ||
/// | ||
/// The type carries the data type of the Zval. | ||
ZvalConversion(DataType), | ||
|
||
/// The type of the Zval is unknown. | ||
/// | ||
/// The type carries the integer representation of the type of Zval. | ||
UnknownDatatype(u8), | ||
} | ||
|
||
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), | ||
} | ||
} | ||
} | ||
|
||
impl ErrorTrait for Error {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
#[macro_use] | ||
pub mod macros; | ||
pub mod bindings; | ||
pub mod errors; | ||
pub mod functions; | ||
pub mod php; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters