use super::{BlockEnv, CfgEnv, Env, SpecId, TxEnv};
use core::ops::{Deref, DerefMut};
use std::boxed::Box;
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
pub struct HandlerCfg {
pub spec_id: SpecId,
#[cfg(feature = "optimism")]
pub is_optimism: bool,
}
impl Default for HandlerCfg {
fn default() -> Self {
Self::new(SpecId::default())
}
}
impl HandlerCfg {
pub fn new(spec_id: SpecId) -> Self {
cfg_if::cfg_if! {
if #[cfg(all(feature = "optimism-default-handler",
not(feature = "negate-optimism-default-handler")))] {
let is_optimism = true;
} else if #[cfg(feature = "optimism")] {
let is_optimism = false;
}
}
Self {
spec_id,
#[cfg(feature = "optimism")]
is_optimism,
}
}
#[cfg(feature = "optimism")]
pub fn new_with_optimism(spec_id: SpecId, is_optimism: bool) -> Self {
Self {
spec_id,
is_optimism,
}
}
pub fn is_optimism(&self) -> bool {
cfg_if::cfg_if! {
if #[cfg(feature = "optimism")] {
self.is_optimism
} else {
false
}
}
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct CfgEnvWithHandlerCfg {
pub cfg_env: CfgEnv,
pub handler_cfg: HandlerCfg,
}
impl CfgEnvWithHandlerCfg {
pub fn new(cfg_env: CfgEnv, handler_cfg: HandlerCfg) -> Self {
Self {
cfg_env,
handler_cfg,
}
}
pub fn new_with_spec_id(cfg_env: CfgEnv, spec_id: SpecId) -> Self {
Self::new(cfg_env, HandlerCfg::new(spec_id))
}
#[cfg(feature = "optimism")]
pub fn enable_optimism(&mut self) {
self.handler_cfg.is_optimism = true;
}
}
impl DerefMut for CfgEnvWithHandlerCfg {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.cfg_env
}
}
impl Deref for CfgEnvWithHandlerCfg {
type Target = CfgEnv;
fn deref(&self) -> &Self::Target {
&self.cfg_env
}
}
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct EnvWithHandlerCfg {
pub env: Box<Env>,
pub handler_cfg: HandlerCfg,
}
impl EnvWithHandlerCfg {
pub fn new(env: Box<Env>, handler_cfg: HandlerCfg) -> Self {
Self { env, handler_cfg }
}
pub fn new_with_spec_id(env: Box<Env>, spec_id: SpecId) -> Self {
Self::new(env, HandlerCfg::new(spec_id))
}
pub fn new_with_cfg_env(cfg: CfgEnvWithHandlerCfg, block: BlockEnv, tx: TxEnv) -> Self {
Self::new(Env::boxed(cfg.cfg_env, block, tx), cfg.handler_cfg)
}
pub const fn spec_id(&self) -> SpecId {
self.handler_cfg.spec_id
}
#[cfg(feature = "optimism")]
pub fn enable_optimism(&mut self) {
self.handler_cfg.is_optimism = true;
}
}
impl DerefMut for EnvWithHandlerCfg {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.env
}
}
impl Deref for EnvWithHandlerCfg {
type Target = Env;
fn deref(&self) -> &Self::Target {
&self.env
}
}