use std::{
cell::{Cell, RefCell},
path::PathBuf,
};
use jiff::Timestamp;
use tracing::instrument;
thread_local! {
static MOCK_TIMESTAMP: Cell<Option<Timestamp>> = const { Cell::new(None) };
static MOCK_ISSUES_DIR: RefCell<Option<PathBuf>> = const { RefCell::new(None) };
}
pub struct MockTimestamp;
impl MockTimestamp {
#[instrument(name = "MockTimestamp::now")]
pub fn now() -> Timestamp {
let ts = MOCK_TIMESTAMP.with(|ts| ts.get());
tracing::debug!(?ts, "returning mock timestamp");
ts.unwrap_or_else(Timestamp::now)
}
}
#[instrument]
pub fn set_timestamp(timestamp: Timestamp) {
MOCK_TIMESTAMP.with(|ts| ts.set(Some(timestamp)));
}
pub struct MockIssuesDir;
impl MockIssuesDir {
pub fn get() -> Option<PathBuf> {
MOCK_ISSUES_DIR.with(|dir| dir.borrow().clone())
}
}
#[instrument]
pub fn set_issues_dir(path: PathBuf) {
MOCK_ISSUES_DIR.with(|dir| *dir.borrow_mut() = Some(path));
}
#[instrument]
pub fn clear_issues_dir() {
MOCK_ISSUES_DIR.with(|dir| *dir.borrow_mut() = None);
}