use std::{ops::Deref, rc::Rc};
use crate::tx_mock::{TxContext, TxResult};
use super::{BlockchainUpdate, TxContextStack};
#[derive(Debug)]
pub struct TxContextRef(Rc<TxContext>);
pub type DebugApi = TxContextRef;
impl Deref for TxContextRef {
type Target = TxContext;
fn deref(&self) -> &Self::Target {
self.0.deref()
}
}
impl Clone for TxContextRef {
fn clone(&self) -> Self {
Self(self.0.clone())
}
}
impl TxContextRef {
pub fn new(tx_context_rc: Rc<TxContext>) -> Self {
Self(tx_context_rc)
}
pub fn new_from_static() -> Self {
let tx_context_rc = TxContextStack::static_peek();
Self(tx_context_rc)
}
pub fn dummy() -> Self {
let tx_context = TxContext::dummy();
let tx_context_rc = Rc::new(tx_context);
TxContextStack::static_push(tx_context_rc.clone());
Self(tx_context_rc)
}
pub fn into_blockchain_updates(self) -> BlockchainUpdate {
let tx_context = Rc::try_unwrap(self.0).unwrap();
let tx_cache = Rc::try_unwrap(tx_context.tx_cache).unwrap();
tx_cache.into_blockchain_updates()
}
pub fn into_tx_result(self) -> TxResult {
self.tx_result_cell.replace(TxResult::default())
}
pub fn printed_messages(&self) -> Vec<String> {
self.0.printed_messages.borrow().clone()
}
pub fn printed_messages_clear(&self) {
self.0.printed_messages.borrow_mut().clear();
}
}