use anyhow::Result;
use rusqlite::Connection;
use crate::catalog::{self, RecordingDetail, RecordingRow, SkillRow};
use crate::span::{self, Event};
pub trait Catalog {
fn recordings(&self) -> Vec<RecordingRow>;
fn detail(&self, rec_id: &str) -> Option<RecordingDetail>;
fn raw_events(&self, rec_id: &str) -> Vec<Event>;
fn skills(&self) -> Vec<SkillRow>;
fn refresh(&mut self) -> Result<()> {
Ok(())
}
}
pub struct FsCatalog {
conn: Connection,
}
impl FsCatalog {
pub fn new() -> Result<Self> {
Ok(Self {
conn: catalog::open_in_memory_indexed()?,
})
}
}
impl Catalog for FsCatalog {
fn recordings(&self) -> Vec<RecordingRow> {
catalog::list_recordings(&self.conn).unwrap_or_default()
}
fn detail(&self, rec_id: &str) -> Option<RecordingDetail> {
catalog::show_recording(&self.conn, rec_id).ok().flatten()
}
fn raw_events(&self, rec_id: &str) -> Vec<Event> {
let Ok(path) = crate::paths::span_file(rec_id) else {
return Vec::new();
};
span::read_span(&path).unwrap_or_default()
}
fn skills(&self) -> Vec<SkillRow> {
catalog::list_skills(&self.conn).unwrap_or_default()
}
fn refresh(&mut self) -> Result<()> {
self.conn = catalog::open_in_memory_indexed()?;
Ok(())
}
}