tatami 0.1.2

A library for creating satellites and interacting with Tatami protocols.
Documentation
use libp2p::identity::Keypair;
use std::sync::Mutex;

use crate::{Satellite, TatamiError};

use super::Module;

/// Configuration for the Satellite
/// For ease-of-use, there is a default config, but it creates a random keypair.
pub struct Blueprint {
	/// The keypair used for network cryptography. It is the most important value for a satellite
	/// and must be kept safe.
	pub keypair: Keypair,
	pub mdns_config: SatelliteMdnsConfig,
	pub bootstrap: bool,
	pub auto_connect: bool,
	pub modules: Vec<Mutex<Box<dyn Module + Send>>>,
}

#[derive(Clone)]
pub struct SatelliteMdnsConfig {
	// pub enable: bool,
	pub auto_register: bool,
}

impl Default for SatelliteMdnsConfig {
	fn default() -> Self {
		Self {
			// enable: true,
			auto_register: true,
		}
	}
}

impl Default for Blueprint {
	fn default() -> Self {
		Self {
			keypair: crate::random_key(),
			mdns_config: Default::default(),
			bootstrap: true,
			auto_connect: true,
			modules: Default::default(),
		}
	}
}

impl Blueprint {
	/// Adds a Keypair to the blueprint
	pub fn with_keypair(self, keypair: Keypair) -> Self {
		Self { keypair, ..self }
	}

	/// Adds a module to the blueprint
	pub fn with_module(self, module: Box<dyn Module + Send>) -> Self {
		let mut modules = self.modules;
		modules.push(Mutex::new(module));
		Self { modules, ..self }
	}

	pub async fn build(self) -> Result<Satellite, TatamiError> {
		Satellite::launch(self).await
	}
}