caretta-sync/lazy-supplements-core/src/config/mod.rs

112 lines
2.9 KiB
Rust
Raw Normal View History

2025-06-18 08:36:01 +09:00
pub mod error;
2025-06-19 07:24:44 +09:00
mod storage;
mod p2p;
2025-05-24 06:11:00 +09:00
2025-05-30 09:26:47 +09:00
use std::path::Path;
use crate::error::Error;
2025-06-18 08:36:01 +09:00
pub use error::ConfigError;
use serde::{de::DeserializeOwned, Deserialize, Serialize};
2025-05-30 09:26:47 +09:00
use tokio::{fs::File, io::{AsyncReadExt, AsyncWriteExt}};
2025-06-19 07:24:44 +09:00
pub use storage::{StorageConfig, PartialStorageConfig};
pub use p2p::{P2pConfig, PartialP2pConfig};
pub trait PartialConfig: Serialize + Sized + DeserializeOwned
{
2025-06-18 08:36:01 +09:00
fn default() -> Self;
fn empty() -> Self;
fn merge(&mut self, other: Self);
2025-06-19 07:24:44 +09:00
fn from_toml(s: &str) -> Result<Self, toml::de::Error> {
toml::from_str(s)
}
fn into_toml(&self) -> Result<String, toml::ser::Error> {
toml::to_string(self)
}
fn is_empty(&self) -> bool;
2025-05-30 09:26:47 +09:00
}
2025-07-02 08:32:15 +09:00
pub trait PartialConfigRoot: DeserializeOwned + Serialize {
2025-06-18 08:36:01 +09:00
fn new() -> Self;
2025-05-24 09:17:45 +09:00
2025-06-18 08:36:01 +09:00
async fn read_or_create<T>(path: T) -> Result<Self, Error>
where
T: AsRef<Path>
{
if !path.as_ref().exists() {
Self::new().write_to(&path).await?;
}
Self::read_from(&path).await
}
async fn read_from<T>(path:T) -> Result<Self, Error>
where
T: AsRef<Path>
{
let mut file = File::open(path.as_ref()).await?;
let mut content = String::new();
file.read_to_string(&mut content).await?;
let config: Self = toml::from_str(&content)?;
Ok(config)
}
async fn write_to<T>(&self, path:T) -> Result<(), Error>
where
T: AsRef<Path>
{
if !path.as_ref().exists() {
if let Some(x) = path.as_ref().parent() {
std::fs::create_dir_all(x)?;
};
let _ = File::create(&path).await?;
}
let mut file = File::create(&path).await?;
file.write_all(toml::to_string(self)?.as_bytes()).await?;
Ok(())
}
2025-06-19 07:24:44 +09:00
}
#[cfg(test)]
mod tests {
use serde::{Deserialize, Serialize};
use crate::tests::test_toml_serialize_deserialize;
use super::{p2p::{P2pConfig, PartialP2pConfig}, PartialConfig};
#[derive(Debug, Deserialize, Serialize, PartialEq)]
pub struct TestConfig {
p2p: Option<PartialP2pConfig>
}
impl PartialConfig for TestConfig {
fn default() -> Self {
Self {
p2p: Some(PartialP2pConfig::default()),
}
}
fn empty() -> Self {
Self {
p2p: None,
}
}
fn is_empty(&self) -> bool {
self.p2p.is_none()
}
fn merge(&mut self, other: Self) {
if let Some(p2p) = other.p2p {
self.p2p = Some(p2p);
}
}
}
#[tokio::test]
async fn test_p2p_config_serialize_deserialize() {
test_toml_serialize_deserialize(TestConfig::empty());
test_toml_serialize_deserialize(TestConfig::default());
assert_eq!(TestConfig::empty(), toml::from_str("").unwrap());
assert_eq!("", &toml::to_string(&TestConfig::empty()).unwrap());
}
2025-06-18 08:36:01 +09:00
}