From e24ff770a5437797767e9fd1083d7b1b207be05d Mon Sep 17 00:00:00 2001 From: fluo10 Date: Sat, 16 Aug 2025 11:47:04 +0900 Subject: [PATCH] Rename BaseConfig to Config --- core/Cargo.toml | 3 +- core/src/config/mod.rs | 77 ++++++++++------------------- core/src/config/p2p.rs | 25 +--------- core/src/config/rpc.rs | 3 +- core/src/config/storage.rs | 23 +++++++-- core/src/rpc/mod.rs | 1 - core/src/rpc/server.rs | 16 ------ core/src/rpc/service/cached_peer.rs | 6 +-- core/src/utils/mod.rs | 9 ++++ examples/core/Cargo.toml | 2 +- examples/core/assets/favicon.ico | 3 -- examples/core/assets/header.svg | 20 -------- examples/core/assets/main.css | 46 ----------------- examples/core/src/lib.rs | 3 +- examples/core/src/rpc/mod.rs | 1 + examples/core/src/rpc/server.rs | 0 examples/core/src/ui/mod.rs | 1 - examples/core/src/ui/plain/mod.rs | 33 ------------- 18 files changed, 68 insertions(+), 204 deletions(-) delete mode 100644 core/src/rpc/server.rs delete mode 100644 examples/core/assets/favicon.ico delete mode 100644 examples/core/assets/header.svg delete mode 100644 examples/core/assets/main.css create mode 100644 examples/core/src/rpc/mod.rs create mode 100644 examples/core/src/rpc/server.rs delete mode 100644 examples/core/src/ui/mod.rs delete mode 100644 examples/core/src/ui/plain/mod.rs diff --git a/core/Cargo.toml b/core/Cargo.toml index 3150480..6728c8f 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -15,12 +15,13 @@ test = ["dep:tempfile", "macros"] [dependencies] base64 = "0.22.1" +caretta-macros = { path = "macros", optional = true } chrono.workspace = true chrono-tz = "0.10.3" ciborium.workspace = true clap = {workspace = true, optional = true} +dirs = "6.0.0" futures = "0.3.31" -caretta-macros = { path = "macros", optional = true } libp2p.workspace = true libp2p-core = { version = "0.43.0", features = ["serde"] } libp2p-identity = { version = "0.2.11", features = ["ed25519", "peerid", "rand", "serde"] } diff --git a/core/src/config/mod.rs b/core/src/config/mod.rs index a059b16..459d6d0 100644 --- a/core/src/config/mod.rs +++ b/core/src/config/mod.rs @@ -16,17 +16,16 @@ pub use rpc::*; #[cfg(feature="desktop")] use clap::Args; -pub trait Config: TryFrom{ - type PartialConfig: PartialConfig; -} -pub trait PartialConfig: Emptiable + From + Mergeable { - type Config: Config; - +#[derive(Clone, Debug)] +pub struct Config { + p2p: P2pConfig, + storage: StorageConfig, + rpc: RpcConfig, } #[cfg_attr(feature="desktop", derive(Args))] -#[derive(Clone, Debug, Deserialize, Serialize)] -pub struct BaseConfig { +#[derive(Clone, Debug, Default, Deserialize, PartialEq, Serialize)] +pub struct PartialConfig { #[cfg_attr(feature="desktop", command(flatten))] p2p: PartialP2pConfig, #[cfg_attr(feature="desktop", command(flatten))] @@ -35,7 +34,7 @@ pub struct BaseConfig { rpc: PartialRpcConfig, } -impl BaseConfig { +impl PartialConfig { fn new() -> Self { Self { p2p : PartialP2pConfig::empty().with_new_secret(), @@ -84,53 +83,29 @@ impl BaseConfig { } } +impl Emptiable for PartialConfig { + fn empty() -> Self { + Self { + p2p: PartialP2pConfig::empty(), + storage: PartialStorageConfig::empty(), + rpc: PartialRpcConfig::empty() + } + } + + fn is_empty(&self) -> bool { + self.p2p.is_empty() && self.rpc.is_empty() && self.storage.is_empty() + } +} + #[cfg(test)] mod tests { - use serde::{Deserialize, Serialize}; - - use crate::{tests::test_toml_serialize_deserialize, utils::{emptiable::Emptiable, mergeable::Mergeable}}; - - use super::{p2p::{P2pConfig, PartialP2pConfig}, PartialConfig}; - - #[derive(Debug, Deserialize, Serialize, PartialEq)] - pub struct TestConfig { - - p2p: Option - } - - impl Default for TestConfig { - fn default() -> Self { - Self { - p2p: Some(PartialP2pConfig::default()), - } - } - } - impl Emptiable for TestConfig { - fn empty() -> Self { - Self { - p2p: None, - } - } - - fn is_empty(&self) -> bool { - self.p2p.is_none() - } - } - impl Mergeable for TestConfig { - fn merge(&mut self, other: Self) { - if let Some(p2p) = other.p2p { - self.p2p = Some(p2p); - } - } - } + use libp2p::identity; + use super::*; + use crate::{tests::test_toml_serialize_deserialize}; #[tokio::test] async fn test_p2p_config_serialize_deserialize() { - test_toml_serialize_deserialize(TestConfig::empty()); - test_toml_serialize_deserialize(TestConfig::default()); - assert_eq!(TestConfig::empty(), toml::from_str("").unwrap()); - assert_eq!("", &toml::to_string(&TestConfig::empty()).unwrap()); + test_toml_serialize_deserialize(PartialConfig::empty()); } - } \ No newline at end of file diff --git a/core/src/config/p2p.rs b/core/src/config/p2p.rs index 8863bd6..ac9ee05 100644 --- a/core/src/config/p2p.rs +++ b/core/src/config/p2p.rs @@ -31,9 +31,8 @@ fn base64_to_keypair(base64: &str) -> Result { Ok(Keypair::from_protobuf_encoding(&vec)?) } -#[derive(Clone, Debug, Deserialize, Serialize,)] +#[derive(Clone, Debug)] pub struct P2pConfig { - #[serde(with = "keypair_parser")] pub secret: Keypair, pub listen_ips: Vec, pub port: u16, @@ -82,26 +81,6 @@ impl TryFrom for P2pConfig { } } -mod keypair_parser { - use libp2p::identity::Keypair; - use serde::{Deserialize, Deserializer, Serializer}; - - pub fn serialize(keypair: &Keypair, serializer: S) -> Result - where S: Serializer - { - serializer.serialize_str(&super::keypair_to_base64(keypair)) - } - pub fn deserialize<'de, D>(deserializer: D) -> Result - where D: Deserializer<'de> - { - match super::base64_to_keypair(&String::deserialize(deserializer)?) { - Ok(x) => Ok(x), - Err(crate::error::Error::Base64Decode(_)) => Err(serde::de::Error::custom("Decoding base64 error")), - Err(_) => unreachable!() - } - } -} - #[cfg_attr(feature="desktop",derive(Args))] #[derive(Clone, Debug, Deserialize, Serialize, PartialEq)] pub struct PartialP2pConfig { @@ -172,7 +151,7 @@ impl Mergeable for PartialP2pConfig { mod tests { use libp2p::identity; use super::*; - use crate::{config::PartialConfig, tests::test_toml_serialize_deserialize}; + use crate::{tests::test_toml_serialize_deserialize}; #[tokio::test] diff --git a/core/src/config/rpc.rs b/core/src/config/rpc.rs index e84c33f..2e390b0 100644 --- a/core/src/config/rpc.rs +++ b/core/src/config/rpc.rs @@ -10,6 +10,7 @@ use crate::config::error::ConfigError; #[cfg(unix)] static DEFAULT_SOCKET_PATH: &str = "caretta.sock"; +#[derive(Clone, Debug)] pub struct RpcConfig { pub socket_path: PathBuf, } @@ -24,7 +25,7 @@ impl TryFrom for RpcConfig { } #[cfg_attr(feature="desktop", derive(Args))] -#[derive(Clone, Debug, Deserialize, Serialize)] +#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)] pub struct PartialRpcConfig { pub socket_path: Option, } diff --git a/core/src/config/storage.rs b/core/src/config/storage.rs index 3bf7329..7e2f9a1 100644 --- a/core/src/config/storage.rs +++ b/core/src/config/storage.rs @@ -5,7 +5,7 @@ use clap::Args; #[cfg(any(test, feature="test"))] use tempfile::tempdir; -use crate::{config::{ConfigError, PartialConfig}, utils::{emptiable::Emptiable, mergeable::Mergeable}}; +use crate::{config::{ConfigError, PartialConfig}, utils::{emptiable::Emptiable, get_binary_name, mergeable::Mergeable}}; use libp2p::mdns::Config; use serde::{Deserialize, Serialize}; @@ -15,7 +15,7 @@ static CACHE_DATABASE_NAME: &str = "cache.sqlite"; #[cfg(any(test, feature="test"))] use crate::tests::{GlobalTestDefault, TestDefault}; -#[derive(Debug)] +#[derive(Clone, Debug)] pub struct StorageConfig { pub data_directory: PathBuf, pub cache_directory: PathBuf, @@ -50,7 +50,7 @@ impl TryFrom for StorageConfig { } } #[cfg_attr(feature="desktop", derive(Args))] -#[derive(Clone, Debug, Deserialize, Serialize)] +#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)] pub struct PartialStorageConfig { #[cfg_attr(feature="desktop", arg(long))] pub data_directory: Option, @@ -58,6 +58,23 @@ pub struct PartialStorageConfig { pub cache_directory: Option, } +impl Default for PartialStorageConfig { + fn default() -> Self { + #[cfg(any(target_os="linux", target_os="macos", target_os="windows"))] + { + let mut data_dir = dirs::data_local_dir().unwrap(); + data_dir.push(get_binary_name().unwrap()); + let mut cache_dir = dirs::cache_dir().unwrap(); + cache_dir.push(get_binary_name().unwrap()); + + Self { + data_directory: Some(data_dir), + cache_directory: Some(cache_dir) + } + } + } +} + impl From for PartialStorageConfig { fn from(config: StorageConfig) -> PartialStorageConfig { Self { diff --git a/core/src/rpc/mod.rs b/core/src/rpc/mod.rs index cbd5b6f..22ebb2d 100644 --- a/core/src/rpc/mod.rs +++ b/core/src/rpc/mod.rs @@ -1,3 +1,2 @@ -pub mod server; pub mod service; diff --git a/core/src/rpc/server.rs b/core/src/rpc/server.rs deleted file mode 100644 index 7214e47..0000000 --- a/core/src/rpc/server.rs +++ /dev/null @@ -1,16 +0,0 @@ -use tonic::transport::Server; - -use crate::{proto::cached_peer_service_server::CachedPeerServiceServer, rpc::service::cached_peer::CachedPeerService}; - - -pub async fn start_server() ->Result<(), Error> { - let addr = "[::1]:50051".parse()?; - let cached_peer_server = CachedPeerService::default(); - Server::builder() - .add_service(CachedPeerServiceServer::new(cached_peer_server)) - .serve(addr) - .await?; - - Ok(()) - -} \ No newline at end of file diff --git a/core/src/rpc/service/cached_peer.rs b/core/src/rpc/service/cached_peer.rs index d51b0a6..4f62191 100644 --- a/core/src/rpc/service/cached_peer.rs +++ b/core/src/rpc/service/cached_peer.rs @@ -2,16 +2,16 @@ use crate::{cache::entity::{CachedAddressEntity, CachedPeerEntity, CachedPeerMod use futures::future::join_all; use tonic::{Request, Response, Status}; -use crate::proto::{cached_peer_service_server::{CachedPeerService, CachedPeerServiceServer}, CachedPeerListRequest, CachedPeerListResponse, CachedPeerMessage}; +use crate::proto::{cached_peer_service_server::{CachedPeerServiceServer}, CachedPeerListRequest, CachedPeerListResponse, CachedPeerMessage}; use sea_orm::prelude::*; #[derive(Debug, Default)] -pub struct CachedPeerServer {} +pub struct CachedPeerService {} #[tonic::async_trait] -impl CachedPeerService for CachedPeerServer { +impl crate::proto::cached_peer_service_server::CachedPeerService for CachedPeerService { async fn list(&self, request: Request) -> Result, Status> { println!("Got a request: {:?}", request); diff --git a/core/src/utils/mod.rs b/core/src/utils/mod.rs index 0254b57..dab2f02 100644 --- a/core/src/utils/mod.rs +++ b/core/src/utils/mod.rs @@ -36,3 +36,12 @@ pub fn utc_to_timestamp(utc: &DateTime) -> Timestamp { pub fn timestamp_to_utc(t: &Timestamp) -> DateTime { Utc.timestamp_opt(t.seconds, u32::try_from(t.nanos).unwrap()).unwrap() } + +pub fn get_binary_name() -> Option { + std::env::current_exe() + .ok()? + .file_name()? + .to_str()? + .to_owned() + .into() +} \ No newline at end of file diff --git a/examples/core/Cargo.toml b/examples/core/Cargo.toml index 53a6fea..9cef966 100644 --- a/examples/core/Cargo.toml +++ b/examples/core/Cargo.toml @@ -7,4 +7,4 @@ license.workspace = true repository.workspace = true [dependencies] -dioxus.workspace = true \ No newline at end of file +caretta.path = "../.." \ No newline at end of file diff --git a/examples/core/assets/favicon.ico b/examples/core/assets/favicon.ico deleted file mode 100644 index c3c83af..0000000 --- a/examples/core/assets/favicon.ico +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:1c02a89f3d27fede4fa7f8c3702a58ab10caccc0a5ac5a4b8d7476053bb99049 -size 132770 diff --git a/examples/core/assets/header.svg b/examples/core/assets/header.svg deleted file mode 100644 index 59c96f2..0000000 --- a/examples/core/assets/header.svg +++ /dev/null @@ -1,20 +0,0 @@ - \ No newline at end of file diff --git a/examples/core/assets/main.css b/examples/core/assets/main.css deleted file mode 100644 index 90c0fc1..0000000 --- a/examples/core/assets/main.css +++ /dev/null @@ -1,46 +0,0 @@ -/* App-wide styling */ -body { - background-color: #0f1116; - color: #ffffff; - font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; - margin: 20px; -} - -#hero { - margin: 0; - display: flex; - flex-direction: column; - justify-content: center; - align-items: center; -} - -#links { - width: 400px; - text-align: left; - font-size: x-large; - color: white; - display: flex; - flex-direction: column; -} - -#links a { - color: white; - text-decoration: none; - margin-top: 20px; - margin: 10px 0px; - border: white 1px solid; - border-radius: 5px; - padding: 10px; -} - -#links a:hover { - background-color: #1f1f1f; - cursor: pointer; -} - -#header { - max-width: 1200px; -} - - - diff --git a/examples/core/src/lib.rs b/examples/core/src/lib.rs index b71b381..de6b564 100644 --- a/examples/core/src/lib.rs +++ b/examples/core/src/lib.rs @@ -1 +1,2 @@ -pub mod ui; \ No newline at end of file +pub mod rpc; + diff --git a/examples/core/src/rpc/mod.rs b/examples/core/src/rpc/mod.rs new file mode 100644 index 0000000..bfe15ae --- /dev/null +++ b/examples/core/src/rpc/mod.rs @@ -0,0 +1 @@ +pub mod server; \ No newline at end of file diff --git a/examples/core/src/rpc/server.rs b/examples/core/src/rpc/server.rs new file mode 100644 index 0000000..e69de29 diff --git a/examples/core/src/ui/mod.rs b/examples/core/src/ui/mod.rs deleted file mode 100644 index d76e31c..0000000 --- a/examples/core/src/ui/mod.rs +++ /dev/null @@ -1 +0,0 @@ -pub mod plain; \ No newline at end of file diff --git a/examples/core/src/ui/plain/mod.rs b/examples/core/src/ui/plain/mod.rs deleted file mode 100644 index dc5fd57..0000000 --- a/examples/core/src/ui/plain/mod.rs +++ /dev/null @@ -1,33 +0,0 @@ -use dioxus::prelude::*; - -const FAVICON: Asset = asset!("/assets/favicon.ico"); -const MAIN_CSS: Asset = asset!("/assets/main.css"); -const HEADER_SVG: Asset = asset!("/assets/header.svg"); - -#[component] -pub fn App() -> Element { - rsx! { - document::Link { rel: "icon", href: FAVICON } - document::Link { rel: "stylesheet", href: MAIN_CSS } - Hero {} - - } -} - -#[component] -pub fn Hero() -> Element { - rsx! { - div { - id: "hero", - img { src: HEADER_SVG, id: "header" } - div { id: "links", - a { href: "https://dioxuslabs.com/learn/0.6/", "📚 Learn Dioxus" } - a { href: "https://dioxuslabs.com/awesome", "🚀 Awesome Dioxus" } - a { href: "https://github.com/dioxus-community/", "📡 Community Libraries" } - a { href: "https://github.com/DioxusLabs/sdk", "⚙️ Dioxus Development Kit" } - a { href: "https://marketplace.visualstudio.com/items?itemName=DioxusLabs.dioxus", "💫 VSCode Extension" } - a { href: "https://discord.gg/XgGxMSkvUM", "👋 Community Discord" } - } - } - } -}