use std::path::PathBuf;
use std::sync::Mutex;
#[derive(Debug, Default)]
pub struct CodexRawDump {
lines: Mutex<Vec<String>>,
path: Option<PathBuf>,
max_lines: usize,
}
impl CodexRawDump {
pub fn new(path: Option<PathBuf>, max_lines: usize) -> Self {
Self {
lines: Mutex::new(Vec::new()),
path,
max_lines: max_lines.max(1),
}
}
pub fn push_line(&self, line: impl Into<String>) {
let line = line.into();
if let Ok(mut g) = self.lines.lock() {
if g.len() < self.max_lines {
g.push(line.clone());
}
}
if let Some(path) = &self.path {
use std::io::Write;
if let Ok(mut f) = std::fs::OpenOptions::new()
.create(true)
.append(true)
.open(path)
{
let _ = writeln!(f, "{line}");
}
}
}
pub fn snapshot(&self) -> Vec<String> {
self.lines.lock().map(|g| g.clone()).unwrap_or_default()
}
pub fn as_text(&self) -> String {
self.snapshot().join("\n")
}
}