1use crate::Terminal;
4use once_cell::sync::Lazy;
5use std::sync::Mutex;
6use std::sync::{MutexGuard, PoisonError};
7use thiserror::Error;
8
9pub(crate) static TERMINAL: Lazy<Mutex<Option<Terminal>>> = Lazy::new(|| Mutex::new(None));
10
11#[derive(Error, Debug)]
12pub enum CacheError {
13 #[error("mutex poisoned")]
14 MutexError(#[from] PoisonError<MutexGuard<'static, Option<Terminal>>>),
15 #[error("cache is empty")]
16 EmptyError,
17}
18
19pub fn cache_get() -> Result<Terminal, CacheError> {
21 let term_lock = TERMINAL.lock()?;
22 match term_lock.clone() {
23 Some(term) => Ok(term),
24 None => Err(CacheError::EmptyError),
25 }
26}
27
28pub fn cache_set(term: Terminal) -> Result<(), CacheError> {
30 let mut term_lock = TERMINAL.lock()?;
31 term_lock.replace(term);
32 Ok(())
33}
34
35pub fn cache_clear() -> Result<Option<Terminal>, CacheError> {
37 let mut term_lock = TERMINAL.lock()?;
38 Ok(term_lock.take())
39}