From 5ee1ffca63d670cfd57b41a74fa954d7ff5f0eb5 Mon Sep 17 00:00:00 2001 From: fluo10 Date: Thu, 21 Aug 2025 07:40:33 +0900 Subject: [PATCH] Update Runnable and fix errors --- examples/core/Cargo.toml | 2 ++ examples/core/src/global.rs | 1 + examples/core/src/lib.rs | 3 ++- examples/core/src/server.rs | 18 +++++++++++++----- examples/desktop/src/cli/mod.rs | 9 +++++---- examples/desktop/src/cli/server.rs | 7 ++++--- examples/desktop/src/main.rs | 2 ++ 7 files changed, 29 insertions(+), 13 deletions(-) create mode 100644 examples/core/src/global.rs diff --git a/examples/core/Cargo.toml b/examples/core/Cargo.toml index 41c03d9..546117a 100644 --- a/examples/core/Cargo.toml +++ b/examples/core/Cargo.toml @@ -8,3 +8,5 @@ repository.workspace = true [dependencies] caretta.path = "../.." +libp2p.workspace = true +tokio.workspace = true diff --git a/examples/core/src/global.rs b/examples/core/src/global.rs new file mode 100644 index 0000000..14d3e83 --- /dev/null +++ b/examples/core/src/global.rs @@ -0,0 +1 @@ +pub const APP_NAME: &str = "caretta_demo"; diff --git a/examples/core/src/lib.rs b/examples/core/src/lib.rs index 6107810..1eaa7fe 100644 --- a/examples/core/src/lib.rs +++ b/examples/core/src/lib.rs @@ -1,2 +1,3 @@ +pub mod global; pub mod rpc; -pub mod server; \ No newline at end of file +pub mod server; diff --git a/examples/core/src/server.rs b/examples/core/src/server.rs index 0f2c331..8aa5762 100644 --- a/examples/core/src/server.rs +++ b/examples/core/src/server.rs @@ -1,17 +1,20 @@ use caretta::{config::P2pConfig, server::ServerTrait}; - -pub struct Server{}; +use libp2p::{futures::StreamExt, noise, swarm::SwarmEvent, tcp, yamux}; +pub struct Server{} impl ServerTrait for Server { - async fn serve_p2p(config: P2pConfig) -> Result<(), caretta::error::Error> { - let mut swarm = libp2p::SwarmBuilder::with_existing_identity(self.secret) + async fn serve_p2p(config: &T) -> Result<(), caretta::error::Error> + where + T: AsRef + { + let mut swarm = libp2p::SwarmBuilder::with_existing_identity(config.as_ref().secret.clone()) .with_tokio() .with_tcp( tcp::Config::default(), noise::Config::new, yamux::Config::default, )? - .with_behaviour(|keypair| p2p::Behaviour::try_from(keypair).unwrap())? + .with_behaviour(|keypair| caretta::p2p::Behaviour::try_from(keypair).unwrap())? .build(); swarm.listen_on("/ip4/0.0.0.0/tcp/0".parse()?)?; loop{ @@ -28,4 +31,9 @@ impl ServerTrait for Server { }); } } + + async fn serve_rpc(config: &T) -> Result<(), caretta::error::Error> + where T: AsRef { + todo!() + } } \ No newline at end of file diff --git a/examples/desktop/src/cli/mod.rs b/examples/desktop/src/cli/mod.rs index 1006c13..560f2ea 100644 --- a/examples/desktop/src/cli/mod.rs +++ b/examples/desktop/src/cli/mod.rs @@ -1,20 +1,21 @@ mod server; use clap::{Parser, Subcommand}; -use caretta::cli::*; +use caretta::{cli::*, utils::runnable::Runnable}; pub use server::*; -#[derive(Debug, Parser)] +#[derive(Debug, Parser, Runnable)] pub struct Cli { #[command(subcommand)] + #[runnable] command: CliCommand } -#[derive(Debug, Subcommand)] +#[derive(Debug, Subcommand, Runnable)] pub enum CliCommand { Config(ConfigCommandArgs), Device(DeviceCommandArgs), Logs(LogsCommandArgs), - Peer(PeerSubcommand), + Peer(PeerCommandArgs), Server(ServerCommandArgs), } \ No newline at end of file diff --git a/examples/desktop/src/cli/server.rs b/examples/desktop/src/cli/server.rs index 1c665d9..2cfc5ac 100644 --- a/examples/desktop/src/cli/server.rs +++ b/examples/desktop/src/cli/server.rs @@ -1,5 +1,5 @@ use clap::Args; -use caretta::{error::Error, global::CONFIG, utils::runnable::Runnable}; +use caretta::{config::Config, error::Error, global::CONFIG, utils::runnable::Runnable}; use libp2p::{noise, ping, swarm::{NetworkBehaviour, SwarmEvent}, tcp, yamux, Swarm}; use super::ConfigArgs; @@ -10,7 +10,8 @@ pub struct ServerCommandArgs { config: ConfigArgs, } impl Runnable for ServerCommandArgs { - async fn run(self) { - let config = CONFIG.get_or_init(self.config.try_into()?).await; + async fn run(self, app_name: &'static str) { + let config = CONFIG.get_or_init::(self.config.into_config_unchecked(app_name).await).await; + } } \ No newline at end of file diff --git a/examples/desktop/src/main.rs b/examples/desktop/src/main.rs index a061f39..82a3a84 100644 --- a/examples/desktop/src/main.rs +++ b/examples/desktop/src/main.rs @@ -1,3 +1,5 @@ +use clap::Parser; + use crate::cli::Cli; mod cli;