wooly-vault/src/vault.rs

104 lines
2.9 KiB
Rust
Raw Normal View History

2024-09-06 23:09:10 +02:00
//! A module for managing and sharing secrets in the Wooly Vault application.
//!
//! This module provides a [`Vault`] struct that holds a secret and allows it to be shared safely
//! across the application using an [`Arc`] (Atomic Reference Counted) pointer.
2024-09-07 19:37:52 +02:00
use std::collections::HashSet;
use std::fmt::{Debug, Display};
use std::net::{IpAddr, SocketAddr};
2024-09-05 17:18:08 +02:00
use std::sync::Arc;
2024-09-07 19:37:52 +02:00
use libpt::log::info;
use serde::{Deserialize, Serialize};
2024-09-07 19:37:52 +02:00
use tokio::sync::Mutex;
2024-09-06 23:09:10 +02:00
/// A type alias for an [`Arc`] pointer to a [`Vault`] instance.
///
/// This type is used to share a [`Vault`] instance across multiple parts of the application.
2024-09-05 17:18:08 +02:00
pub type VaultRef = Arc<Vault>;
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, PartialOrd, Ord)]
2024-09-07 19:37:52 +02:00
pub struct Contestant {
ip: IpAddr,
}
2024-09-06 23:09:10 +02:00
/// A struct that holds a secret and provides methods for accessing it.
///
/// The [`Vault`] struct is designed to be shared safely across the application using an [`Arc`] pointer.
2024-09-07 19:37:52 +02:00
#[derive(Debug, Clone)]
2024-09-05 17:18:08 +02:00
pub struct Vault {
2024-09-06 23:09:10 +02:00
/// The secret stored in the vault
2024-09-05 17:18:08 +02:00
secret: String,
2024-09-07 19:37:52 +02:00
contestants: Arc<Mutex<HashSet<Contestant>>>,
winners: Arc<Mutex<HashSet<Contestant>>>,
}
impl From<IpAddr> for Contestant {
fn from(value: IpAddr) -> Self {
Self { ip: value }
}
}
impl From<SocketAddr> for Contestant {
fn from(value: SocketAddr) -> Self {
Self { ip: value.ip() }
}
}
impl From<&SocketAddr> for Contestant {
fn from(value: &SocketAddr) -> Self {
Self { ip: value.ip() }
}
}
impl Display for Contestant {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.ip)
2024-09-07 19:37:52 +02:00
}
2024-09-05 17:18:08 +02:00
}
impl Vault {
2024-09-06 23:09:10 +02:00
/// Creates a new [`Vault`] instance with the given secret.
///
/// Returns an [`Arc`] pointer to the new [`Vault`] instance.
///
/// # Returns
///
/// A new [`Vault`] instance with the given secret.
2024-09-05 17:18:08 +02:00
pub fn new(secret: &str) -> VaultRef {
let v = Self {
secret: secret.to_string(),
2024-09-07 19:37:52 +02:00
contestants: Default::default(),
winners: Default::default(),
2024-09-05 17:18:08 +02:00
};
Arc::new(v)
}
2024-09-06 23:09:10 +02:00
/// Returns a reference to the secret stored in the vault.
///
/// # Returns
///
/// A reference to the secret stored in the vault.
2024-09-05 17:18:08 +02:00
pub fn secret(&self) -> &str {
&self.secret
}
2024-09-07 19:37:52 +02:00
pub async fn add_contestant(&self, contestant: Contestant) -> bool {
info!("new contestant: {contestant}");
self.contestants.lock().await.insert(contestant)
}
pub async fn add_winner(&self, contestant: Contestant) -> bool {
info!("new winner: {contestant}");
self.winners.lock().await.insert(contestant)
}
pub async fn contestants(&self) -> HashSet<Contestant> {
self.contestants.lock().await.clone()
}
pub async fn winners(&self) -> HashSet<Contestant> {
self.winners.lock().await.clone()
2024-09-07 19:37:52 +02:00
}
2024-09-05 17:18:08 +02:00
}