use std::cell::RefCell;
use std::collections::HashMap;
thread_local! {
pub static HTTP_HEADERS: RefCell<Option<HashMap<String, String>>> = const { RefCell::new(None) };
pub static STDOUT_CAPTURE: RefCell<String> = const { RefCell::new(String::new()) };
pub static DEBUG_CONTROLLER: RefCell<Option<std::sync::Arc<crate::common::debug::DebugController>>> = const { RefCell::new(None) };
pub static CURRENT_PROCEDURE_NAME: RefCell<Option<String>> = const { RefCell::new(None) };
}
pub fn with_http_headers_and_debug<F, R>(
headers: HashMap<String, String>,
debug_controller: Option<std::sync::Arc<crate::common::debug::DebugController>>,
f: F,
) -> R
where
F: FnOnce() -> R,
{
HTTP_HEADERS.with(|h| *h.borrow_mut() = Some(headers));
STDOUT_CAPTURE.with(|s| s.borrow_mut().clear());
DEBUG_CONTROLLER.with(|c| *c.borrow_mut() = debug_controller);
let result = f();
HTTP_HEADERS.with(|h| *h.borrow_mut() = None);
DEBUG_CONTROLLER.with(|c| *c.borrow_mut() = None);
result
}
pub fn set_current_procedure_name(name: Option<String>) {
CURRENT_PROCEDURE_NAME.with(|n| *n.borrow_mut() = name);
}
pub fn get_current_procedure_name() -> Option<String> {
CURRENT_PROCEDURE_NAME.with(|n| n.borrow().clone())
}
pub fn with_http_headers<F, R>(headers: HashMap<String, String>, f: F) -> R
where
F: FnOnce() -> R,
{
with_http_headers_and_debug(headers, None, f)
}
pub fn append_stdout(s: &str) {
STDOUT_CAPTURE.with(|out| {
out.borrow_mut().push_str(s);
});
}
pub fn get_stdout() -> String {
STDOUT_CAPTURE.with(|out| out.borrow().clone())
}
pub fn get_debug_controller() -> Option<std::sync::Arc<crate::common::debug::DebugController>> {
DEBUG_CONTROLLER.with(|c| c.borrow().clone())
}