diff --git a/core/src/config/mod.rs b/core/src/config/mod.rs index 459d6d0..fd084a2 100644 --- a/core/src/config/mod.rs +++ b/core/src/config/mod.rs @@ -18,37 +18,37 @@ use clap::Args; #[derive(Clone, Debug)] pub struct Config { - p2p: P2pConfig, - storage: StorageConfig, - rpc: RpcConfig, + pub p2p: P2pConfig, + pub storage: StorageConfig, + pub rpc: RpcConfig, } #[cfg_attr(feature="desktop", derive(Args))] #[derive(Clone, Debug, Default, Deserialize, PartialEq, Serialize)] pub struct PartialConfig { #[cfg_attr(feature="desktop", command(flatten))] - p2p: PartialP2pConfig, + pub p2p: PartialP2pConfig, #[cfg_attr(feature="desktop", command(flatten))] - storage: PartialStorageConfig, + pub storage: PartialStorageConfig, #[cfg_attr(feature="desktop", command(flatten))] - rpc: PartialRpcConfig, + pub rpc: PartialRpcConfig, } impl PartialConfig { - fn new() -> Self { + pub fn new() -> Self { Self { p2p : PartialP2pConfig::empty().with_new_secret(), storage: PartialStorageConfig::empty(), rpc: PartialRpcConfig::empty(), } } - fn from_toml(s: &str) -> Result { + pub fn from_toml(s: &str) -> Result { toml::from_str(s) } - fn into_toml(&self) -> Result { + pub fn into_toml(&self) -> Result { toml::to_string(self) } - async fn read_or_create(path: T) -> Result + pub async fn read_or_create(path: T) -> Result where T: AsRef { @@ -57,7 +57,7 @@ impl PartialConfig { } Self::read_from(&path).await } - async fn read_from(path:T) -> Result + pub async fn read_from(path:T) -> Result where T: AsRef { @@ -67,7 +67,7 @@ impl PartialConfig { let config: Self = toml::from_str(&content)?; Ok(config) } - async fn write_to(&self, path:T) -> Result<(), ConfigError> + pub async fn write_to(&self, path:T) -> Result<(), ConfigError> where T: AsRef { diff --git a/core/src/global/config.rs b/core/src/global/config.rs index 4cf5916..836f61e 100644 --- a/core/src/global/config.rs +++ b/core/src/global/config.rs @@ -1,15 +1,46 @@ -use crate::{config::{P2pConfig, StorageConfig}, error::Error, global::GlobalConstant}; +use tempfile::TempDir; +use tokio::sync::OnceCell; -pub static STORAGE_CONFIG: GlobalConstant = GlobalConstant::const_new(stringify!(STORAGE_CONFIG)); -pub static P2P_CONFIG: GlobalConstant = GlobalConstant::const_new(stringify!(P2P_CONFIG)); +use crate::{config::{Config, PartialP2pConfig, PartialRpcConfig, PartialStorageConfig, StorageConfig}, error::Error, global::GlobalConstant}; -#[cfg(test)] -mod tests { - use crate::global::{config::P2P_CONFIG, STORAGE_CONFIG}; +pub static CONFIG: GlobalConfig = GlobalConfig::const_new(); +pub struct GlobalConfig { + inner: OnceCell +} - #[test] - fn test_global_constant_names() { - assert_eq!(STORAGE_CONFIG.name, stringify!(STORAGE_CONFIG)); - assert_eq!(P2P_CONFIG.name, stringify!(P2P_CONFIG)); +impl GlobalConfig { + pub const fn const_new() -> Self { + Self{ + inner: OnceCell::const_new() + } + } + pub async fn get_or_init(&'static self, source: Config) -> &'static Config { + self.inner.get_or_init(|| async { + source + }).await + } + pub fn get(&'static self) -> Option<&'static Config> { + self.inner.get() + } + pub fn get_and_unwrap(&'static self) -> &'static Config { + self.get().expect(&format!("Config is uninitialized!")) + } + #[cfg(any(test, feature=test))] + pub async fn get_or_init_test(&'static self) -> &'static Config { + let temp_dir = TempDir::new().unwrap().keep(); + let mut data_dir = temp_dir.clone(); + data_dir.push("data"); + let mut cache_dir = temp_dir; + cache_dir.push("cache"); + + + self.get_or_init(Config { + p2p: PartialP2pConfig::default().with_new_secret().try_into().unwrap(), + storage: StorageConfig { + data_directory: data_dir, + cache_directory: cache_dir, + }, + rpc: PartialRpcConfig::default().try_into().unwrap(), + }).await } } \ No newline at end of file diff --git a/core/src/global/database_connection.rs b/core/src/global/database_connection.rs index 6ee4a37..72d24f3 100644 --- a/core/src/global/database_connection.rs +++ b/core/src/global/database_connection.rs @@ -44,12 +44,12 @@ pub use tests::*; #[cfg(test)] mod tests { use super::*; - use crate::{cache::migration::CacheMigrator, data::migration::DataMigrator, global::STORAGE_CONFIG, tests::GlobalTestDefault}; + use crate::{cache::migration::CacheMigrator, data::migration::DataMigrator, global::CONFIG, tests::GlobalTestDefault}; pub async fn get_or_init_test_data_database() -> &'static DatabaseConnection{ - DATA_DATABASE_CONNECTION.get_or_init(STORAGE_CONFIG.get_or_init_test_default().await.get_data_database_path(), DataMigrator).await + DATA_DATABASE_CONNECTION.get_or_init(CONFIG.get_or_init_test_default().await.storage.get_cache_database_path(), DataMigrator).await } pub async fn get_or_init_test_cache_database() -> &'static DatabaseConnection{ - CACHE_DATABASE_CONNECTION.get_or_init(STORAGE_CONFIG.get_or_init_test_default().await.get_cache_database_path(), CacheMigrator).await + CACHE_DATABASE_CONNECTION.get_or_init(CONFIG.get_or_init_test_default()await.storage.get_cache_database_path(), CacheMigrator).await } } \ No newline at end of file diff --git a/core/src/global/mod.rs b/core/src/global/mod.rs index 1a91c6e..750f1cb 100644 --- a/core/src/global/mod.rs +++ b/core/src/global/mod.rs @@ -58,7 +58,7 @@ impl GlobalConstant { self.inner.get() } pub fn get_and_unwrap(&'static self) -> &'static T { - self.get().expect(&format!("{} is uninitialized!", &stringify!(self))) + self.get().expect(&format!("{} is uninitialized!", self.name)) } } diff --git a/core/src/tests.rs b/core/src/tests.rs index 50d4ffc..b625771 100644 --- a/core/src/tests.rs +++ b/core/src/tests.rs @@ -29,20 +29,3 @@ pub trait TestDefault { pub trait GlobalTestDefault { async fn get_or_init_test_default(&'static self) -> &'static T; } - -pub fn test_cbor_serialize_deserialize(src: T) -where T: DeserializeOwned + Serialize + PartialEq + std::fmt::Debug -{ - let mut buf: Vec = Vec::new(); - ciborium::into_writer(&src, &mut buf).unwrap(); - let dst: T = ciborium::from_reader(buf.as_slice()).unwrap(); - assert_eq!(src, dst); -} - -pub fn test_toml_serialize_deserialize(src: T) -where T: DeserializeOwned + Serialize + PartialEq + std::fmt::Debug -{ - let buf = toml::to_string(&src).unwrap(); - let dst: T = toml::from_str(&buf).unwrap(); - assert_eq!(src, dst); -} diff --git a/desktop/src/cli/args/config.rs b/desktop/src/cli/args/config.rs index f446d69..498ca27 100644 --- a/desktop/src/cli/args/config.rs +++ b/desktop/src/cli/args/config.rs @@ -1,8 +1,7 @@ use std::{net::IpAddr, path::PathBuf}; use clap::Args; -use caretta_core::config::{BaseConfig, ConfigError}; -use crate::config::{PartialP2pConfig, PartialStorageConfig}; +use caretta_core::config::{PartialConfig,PartialP2pConfig, PartialStorageConfig, ConfigError}; use serde::{Deserialize, Serialize}; @@ -13,9 +12,9 @@ pub struct ConfigArgs { #[arg(short = 'c', long = "config")] pub file_path: Option, #[arg(skip)] - pub file_content: Option, + pub file_content: Option, #[command(flatten)] - pub args: BaseConfig, + pub args: PartialConfig, } @@ -23,9 +22,9 @@ impl ConfigArgs { pub fn get_file_path_or_default(&self) -> PathBuf { self.file_path.clone().unwrap_or((*DEFAULT_CONFIG_FILE_PATH).clone()) } - pub async fn get_or_read_file_content(&mut self) -> &mut BaseConfig { + pub async fn get_or_read_file_content(&mut self) -> &mut PartialConfig { self.file_content.get_or_insert( - BaseConfig::read_from(self.get_file_path_or_default()).await.unwrap() + PartialConfig::read_from(self.get_file_path_or_default()).await.unwrap() ) } } diff --git a/desktop/src/cli/mod.rs b/desktop/src/cli/mod.rs index 9f36a6b..c6d651d 100644 --- a/desktop/src/cli/mod.rs +++ b/desktop/src/cli/mod.rs @@ -2,11 +2,11 @@ use std::path::PathBuf; mod args; mod device; -mod server; +mod peer; pub use args::*; pub use device::*; -pub use server::*; +pub use peer::*; pub trait RunnableCommand { async fn run(self); diff --git a/desktop/src/cli/server.rs b/desktop/src/cli/server.rs deleted file mode 100644 index 64fe37f..0000000 --- a/desktop/src/cli/server.rs +++ /dev/null @@ -1,18 +0,0 @@ -use clap::Args; -use caretta_core::utils::runnable::Runnable; -use libp2p::{noise, ping, swarm::{NetworkBehaviour, SwarmEvent}, tcp, yamux, Swarm}; - -use crate::{error::Error, global::P2P_CONFIG}; - -use super::ConfigArgs; - -#[derive(Args, Debug)] -pub struct ServerCommandArgs { - #[command(flatten)] - config: ConfigArgs, -} -impl Runnable for ServerCommandArgs { - async fn run(self) { - P2P_CONFIG.get_and_unwrap().clone().launch_swarm(); - } -} \ No newline at end of file diff --git a/examples/core/Cargo.toml b/examples/core/Cargo.toml index 9cef966..41c03d9 100644 --- a/examples/core/Cargo.toml +++ b/examples/core/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "caretta-examples-core" +name = "caretta-example-core" edition.workspace = true version.workspace = true description.workspace = true @@ -7,4 +7,4 @@ license.workspace = true repository.workspace = true [dependencies] -caretta.path = "../.." \ No newline at end of file +caretta.path = "../.." diff --git a/examples/desktop/Cargo.toml b/examples/desktop/Cargo.toml index d1a83a8..593a985 100644 --- a/examples/desktop/Cargo.toml +++ b/examples/desktop/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "caretta-examples-desktop" +name = "caretta-example-desktop" version = "0.1.0" edition = "2021" @@ -7,12 +7,8 @@ edition = "2021" [dependencies] clap.workspace = true -dioxus.workspace = true -caretta-desktop.path = "../../desktop" -caretta-examples-core.path = "../core" +caretta = { path = "../..", features = ["desktop"] } +caretta-example-core.path = "../core" +libp2p.workspace = true +tokio.workspace = true -[features] -default = ["desktop"] -web = ["dioxus/web"] -desktop = ["dioxus/desktop"] -mobile = ["dioxus/mobile"] diff --git a/examples/desktop/src/cli.rs b/examples/desktop/src/cli/mod.rs similarity index 85% rename from examples/desktop/src/cli.rs rename to examples/desktop/src/cli/mod.rs index 63d4b54..5484284 100644 --- a/examples/desktop/src/cli.rs +++ b/examples/desktop/src/cli/mod.rs @@ -1,5 +1,8 @@ +mod server; use clap::{Parser, Subcommand}; -use caretta_desktop::cli::*; +use caretta::cli::*; +pub use server::*; + #[derive(Debug, Parser)] pub struct Cli { diff --git a/examples/desktop/src/cli/server.rs b/examples/desktop/src/cli/server.rs new file mode 100644 index 0000000..beb2cfa --- /dev/null +++ b/examples/desktop/src/cli/server.rs @@ -0,0 +1,19 @@ +use clap::Args; +use caretta::{error::Error, utils::runnable::Runnable}; +use libp2p::{noise, ping, swarm::{NetworkBehaviour, SwarmEvent}, tcp, yamux, Swarm}; + +use super::ConfigArgs; + +#[derive(Args, Debug)] +pub struct ServerCommandArgs { + #[command(flatten)] + config: ConfigArgs, +} +impl Runnable for ServerCommandArgs { + async fn run(self) { + let swarm_handler = P2P_CONFIG.get_and_unwrap().clone().launch_swarm(); + let server_handler = caretta_example_core::rpc::server::start_server(); + + let (swarm_result, server_result) = tokio::try_join!(swarm_handler, server_handler).unwrap(); + } +} \ No newline at end of file diff --git a/examples/desktop/src/main.rs b/examples/desktop/src/main.rs index e186ccc..a061f39 100644 --- a/examples/desktop/src/main.rs +++ b/examples/desktop/src/main.rs @@ -1,5 +1,11 @@ +use crate::cli::Cli; + mod cli; mod ipc; -fn main() { - dioxus::launch(caretta_examples_core::ui::plain::App); + +#[tokio::main] +async fn main() { + let args = Cli::parse(); + + } diff --git a/examples/mobile/Cargo.toml b/examples/mobile/Cargo.toml index 8441696..9682ac8 100644 --- a/examples/mobile/Cargo.toml +++ b/examples/mobile/Cargo.toml @@ -1,17 +1,9 @@ [package] -name = "caretta-examples-mobile" +name = "caretta-example-mobile" version = "0.1.0" authors = ["fluo10 "] edition = "2021" -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - [dependencies] dioxus.workspace = true -caretta-examples-core.path = "../core" - -[features] -default = ["mobile"] -web = ["dioxus/web"] -desktop = ["dioxus/desktop"] -mobile = ["dioxus/mobile"] +caretta-example-core.path = "../core" diff --git a/src/lib.rs b/src/lib.rs index 508000e..ebb0f03 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,5 @@ pub use caretta_core::*; -#[cfg(desktop)] +#[cfg(feature = "desktop")] pub use caretta_desktop::*; -#[cfg(mobile)] +#[cfg(feature = "mobile")] pub use caretta_mobile::*; \ No newline at end of file