syncweb-core 0.1.1-alpha.7

delay-tolerant web surfing
Documentation
use std::collections::HashSet;
use std::path::{Path, PathBuf};
use std::sync::{Arc, RwLock};

pub fn empty_member_keys() -> Arc<RwLock<HashSet<iroh::PublicKey>>> {
    Arc::new(RwLock::new(HashSet::new()))
}

pub struct TestDirectory(PathBuf);

impl TestDirectory {
    /// Create a new test directory.
    ///
    /// # Errors
    ///
    /// Returns an error if the directory cannot be created.
    pub fn new(name: &str) -> anyhow::Result<Self> {
        let dir = std::env::temp_dir().join(format!("{}-{}", name, uuid::Uuid::new_v4()));
        std::fs::create_dir_all(&dir)?;
        Ok(Self(dir))
    }

    #[must_use]
    pub fn path(&self) -> &Path {
        &self.0
    }
}

impl Drop for TestDirectory {
    fn drop(&mut self) {
        if let Err(error) = std::fs::remove_dir_all(&self.0) {
            eprintln!("failed to remove test directory {}: {error}", self.0.display());
        }
    }
}