use std::path::Path;
use super::DataDir;
#[derive(Debug, Clone)]
pub struct MockDataDir {
pub(crate) temp_dir: std::sync::Arc<tempfile::TempDir>,
pub config_toml: super::ConfigToml,
pub keypair: pkarr::Keypair,
}
impl MockDataDir {
pub fn new(
config_toml: super::ConfigToml,
keypair: Option<pkarr::Keypair>,
) -> anyhow::Result<Self> {
let keypair = keypair.unwrap_or_else(pkarr::Keypair::random);
Ok(Self {
temp_dir: std::sync::Arc::new(tempfile::TempDir::new()?),
config_toml,
keypair,
})
}
#[cfg(any(test, feature = "testing"))]
pub fn test() -> Self {
let config = super::ConfigToml::test();
let keypair = pkarr::Keypair::from_secret_key(&[0; 32]);
Self::new(config, Some(keypair)).expect("failed to create MockDataDir")
}
}
impl Default for MockDataDir {
fn default() -> Self {
Self::test()
}
}
impl DataDir for MockDataDir {
fn path(&self) -> &Path {
self.temp_dir.path()
}
fn ensure_data_dir_exists_and_is_writable(&self) -> anyhow::Result<()> {
Ok(()) }
fn read_or_create_config_file(&self) -> anyhow::Result<super::ConfigToml> {
Ok(self.config_toml.clone())
}
fn read_or_create_keypair(&self) -> anyhow::Result<pkarr::Keypair> {
Ok(self.keypair.clone())
}
}