use crate::Terminal;
use once_cell::sync::Lazy;
use std::sync::Mutex;
use std::sync::{MutexGuard, PoisonError};
use thiserror::Error;
pub(crate) static TERMINAL: Lazy<Mutex<Option<Terminal>>> = Lazy::new(|| Mutex::new(None));
#[derive(Error, Debug)]
pub enum CacheError {
#[error("mutex poisoned")]
MutexError(#[from] PoisonError<MutexGuard<'static, Option<Terminal>>>),
#[error("cache is empty")]
EmptyError,
}
pub fn cache_get() -> Result<Terminal, CacheError> {
let term_lock = TERMINAL.lock()?;
match term_lock.clone() {
Some(term) => Ok(term),
None => Err(CacheError::EmptyError),
}
}
pub fn cache_set(term: Terminal) -> Result<(), CacheError> {
let mut term_lock = TERMINAL.lock()?;
term_lock.replace(term);
Ok(())
}
pub fn cache_clear() -> Result<Option<Terminal>, CacheError> {
let mut term_lock = TERMINAL.lock()?;
Ok(term_lock.take())
}