use std::sync::Arc;
use parking_lot::RwLock;
use scc::HashMap as SccHashMap;
#[derive(Clone, Default)]
pub struct ReflectionRegistry {
services: Arc<RwLock<Vec<String>>>,
files: Arc<SccHashMap<String, Vec<u8>>>,
symbols: Arc<SccHashMap<String, String>>,
}
impl ReflectionRegistry {
pub fn new() -> Self {
Self::default()
}
pub fn add_service(&self, name: impl Into<String>) {
self.services.write().push(name.into());
}
pub fn add_file(&self, filename: impl Into<String>, descriptor: Vec<u8>) {
self.files.upsert_sync(filename.into(), descriptor);
}
pub fn map_symbol(&self, symbol: impl Into<String>, filename: impl Into<String>) {
self.symbols.upsert_sync(symbol.into(), filename.into());
}
pub fn list_services(&self) -> Vec<String> {
self.services.read().clone()
}
pub fn file_by_filename(&self, filename: &str) -> Option<Vec<u8>> {
self.files.get_sync(filename).map(|e| e.get().clone())
}
pub fn file_containing_symbol(&self, symbol: &str) -> Option<Vec<u8>> {
let entry = self.symbols.get_sync(symbol)?;
let filename = entry.get().clone();
drop(entry);
self.file_by_filename(&filename)
}
}
pub struct ReflectionState {
pub registry: ReflectionRegistry,
}