2025-05-24 06:11:00 +09:00
|
|
|
mod node;
|
|
|
|
mod server;
|
|
|
|
|
2025-05-30 09:26:47 +09:00
|
|
|
use std::path::Path;
|
|
|
|
use crate::error::Error;
|
2025-05-24 06:11:00 +09:00
|
|
|
pub use node::NodeConfig;
|
2025-05-30 09:26:47 +09:00
|
|
|
use serde::{Deserialize, Serialize};
|
2025-05-24 09:17:45 +09:00
|
|
|
pub use server::{
|
|
|
|
PartialServerConfig,
|
|
|
|
ServerConfig,
|
|
|
|
DEFAULT_LISTEN_IPS,
|
|
|
|
DEFAULT_PORT,
|
|
|
|
DEFAULT_PARTIAL_SERVER_CONFIG,
|
|
|
|
DEFAULT_SERVER_CONFIG
|
|
|
|
};
|
2025-05-30 09:26:47 +09:00
|
|
|
use tokio::{fs::File, io::{AsyncReadExt, AsyncWriteExt}};
|
|
|
|
#[derive(Debug, Deserialize, Serialize)]
|
|
|
|
pub struct PartialConfig {
|
|
|
|
node: Option<NodeConfig>,
|
|
|
|
server: Option<ServerConfig>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PartialConfig {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
PartialConfig {
|
|
|
|
node: Some(NodeConfig::new()),
|
|
|
|
server: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pub 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
|
|
|
|
}
|
|
|
|
pub 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: PartialConfig = toml::from_str(&content)?;
|
|
|
|
Ok(config)
|
|
|
|
}
|
|
|
|
pub async fn write_to<T>(&self, path:T) -> Result<(), Error>
|
|
|
|
where
|
|
|
|
T: AsRef<Path>
|
|
|
|
{
|
|
|
|
if !path.as_ref().exists() {
|
|
|
|
let _ = File::create(&path).await?;
|
|
|
|
}
|
|
|
|
let mut file = File::open(&path).await?;
|
|
|
|
file.write_all(toml::to_string(self)?.as_bytes()).await?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2025-05-24 09:17:45 +09:00
|
|
|
|