use std::{ env::var_os, io::Write, path::PathBuf, sync::{Arc, Mutex}, }; use miette::{IntoDiagnostic, Result}; use tempfile::NamedTempFile; #[derive(Clone, Debug, Default)] pub struct State { pub emit_file: RotatingTempFile, } #[derive(Clone, Debug, Default)] pub struct RotatingTempFile(Arc>>); impl RotatingTempFile { pub fn rotate(&self) -> Result<()> { // implicitly drops the old file *self.0.lock().unwrap() = Some( if let Some(dir) = var_os("WATCHEXEC_TMPDIR") { NamedTempFile::new_in(dir) } else { NamedTempFile::new() } .into_diagnostic()?, ); Ok(()) } pub fn write(&self, data: &[u8]) -> Result<()> { if let Some(file) = self.0.lock().unwrap().as_mut() { file.write_all(data).into_diagnostic()?; } Ok(()) } pub fn path(&self) -> PathBuf { if let Some(file) = self.0.lock().unwrap().as_ref() { file.path().to_owned() } else { PathBuf::new() } } }