use miette::NamedSource;
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::sync::{Arc, LazyLock, RwLock};
static SOURCES: LazyLock<RwLock<HashMap<PathBuf, Arc<String>>>> =
LazyLock::new(|| RwLock::new(HashMap::new()));
pub fn register(path: &Path, content: String) {
let canonical = path.canonicalize().unwrap_or_else(|_| path.to_path_buf());
if let Ok(mut sources) = SOURCES.write() {
sources.insert(canonical, Arc::new(content));
}
}
pub fn get_named_source(path: &Path) -> Option<Arc<NamedSource<Arc<String>>>> {
let canonical = path.canonicalize().unwrap_or_else(|_| path.to_path_buf());
let sources = SOURCES.read().ok()?;
sources.get(&canonical).map(|content| {
Arc::new(NamedSource::new(
path.display().to_string(),
Arc::clone(content),
))
})
}
#[cfg(test)]
pub fn get_content(path: &Path) -> Option<Arc<String>> {
let canonical = path.canonicalize().unwrap_or_else(|_| path.to_path_buf());
let sources = SOURCES.read().ok()?;
sources.get(&canonical).cloned()
}
#[cfg(test)]
mod tests {
use super::*;
use std::io::Write;
use tempfile::NamedTempFile;
#[test]
fn test_register_and_get() {
let mut temp = NamedTempFile::new().unwrap();
writeln!(temp, "test content").unwrap();
let path = temp.path();
register(path, "test content\n".to_string());
let source = get_named_source(path);
assert!(source.is_some());
let content = get_content(path);
assert_eq!(content.as_deref(), Some(&"test content\n".to_string()));
}
#[test]
fn test_missing_path() {
let path = Path::new("/nonexistent/path/to/file.toml");
assert!(get_named_source(path).is_none());
assert!(get_content(path).is_none());
}
}