From 8f04832397725a6969b39fecce1ae1f6f0545cb2 Mon Sep 17 00:00:00 2001 From: fluo10 Date: Thu, 14 Aug 2025 08:02:00 +0900 Subject: [PATCH] Move RpcConfig from desktop to core --- core/src/config/mod.rs | 27 +++++++++++++++++++++++++-- {desktop => core}/src/config/rpc.rs | 3 ++- desktop/src/cli/peer/list.rs | 15 +++++++++++++++ desktop/src/cli/peer/mod.rs | 0 examples/desktop/src/cli.rs | 6 +++--- 5 files changed, 45 insertions(+), 6 deletions(-) rename {desktop => core}/src/config/rpc.rs (93%) create mode 100644 desktop/src/cli/peer/list.rs create mode 100644 desktop/src/cli/peer/mod.rs diff --git a/core/src/config/mod.rs b/core/src/config/mod.rs index 79de80c..b3cb8ea 100644 --- a/core/src/config/mod.rs +++ b/core/src/config/mod.rs @@ -1,6 +1,7 @@ pub mod error; mod storage; mod p2p; +mod rpc; use std::path::Path; use crate::{utils::{emptiable::Emptiable, mergeable::Mergeable}}; @@ -10,6 +11,10 @@ use serde::{de::DeserializeOwned, Deserialize, Serialize}; use tokio::{fs::File, io::{AsyncReadExt, AsyncWriteExt}}; pub use storage::{StorageConfig, PartialStorageConfig}; pub use p2p::{P2pConfig, PartialP2pConfig}; +pub use rpc::*; + +#[cfg(feature="desktop")] +use clap::Args; pub trait Config: TryFrom{ type PartialConfig: PartialConfig; @@ -19,8 +24,25 @@ pub trait PartialConfig: Emptiable + From + Mergeable { } -pub trait BaseConfig: DeserializeOwned + Serialize { - fn new() -> Self; +#[cfg_attr(feature="desktop", derive(Args))] +#[derive(Clone, Debug, Deserialize, Serialize)] +pub struct BaseConfig { + #[cfg_attr(feature="desktop", command(flatten))] + p2p: PartialP2pConfig, + #[cfg_attr(feature="desktop", command(flatten))] + storage: PartialStorageConfig, + #[cfg_attr(feature="desktop", command(flatten))] + rpc: PartialRpcConfig, +} + +impl BaseConfig { + fn new() -> Self { + Self { + p2p : PartialP2pConfig::empty().with_new_secret(), + storage: PartialStorageConfig::empty(), + rpc: PartialRpcConfig::empty().with_unused_port(), + } + } fn from_toml(s: &str) -> Result { toml::from_str(s) } @@ -62,6 +84,7 @@ pub trait BaseConfig: DeserializeOwned + Serialize { } } + #[cfg(test)] mod tests { use serde::{Deserialize, Serialize}; diff --git a/desktop/src/config/rpc.rs b/core/src/config/rpc.rs similarity index 93% rename from desktop/src/config/rpc.rs rename to core/src/config/rpc.rs index bfafc8f..a290060 100644 --- a/desktop/src/config/rpc.rs +++ b/core/src/config/rpc.rs @@ -1,6 +1,7 @@ use std::{net::{IpAddr, Ipv4Addr, SocketAddr, TcpListener}, path::PathBuf}; +#[cfg(feature="desktop")] use clap::Args; -use caretta_core::{config::PartialConfig, utils::{emptiable::Emptiable, mergeable::Mergeable}}; +use crate::{config::PartialConfig, utils::{emptiable::Emptiable, mergeable::Mergeable}}; use libp2p::mdns::Config; use serde::{Deserialize, Serialize}; diff --git a/desktop/src/cli/peer/list.rs b/desktop/src/cli/peer/list.rs new file mode 100644 index 0000000..1e9b575 --- /dev/null +++ b/desktop/src/cli/peer/list.rs @@ -0,0 +1,15 @@ +use clap::Args; +use crate::utils::runnable::Runnable; +use crate::cli::{ConfigArgs, RunnableCommand}; + +#[derive(Debug, Args)] +pub struct DeviceListCommandArgs{ + #[command(flatten)] + config: ConfigArgs +} + +impl Runnable for DeviceListCommandArgs { + async fn run(self) { + todo!() + } +} \ No newline at end of file diff --git a/desktop/src/cli/peer/mod.rs b/desktop/src/cli/peer/mod.rs new file mode 100644 index 0000000..e69de29 diff --git a/examples/desktop/src/cli.rs b/examples/desktop/src/cli.rs index d044ea8..63d4b54 100644 --- a/examples/desktop/src/cli.rs +++ b/examples/desktop/src/cli.rs @@ -9,8 +9,8 @@ pub struct Cli { #[derive(Debug, Subcommand)] pub enum CliCommand { - Config(ConfigCommandArgs), - Device(DeviceCommandArgs), - Log(LogCommandArgs), + //Config(ConfigCommandArgs), + //Device(DeviceCommandArgs), + //Log(LogCommandArgs), Server(ServerCommandArgs), } \ No newline at end of file