nostr-memory 0.45.0-alpha.1

Nostr in-memory events database implementation
//! Memory Database Builder

use core::num::NonZeroUsize;

use nostr::types::RelayUrl;

use crate::MemoryDatabase;

/// Memory Database Builder
#[derive(Debug)]
pub struct MemoryDatabaseBuilder {
    /// Max number of events to store in memory. If None, there is no limit.
    pub(crate) max_events: Option<NonZeroUsize>,
    /// Whether to process event deletion request (NIP-09) events.
    ///
    /// Defaults to `true`
    pub(crate) process_nip09: bool,
    /// Whether to process request to vanish (NIP-62) events.
    pub(crate) process_nip62: bool,
    /// Relay URL for relay-specific request to vanish (NIP-62).
    pub(crate) relay_url: Option<RelayUrl>,
}

impl Default for MemoryDatabaseBuilder {
    fn default() -> Self {
        Self {
            max_events: None,
            process_nip09: true,
            process_nip62: true,
            relay_url: None,
        }
    }
}

impl MemoryDatabaseBuilder {
    /// Set a maximum number of events to store in memory.
    ///
    /// When the limit is reached, the oldest events will be removed to make space for new ones.
    #[inline]
    pub fn max_events(mut self, max_events: NonZeroUsize) -> Self {
        self.max_events = Some(max_events);
        self
    }

    /// Whether to process event deletion request (NIP-09) events.
    ///
    /// Defaults to `true`
    #[inline]
    pub fn process_nip09(mut self, process_nip09: bool) -> Self {
        self.process_nip09 = process_nip09;
        self
    }

    /// Whether to process request to vanish (NIP-62) events
    ///
    /// Defaults to `true`
    #[inline]
    pub fn process_nip62(mut self, process_nip62: bool) -> Self {
        self.process_nip62 = process_nip62;
        self
    }

    /// Set the relay URL to handle relay-specific request to vanish
    #[inline]
    pub fn relay_url(mut self, relay_url: RelayUrl) -> Self {
        self.relay_url = Some(relay_url);
        self
    }

    /// Build the in-memory database.
    #[inline]
    pub fn build(self) -> MemoryDatabase {
        MemoryDatabase::from_builder(self)
    }
}