use crate::Result;
use crate::plugins::{PostProcessor, ProcessingStage};
use once_cell::sync::Lazy;
use std::sync::Arc;
use std::sync::RwLock as StdRwLock;
pub(super) struct ProcessorCache {
pub(super) early: Arc<Vec<Arc<dyn PostProcessor>>>,
pub(super) middle: Arc<Vec<Arc<dyn PostProcessor>>>,
pub(super) late: Arc<Vec<Arc<dyn PostProcessor>>>,
}
impl ProcessorCache {
pub(super) fn new() -> Result<Self> {
let processor_registry = crate::plugins::registry::get_post_processor_registry();
let registry = processor_registry
.read()
.map_err(|e| crate::KreuzbergError::Other(format!("Post-processor registry lock poisoned: {}", e)))?;
Ok(Self {
early: Arc::new(registry.get_for_stage(ProcessingStage::Early)),
middle: Arc::new(registry.get_for_stage(ProcessingStage::Middle)),
late: Arc::new(registry.get_for_stage(ProcessingStage::Late)),
})
}
#[allow(dead_code)]
pub(super) fn get_for_stage(&self, stage: ProcessingStage) -> Arc<Vec<Arc<dyn PostProcessor>>> {
match stage {
ProcessingStage::Early => Arc::clone(&self.early),
ProcessingStage::Middle => Arc::clone(&self.middle),
ProcessingStage::Late => Arc::clone(&self.late),
}
}
}
pub(super) static PROCESSOR_CACHE: Lazy<StdRwLock<Option<ProcessorCache>>> = Lazy::new(|| StdRwLock::new(None));
#[allow(dead_code)]
pub fn clear_processor_cache() -> Result<()> {
let mut cache = PROCESSOR_CACHE
.write()
.map_err(|e| crate::KreuzbergError::Other(format!("Processor cache lock poisoned: {}", e)))?;
*cache = None;
Ok(())
}