use alloy_primitives::Bytes;
use revm::precompile::{PrecompileError, PrecompileOutput, PrecompileResult};
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub enum DataNetworkPrecompileError {
#[error("gas limit exceeded")]
OutOfGas,
#[error("{0}")]
Unauthorized(&'static str),
#[error("{0}")]
Revert(&'static str),
#[error("fatal precompile error: {0}")]
Fatal(String),
}
pub type Result<T> = std::result::Result<T, DataNetworkPrecompileError>;
impl From<DataNetworkPrecompileError> for PrecompileError {
fn from(value: DataNetworkPrecompileError) -> Self {
match value {
DataNetworkPrecompileError::OutOfGas => Self::OutOfGas,
DataNetworkPrecompileError::Fatal(message) => Self::Fatal(message),
error @ (DataNetworkPrecompileError::Unauthorized(_)
| DataNetworkPrecompileError::Revert(_)) => Self::Other(error.to_string().into()),
}
}
}
pub(crate) trait IntoPrecompileResult<T> {
fn into_precompile_result(self, encode_ok: impl FnOnce(T) -> Bytes) -> PrecompileResult;
}
impl<T> IntoPrecompileResult<T> for Result<T> {
fn into_precompile_result(self, encode_ok: impl FnOnce(T) -> Bytes) -> PrecompileResult {
match self {
Ok(value) => Ok(PrecompileOutput::new(0, encode_ok(value))),
Err(error) => Err(error.into()),
}
}
}