2025-06-19 07:24:44 +09:00
|
|
|
use std::path::PathBuf;
|
|
|
|
|
|
|
|
#[cfg(feature="desktop")]
|
|
|
|
use clap::Args;
|
2025-06-24 22:06:25 +09:00
|
|
|
|
|
|
|
#[cfg(any(test, feature="test"))]
|
|
|
|
use tempfile::tempdir;
|
2025-07-04 08:05:43 +09:00
|
|
|
use crate::{config::{ConfigError, PartialConfig}, utils::emptiable::Emptiable};
|
2025-06-19 07:24:44 +09:00
|
|
|
use libp2p::mdns::Config;
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
|
|
|
static DATA_DATABASE_NAME: &str = "data.sqlite";
|
|
|
|
static CACHE_DATABASE_NAME: &str = "cache.sqlite";
|
|
|
|
|
|
|
|
#[cfg(any(test, feature="test"))]
|
2025-06-22 11:29:44 +09:00
|
|
|
use crate::tests::{GlobalTestDefault, TestDefault};
|
2025-06-19 07:24:44 +09:00
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct StorageConfig {
|
|
|
|
pub data_directory: PathBuf,
|
|
|
|
pub cache_directory: PathBuf,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl StorageConfig {
|
|
|
|
pub fn get_data_database_path(&self) -> PathBuf{
|
|
|
|
self.data_directory.join(DATA_DATABASE_NAME)
|
|
|
|
}
|
|
|
|
pub fn get_cache_database_path(&self) -> PathBuf {
|
|
|
|
self.cache_directory.join(CACHE_DATABASE_NAME)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-06-22 11:29:44 +09:00
|
|
|
#[cfg(any(test, feature="test"))]
|
|
|
|
impl TestDefault for StorageConfig {
|
|
|
|
fn test_default() -> Self {
|
2025-06-24 22:06:25 +09:00
|
|
|
|
|
|
|
let temp_dir = tempdir().unwrap().keep();
|
|
|
|
Self { data_directory: temp_dir.clone(), cache_directory: temp_dir }
|
2025-06-22 11:29:44 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-06-19 07:24:44 +09:00
|
|
|
impl TryFrom<PartialStorageConfig> for StorageConfig {
|
|
|
|
type Error = ConfigError;
|
|
|
|
|
|
|
|
fn try_from(value: PartialStorageConfig) -> Result<Self, Self::Error> {
|
|
|
|
Ok(Self {
|
|
|
|
data_directory: value.data_directory.ok_or(ConfigError::MissingConfig("data_directory".to_string()))?,
|
|
|
|
cache_directory: value.cache_directory.ok_or(ConfigError::MissingConfig("cache_directory".to_string()))?,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#[cfg_attr(feature="desktop", derive(Args))]
|
2025-07-04 08:05:43 +09:00
|
|
|
#[cfg_attr(feature="macros", derive(Emptiable))]
|
2025-06-19 07:24:44 +09:00
|
|
|
#[derive(Clone, Debug, Deserialize, Serialize)]
|
|
|
|
pub struct PartialStorageConfig {
|
|
|
|
#[cfg_attr(feature="desktop", arg(long))]
|
|
|
|
pub data_directory: Option<PathBuf>,
|
|
|
|
#[cfg_attr(feature="desktop", arg(long))]
|
|
|
|
pub cache_directory: Option<PathBuf>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<StorageConfig> for PartialStorageConfig {
|
|
|
|
fn from(config: StorageConfig) -> PartialStorageConfig {
|
|
|
|
Self {
|
|
|
|
data_directory: Some(config.data_directory),
|
|
|
|
cache_directory: Some(config.cache_directory),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|