Update Runnable and fix errors

This commit is contained in:
fluo10 2025-08-21 07:40:33 +09:00
parent be896aaae3
commit 5ee1ffca63
7 changed files with 29 additions and 13 deletions

View file

@ -8,3 +8,5 @@ repository.workspace = true
[dependencies] [dependencies]
caretta.path = "../.." caretta.path = "../.."
libp2p.workspace = true
tokio.workspace = true

View file

@ -0,0 +1 @@
pub const APP_NAME: &str = "caretta_demo";

View file

@ -1,2 +1,3 @@
pub mod global;
pub mod rpc; pub mod rpc;
pub mod server; pub mod server;

View file

@ -1,17 +1,20 @@
use caretta::{config::P2pConfig, server::ServerTrait}; use caretta::{config::P2pConfig, server::ServerTrait};
use libp2p::{futures::StreamExt, noise, swarm::SwarmEvent, tcp, yamux};
pub struct Server{}; pub struct Server{}
impl ServerTrait for Server { impl ServerTrait for Server {
async fn serve_p2p(config: P2pConfig) -> Result<(), caretta::error::Error> { async fn serve_p2p<T>(config: &T) -> Result<(), caretta::error::Error>
let mut swarm = libp2p::SwarmBuilder::with_existing_identity(self.secret) where
T: AsRef<P2pConfig>
{
let mut swarm = libp2p::SwarmBuilder::with_existing_identity(config.as_ref().secret.clone())
.with_tokio() .with_tokio()
.with_tcp( .with_tcp(
tcp::Config::default(), tcp::Config::default(),
noise::Config::new, noise::Config::new,
yamux::Config::default, yamux::Config::default,
)? )?
.with_behaviour(|keypair| p2p::Behaviour::try_from(keypair).unwrap())? .with_behaviour(|keypair| caretta::p2p::Behaviour::try_from(keypair).unwrap())?
.build(); .build();
swarm.listen_on("/ip4/0.0.0.0/tcp/0".parse()?)?; swarm.listen_on("/ip4/0.0.0.0/tcp/0".parse()?)?;
loop{ loop{
@ -28,4 +31,9 @@ impl ServerTrait for Server {
}); });
} }
} }
async fn serve_rpc<T>(config: &T) -> Result<(), caretta::error::Error>
where T: AsRef<caretta::config::RpcConfig> {
todo!()
}
} }

View file

@ -1,20 +1,21 @@
mod server; mod server;
use clap::{Parser, Subcommand}; use clap::{Parser, Subcommand};
use caretta::cli::*; use caretta::{cli::*, utils::runnable::Runnable};
pub use server::*; pub use server::*;
#[derive(Debug, Parser)] #[derive(Debug, Parser, Runnable)]
pub struct Cli { pub struct Cli {
#[command(subcommand)] #[command(subcommand)]
#[runnable]
command: CliCommand command: CliCommand
} }
#[derive(Debug, Subcommand)] #[derive(Debug, Subcommand, Runnable)]
pub enum CliCommand { pub enum CliCommand {
Config(ConfigCommandArgs), Config(ConfigCommandArgs),
Device(DeviceCommandArgs), Device(DeviceCommandArgs),
Logs(LogsCommandArgs), Logs(LogsCommandArgs),
Peer(PeerSubcommand), Peer(PeerCommandArgs),
Server(ServerCommandArgs), Server(ServerCommandArgs),
} }

View file

@ -1,5 +1,5 @@
use clap::Args; 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 libp2p::{noise, ping, swarm::{NetworkBehaviour, SwarmEvent}, tcp, yamux, Swarm};
use super::ConfigArgs; use super::ConfigArgs;
@ -10,7 +10,8 @@ pub struct ServerCommandArgs {
config: ConfigArgs, config: ConfigArgs,
} }
impl Runnable for ServerCommandArgs { impl Runnable for ServerCommandArgs {
async fn run(self) { async fn run(self, app_name: &'static str) {
let config = CONFIG.get_or_init(self.config.try_into()?).await; let config = CONFIG.get_or_init::<Config>(self.config.into_config_unchecked(app_name).await).await;
} }
} }

View file

@ -1,3 +1,5 @@
use clap::Parser;
use crate::cli::Cli; use crate::cli::Cli;
mod cli; mod cli;