#![deny(unused_crate_dependencies)]
#![deny(missing_docs)]
use fuel_core_types::services::executor::Error as ExecutorError;
use std::io::ErrorKind;
pub use fuel_vm_private::{
fuel_storage::*,
storage::InterpreterStorage,
};
pub mod iter;
pub mod tables;
#[cfg(feature = "test-helpers")]
pub mod test_helpers;
pub mod transactional;
pub use fuel_vm_private::storage::{
ContractsAssetKey,
ContractsStateKey,
};
pub type Result<T> = core::result::Result<T, Error>;
#[derive(thiserror::Error, Debug)]
#[non_exhaustive]
pub enum Error {
#[error("error performing serialization or deserialization")]
Codec,
#[error("error occurred in the underlying datastore `{0}`")]
DatabaseError(Box<dyn std::error::Error + Send + Sync>),
#[error("resource of type `{0}` was not found at the: {1}")]
NotFound(&'static str, &'static str),
#[error(transparent)]
Other(#[from] anyhow::Error),
}
impl From<Error> for std::io::Error {
fn from(e: Error) -> Self {
std::io::Error::new(ErrorKind::Other, e)
}
}
impl From<Error> for ExecutorError {
fn from(e: Error) -> Self {
ExecutorError::StorageError(Box::new(e))
}
}
pub trait IsNotFound {
fn is_not_found(&self) -> bool;
}
impl IsNotFound for Error {
fn is_not_found(&self) -> bool {
matches!(self, Error::NotFound(_, _))
}
}
impl<T> IsNotFound for Result<T> {
fn is_not_found(&self) -> bool {
match self {
Err(err) => err.is_not_found(),
_ => false,
}
}
}
#[macro_export]
macro_rules! not_found {
($name: literal) => {
$crate::Error::NotFound($name, concat!(file!(), ":", line!()))
};
($ty: path) => {
$crate::Error::NotFound(
::core::any::type_name::<<$ty as $crate::Mappable>::OwnedValue>(),
concat!(file!(), ":", line!()),
)
};
}
#[cfg(test)]
mod test {
use crate::tables::Coins;
#[test]
fn not_found_output() {
#[rustfmt::skip]
assert_eq!(
format!("{}", not_found!("BlockId")),
format!("resource of type `BlockId` was not found at the: {}:{}", file!(), line!() - 1)
);
#[rustfmt::skip]
assert_eq!(
format!("{}", not_found!(Coins)),
format!("resource of type `fuel_core_types::entities::coin::CompressedCoin` was not found at the: {}:{}", file!(), line!() - 1)
);
}
}