12345678910111213141516171819#[cfg(not(feature = "std"))]20use alloc::string::{String, ToString};21use evm_core::{ExitError, ExitFatal};22#[cfg(feature = "std")]23use std::string::{String, ToString};2425use crate::Weight;262728#[derive(Debug, Clone)]29pub enum Error {30 31 Revert(String),32 33 Fatal(ExitFatal),34 35 Error(ExitError),36}3738impl<E> From<E> for Error39where40 E: ToString,41{42 fn from(e: E) -> Self {43 Self::Revert(e.to_string())44 }45}464748pub type Result<T> = core::result::Result<T, Error>;495051pub struct DispatchInfo {52 53 pub weight: Weight,54}5556impl From<Weight> for DispatchInfo {57 fn from(weight: Weight) -> Self {58 Self { weight }59 }60}61impl From<u64> for DispatchInfo {62 fn from(weight: u64) -> Self {63 Self {64 weight: Weight::from_ref_time(weight),65 }66 }67}68impl From<()> for DispatchInfo {69 fn from(_: ()) -> Self {70 Self {71 weight: Weight::zero(),72 }73 }74}75767778#[derive(Default, Clone)]79pub struct PostDispatchInfo {80 81 actual_weight: Option<Weight>,82}8384impl PostDispatchInfo {85 86 pub fn calc_unspent(&self, info: &DispatchInfo) -> Weight {87 info.weight - self.calc_actual_weight(info)88 }8990 91 92 pub fn calc_actual_weight(&self, info: &DispatchInfo) -> Weight {93 if let Some(actual_weight) = self.actual_weight {94 actual_weight.min(info.weight)95 } else {96 info.weight97 }98 }99}100101102#[derive(Clone)]103pub struct WithPostDispatchInfo<T> {104 105 pub data: T,106 107 pub post_info: PostDispatchInfo,108}109110impl<T> From<T> for WithPostDispatchInfo<T> {111 fn from(data: T) -> Self {112 Self {113 data,114 post_info: Default::default(),115 }116 }117}118119120pub type ResultWithPostInfo<T> =121 core::result::Result<WithPostDispatchInfo<T>, WithPostDispatchInfo<Error>>;