use std::path::{Path, PathBuf};
use nostr::types::RelayUrl;
use crate::error::Error;
use crate::store::NostrSqlite;
#[derive(Default)]
pub(crate) enum DatabaseConnType {
#[default]
InMemory,
#[cfg(not(target_arch = "wasm32"))]
File(PathBuf),
WithVFS {
path: PathBuf,
vfs: String,
},
}
pub struct NostrSqliteBuilder {
pub(crate) db_type: DatabaseConnType,
pub(crate) process_nip62: bool,
pub(crate) process_nip09: bool,
pub(crate) relay_url: Option<RelayUrl>,
}
impl NostrSqliteBuilder {
#[inline]
pub fn in_memory(mut self) -> Self {
self.db_type = DatabaseConnType::InMemory;
self
}
#[inline]
#[cfg(not(target_arch = "wasm32"))]
pub fn in_file<P>(mut self, path: P) -> Self
where
P: AsRef<Path>,
{
self.db_type = DatabaseConnType::File(path.as_ref().to_path_buf());
self
}
#[inline]
pub fn persistent_with_vfs<P, S>(mut self, path: P, vfs: S) -> Self
where
P: AsRef<Path>,
S: Into<String>,
{
self.db_type = DatabaseConnType::WithVFS {
path: path.as_ref().to_path_buf(),
vfs: vfs.into(),
};
self
}
#[inline]
pub fn process_nip62(mut self, process_nip62: bool) -> Self {
self.process_nip62 = process_nip62;
self
}
#[inline]
pub fn process_nip09(mut self, process_nip09: bool) -> Self {
self.process_nip09 = process_nip09;
self
}
#[inline]
pub fn relay_url(mut self, relay_url: RelayUrl) -> Self {
self.relay_url = Some(relay_url);
self
}
#[inline]
pub async fn build(self) -> Result<NostrSqlite, Error> {
NostrSqlite::from_builder(self).await
}
}
impl Default for NostrSqliteBuilder {
fn default() -> Self {
Self {
db_type: Default::default(),
process_nip62: true,
process_nip09: true,
relay_url: None,
}
}
}