use std::path::PathBuf;
use anyhow::Result;
use rudy_dwarf::{Binary, DwarfDb, file::File};
#[salsa::db]
pub trait Db: salsa::Database + DwarfDb {}
#[salsa::db]
#[derive(Clone)]
pub struct DebugDatabaseImpl {
storage: salsa::Storage<Self>,
source_map: Vec<(PathBuf, PathBuf)>,
}
pub struct DebugDbRef {
handle: salsa::StorageHandle<DebugDatabaseImpl>,
source_map: Vec<(PathBuf, PathBuf)>,
}
impl DebugDbRef {
pub fn get_db(self) -> DebugDatabaseImpl {
DebugDatabaseImpl {
storage: self.handle.into_storage(),
source_map: self.source_map,
}
}
}
impl Default for DebugDatabaseImpl {
fn default() -> Self {
Self::new()
}
}
impl DebugDatabaseImpl {
pub fn new() -> Self {
Self {
storage: salsa::Storage::default(),
source_map: Default::default(),
}
}
pub fn new_with_events(
event_callback: Option<Box<dyn Fn(salsa::Event) + Send + Sync + 'static>>,
) -> Self {
Self {
storage: salsa::Storage::new(event_callback),
source_map: Default::default(),
}
}
pub fn with_source_map(mut self, source_map: Vec<(PathBuf, PathBuf)>) -> Self {
self.source_map = source_map;
self
}
pub(crate) fn load_binary(&self, binary_file: PathBuf) -> Result<Binary> {
let file = File::build(self, binary_file, None)?;
Ok(Binary::new(self, file))
}
pub fn get_sync_ref(&self) -> DebugDbRef {
DebugDbRef {
handle: self.storage.clone().into_zalsa_handle(),
source_map: self.source_map.clone(),
}
}
}
#[salsa::db]
impl salsa::Database for DebugDatabaseImpl {}
#[salsa::db]
impl DwarfDb for DebugDatabaseImpl {
fn get_source_map(&self) -> &[(PathBuf, PathBuf)] {
&self.source_map
}
}
#[salsa::db]
impl Db for DebugDatabaseImpl {}