From ba87ccf7343fb92d190ac0ff86f4e4c3cce8a47c Mon Sep 17 00:00:00 2001 From: fluo10 Date: Sat, 24 May 2025 06:11:00 +0900 Subject: [PATCH] Add example, config, cli --- Cargo.toml | 3 +- examples/timestamper/Cargo.toml | 10 +++++ examples/timestamper/src/main.rs | 3 ++ lazy-supplements/Cargo.toml | 9 ++++- lazy-supplements/src/cli/connect.rs | 13 +++++++ lazy-supplements/src/cli/init.rs | 29 +++++++++++++++ lazy-supplements/src/cli/mod.rs | 9 +++++ lazy-supplements/src/cli/server.rs | 13 +++++++ lazy-supplements/src/config/mod.rs | 5 +++ lazy-supplements/src/config/node.rs | 53 +++++++++++++++++++++++++++ lazy-supplements/src/config/server.rs | 9 +++++ lazy-supplements/src/lib.rs | 2 + 12 files changed, 156 insertions(+), 2 deletions(-) create mode 100644 examples/timestamper/Cargo.toml create mode 100644 examples/timestamper/src/main.rs create mode 100644 lazy-supplements/src/cli/connect.rs create mode 100644 lazy-supplements/src/cli/init.rs create mode 100644 lazy-supplements/src/cli/mod.rs create mode 100644 lazy-supplements/src/cli/server.rs create mode 100644 lazy-supplements/src/config/mod.rs create mode 100644 lazy-supplements/src/config/node.rs create mode 100644 lazy-supplements/src/config/server.rs diff --git a/Cargo.toml b/Cargo.toml index 388f5a2..fc57abd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [workspace] -members = [ "lazy-supplements", "lazy-supplements-*" ] +members = [ "examples/timestamper", "lazy-supplements", "lazy-supplements-*" ] [workspace.package] edition = "2024" @@ -9,4 +9,5 @@ license = "MIT OR Apache-2.0" repository = "https://forgejo.fireturlte.net" [workspace.dependencies] +lazy-supplements.path = "lazy-supplements" libp2p = "0.55.0" diff --git a/examples/timestamper/Cargo.toml b/examples/timestamper/Cargo.toml new file mode 100644 index 0000000..8a40a3e --- /dev/null +++ b/examples/timestamper/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "timestamper" +edition.workspace = true +version.workspace = true +description.workspace = true +license.workspace = true +repository.workspace = true + +[dependencies] +lazy-supplements.workspace = true \ No newline at end of file diff --git a/examples/timestamper/src/main.rs b/examples/timestamper/src/main.rs new file mode 100644 index 0000000..e7a11a9 --- /dev/null +++ b/examples/timestamper/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, world!"); +} diff --git a/lazy-supplements/Cargo.toml b/lazy-supplements/Cargo.toml index fa71961..a2e1adc 100644 --- a/lazy-supplements/Cargo.toml +++ b/lazy-supplements/Cargo.toml @@ -8,4 +8,11 @@ repository.workspace = true [dependencies] automerge = "0.6.1" -libp2p.workspace = true \ No newline at end of file +autosurgeon = "0.8.7" +base64 = "0.22.1" +clap = { version = "4.5.38", features = ["derive"] } +libp2p.workspace = true +serde = { version = "1.0.219", features = ["derive"] } +thiserror = "2.0.12" +tokio = { version = "1.45.0", features = ["macros", "rt"] } +toml = "0.8.22" diff --git a/lazy-supplements/src/cli/connect.rs b/lazy-supplements/src/cli/connect.rs new file mode 100644 index 0000000..0bda4d0 --- /dev/null +++ b/lazy-supplements/src/cli/connect.rs @@ -0,0 +1,13 @@ +use std::{net::IpAddr, path::PathBuf}; + +use clap::Args; + +#[derive(Args, Debug)] +pub struct ConnectArgs { + #[arg(long)] + endpoint: IpAddr, + #[arg(long)] + port: i32, + #[arg(long)] + config: PathBuf, +} \ No newline at end of file diff --git a/lazy-supplements/src/cli/init.rs b/lazy-supplements/src/cli/init.rs new file mode 100644 index 0000000..e80283e --- /dev/null +++ b/lazy-supplements/src/cli/init.rs @@ -0,0 +1,29 @@ +use std::path::PathBuf; + +use clap::Args; +use libp2p::identity; + +#[derive(Args, Debug)] +pub struct InitArgs { + #[arg(long)] + config: Option +} + +impl InitArgs { + fn main(self) { + let config_path = if let Some(x) = self.config { + x + } else { + crate::cli::default_config_path() + }; + if config_path.exists() { + println!("Config file already exists!"); + return; + } else { + let keypair = identity::Keypair::generate_ed25519(); + let buf = keypair.to_protobuf_encoding().unwrap(); + + } + + } +} \ No newline at end of file diff --git a/lazy-supplements/src/cli/mod.rs b/lazy-supplements/src/cli/mod.rs new file mode 100644 index 0000000..715937c --- /dev/null +++ b/lazy-supplements/src/cli/mod.rs @@ -0,0 +1,9 @@ +use std::path::PathBuf; + +mod connect; +mod init; +mod server; + +pub fn default_config_path() -> PathBuf { + todo!() +} \ No newline at end of file diff --git a/lazy-supplements/src/cli/server.rs b/lazy-supplements/src/cli/server.rs new file mode 100644 index 0000000..0b2e4f4 --- /dev/null +++ b/lazy-supplements/src/cli/server.rs @@ -0,0 +1,13 @@ +use std::{net::IpAddr, path::PathBuf}; + +use clap::Args; + +#[derive(Args, Debug)] +pub struct ServerArgs { + #[arg(long)] + listen_ip: IpAddr, + #[arg(long)] + port: i32, + #[arg(long)] + config: PathBuf, +} \ No newline at end of file diff --git a/lazy-supplements/src/config/mod.rs b/lazy-supplements/src/config/mod.rs new file mode 100644 index 0000000..16b8333 --- /dev/null +++ b/lazy-supplements/src/config/mod.rs @@ -0,0 +1,5 @@ +mod node; +mod server; + +pub use node::NodeConfig; +pub use server::ServerConfig; diff --git a/lazy-supplements/src/config/node.rs b/lazy-supplements/src/config/node.rs new file mode 100644 index 0000000..4f80ba9 --- /dev/null +++ b/lazy-supplements/src/config/node.rs @@ -0,0 +1,53 @@ +use libp2p::identity::{self, Keypair}; +use serde::{Deserialize, Serialize}; + +#[derive(Debug, Deserialize, Serialize)] +pub struct NodeConfig { + #[serde(with = "keypair")] + secret: Keypair, +} + +mod keypair { + use base64::{prelude::BASE64_STANDARD, Engine}; + use libp2p::identity::Keypair; + use serde::{Deserialize, Deserializer, Serializer}; + + pub fn serialize(keypair: &Keypair, serializer: S) -> Result + where S: Serializer + { + let vec = keypair.to_protobuf_encoding().unwrap(); + let base64 = BASE64_STANDARD.encode(vec); + serializer.serialize_str(&base64) + } + pub fn deserialize<'de, D>(deserializer: D) -> Result + where D: Deserializer<'de> + { + let base64 = String::deserialize(deserializer)?; + let vec = BASE64_STANDARD.decode(base64).unwrap(); + Ok(Keypair::from_protobuf_encoding(&vec).unwrap()) + } + +} + +pub struct PartialNodeConfig { + pub secret: Option>, +} + +#[cfg(test)] +mod tests { + use libp2p::identity; + use super::*; + + + #[tokio::test] + async fn parse_ed25519() { + let keypair = identity::Keypair::generate_ed25519(); + let config = NodeConfig { + secret: keypair.clone(), + }; + let string = toml::to_string(&config).unwrap(); + println!("Parsed config: {}", &string); + let parsed_config: NodeConfig = toml::from_str(&string).unwrap(); + assert_eq!(keypair.public(), parsed_config.secret.public()) + } +} diff --git a/lazy-supplements/src/config/server.rs b/lazy-supplements/src/config/server.rs new file mode 100644 index 0000000..854dc7c --- /dev/null +++ b/lazy-supplements/src/config/server.rs @@ -0,0 +1,9 @@ +use std::{collections::HashSet, net::IpAddr}; + +use serde::{Deserialize, Serialize}; + +#[derive(Debug, Deserialize, Serialize)] +pub struct ServerConfig { + listen_ips: HashSet, + port: i32, +} diff --git a/lazy-supplements/src/lib.rs b/lazy-supplements/src/lib.rs index e69de29..973dec0 100644 --- a/lazy-supplements/src/lib.rs +++ b/lazy-supplements/src/lib.rs @@ -0,0 +1,2 @@ +pub mod cli; +pub mod config; \ No newline at end of file